diff --git a/AG/AttributeGraph.xcframework/ios-arm64-arm64e/AttributeGraph.framework/Headers/AGAttribute.h b/AG/AttributeGraph.xcframework/ios-arm64-arm64e/AttributeGraph.framework/Headers/AGAttribute.h index caf5b79e..dd014270 100644 --- a/AG/AttributeGraph.xcframework/ios-arm64-arm64e/AttributeGraph.framework/Headers/AGAttribute.h +++ b/AG/AttributeGraph.xcframework/ios-arm64-arm64e/AttributeGraph.framework/Headers/AGAttribute.h @@ -64,7 +64,11 @@ const AGAttributeInfo AGGraphGetAttributeInfo(AGAttribute attribute) AG_SWIFT_NA AG_EXPORT AG_REFINED_FOR_SWIFT -void AGGraphMutateAttribute(AGAttribute attribute, const AGTypeID type, bool invalidating/*, closure*/); +void AGGraphMutateAttribute(AGAttribute attribute, + const AGTypeID type, + bool invalidating, + const void (*function)(const void * _Nullable context AG_SWIFT_CONTEXT, void *body) AG_SWIFT_CC(swift), + const void * _Nullable context); AG_EXPORT AG_REFINED_FOR_SWIFT diff --git a/AG/AttributeGraph.xcframework/ios-arm64-x86_64-simulator/AttributeGraph.framework/Headers/AGAttribute.h b/AG/AttributeGraph.xcframework/ios-arm64-x86_64-simulator/AttributeGraph.framework/Headers/AGAttribute.h index caf5b79e..dd014270 100644 --- a/AG/AttributeGraph.xcframework/ios-arm64-x86_64-simulator/AttributeGraph.framework/Headers/AGAttribute.h +++ b/AG/AttributeGraph.xcframework/ios-arm64-x86_64-simulator/AttributeGraph.framework/Headers/AGAttribute.h @@ -64,7 +64,11 @@ const AGAttributeInfo AGGraphGetAttributeInfo(AGAttribute attribute) AG_SWIFT_NA AG_EXPORT AG_REFINED_FOR_SWIFT -void AGGraphMutateAttribute(AGAttribute attribute, const AGTypeID type, bool invalidating/*, closure*/); +void AGGraphMutateAttribute(AGAttribute attribute, + const AGTypeID type, + bool invalidating, + const void (*function)(const void * _Nullable context AG_SWIFT_CONTEXT, void *body) AG_SWIFT_CC(swift), + const void * _Nullable context); AG_EXPORT AG_REFINED_FOR_SWIFT diff --git a/AG/AttributeGraph.xcframework/macos-arm64e-arm64-x86_64/AttributeGraph.framework/Headers/AGAttribute.h b/AG/AttributeGraph.xcframework/macos-arm64e-arm64-x86_64/AttributeGraph.framework/Headers/AGAttribute.h index caf5b79e..dd014270 100644 --- a/AG/AttributeGraph.xcframework/macos-arm64e-arm64-x86_64/AttributeGraph.framework/Headers/AGAttribute.h +++ b/AG/AttributeGraph.xcframework/macos-arm64e-arm64-x86_64/AttributeGraph.framework/Headers/AGAttribute.h @@ -64,7 +64,11 @@ const AGAttributeInfo AGGraphGetAttributeInfo(AGAttribute attribute) AG_SWIFT_NA AG_EXPORT AG_REFINED_FOR_SWIFT -void AGGraphMutateAttribute(AGAttribute attribute, const AGTypeID type, bool invalidating/*, closure*/); +void AGGraphMutateAttribute(AGAttribute attribute, + const AGTypeID type, + bool invalidating, + const void (*function)(const void * _Nullable context AG_SWIFT_CONTEXT, void *body) AG_SWIFT_CC(swift), + const void * _Nullable context); AG_EXPORT AG_REFINED_FOR_SWIFT diff --git a/AG/Sources/Headers/AGAttribute.h b/AG/Sources/Headers/AGAttribute.h index caf5b79e..dd014270 100644 --- a/AG/Sources/Headers/AGAttribute.h +++ b/AG/Sources/Headers/AGAttribute.h @@ -64,7 +64,11 @@ const AGAttributeInfo AGGraphGetAttributeInfo(AGAttribute attribute) AG_SWIFT_NA AG_EXPORT AG_REFINED_FOR_SWIFT -void AGGraphMutateAttribute(AGAttribute attribute, const AGTypeID type, bool invalidating/*, closure*/); +void AGGraphMutateAttribute(AGAttribute attribute, + const AGTypeID type, + bool invalidating, + const void (*function)(const void * _Nullable context AG_SWIFT_CONTEXT, void *body) AG_SWIFT_CC(swift), + const void * _Nullable context); AG_EXPORT AG_REFINED_FOR_SWIFT diff --git a/Sources/OpenGraph/Attribute/Attribute/OGAttribute.swift b/Sources/OpenGraph/Attribute/Attribute/OGAttribute.swift index 2cabafe2..bc561afa 100644 --- a/Sources/OpenGraph/Attribute/Attribute/OGAttribute.swift +++ b/Sources/OpenGraph/Attribute/Attribute/OGAttribute.swift @@ -43,9 +43,10 @@ extension OGAttribute { bodyType._visitBody(&visitor, info.body) } - public func mutateBody(as type: Value.Type, invalidating: Bool, _: (inout Value) -> Void) { - // FIXME: pass body - __OGGraphMutateAttribute(self, OGTypeID(type), invalidating) + public func mutateBody(as type: Value.Type, invalidating: Bool, _ body: (inout Value) -> Void) { + OGAttribute.mutateAttribute(self, type: OGTypeID(type), invalidating: invalidating) { value in + body(&value.assumingMemoryBound(to: Value.self).pointee) + } } public func breadthFirstSearch(options _: OGSearchOptions = [], _: (OGAttribute) -> Bool) -> Bool { @@ -83,3 +84,14 @@ extension OGAttribute: CustomStringConvertible { } public typealias AttributeUpdateBlock = () -> (UnsafeMutableRawPointer, OGAttribute) -> Void + +// FIXME: migrate to use @_extern(c, "xx") in Swift 6 +extension OGAttribute { + @_silgen_name("OGGraphMutateAttribute") + private static func mutateAttribute( + _ attribute: OGAttribute, + type: OGTypeID, + invalidating: Bool, + body: (UnsafeMutableRawPointer) -> Void + ) +} diff --git a/Sources/_OpenGraph/Attribute/AttributeID.hpp b/Sources/_OpenGraph/Attribute/AttributeID.hpp index 533ef062..f64465f0 100644 --- a/Sources/_OpenGraph/Attribute/AttributeID.hpp +++ b/Sources/_OpenGraph/Attribute/AttributeID.hpp @@ -10,6 +10,7 @@ #include "OGBase.h" #include "OGAttribute.h" +#include "../Util/assert.hpp" namespace OG { class AttributeID final { @@ -32,7 +33,7 @@ class AttributeID final { } public: OG_INLINE OG_CONSTEXPR - AttributeID(OGAttribute attribute) OG_NOEXCEPT: + AttributeID(OGAttribute& attribute) OG_NOEXCEPT: _rawValue(attribute) {} OG_INLINE OG_CONSTEXPR @@ -61,6 +62,13 @@ class AttributeID final { const bool isNil() const OG_NOEXCEPT { return getKind() == Kind::Nil; } + + OG_INLINE OG_CONSTEXPR + const void checkIsDirect() const OG_NOEXCEPT { + if (!isDirect()) { + OG::precondition_failure("non-direct attribute id: %u", _rawValue); + } + } }; static_assert(sizeof(AttributeID) == sizeof(uint32_t)); } diff --git a/Sources/_OpenGraph/Attribute/OGAttribute.cpp b/Sources/_OpenGraph/Attribute/OGAttribute.cpp index 0713d80a..0b0c8e01 100644 --- a/Sources/_OpenGraph/Attribute/OGAttribute.cpp +++ b/Sources/_OpenGraph/Attribute/OGAttribute.cpp @@ -50,40 +50,38 @@ OGAttribute OGGraphCreateIndirectAttribute2(OGAttribute attribute, uint64_t size OGAttributeFlags OGGraphGetFlags(OGAttribute attribute) { const OG::AttributeID id = OG::AttributeID(attribute); - if (!id.isDirect()) { - OG::precondition_failure("non-direct attribute id: %u", id); - } + id.checkIsDirect(); // TODO: data/table return OGAttributeFlagsDefault; } void OGGraphSetFlags(OGAttribute attribute, OGAttributeFlags flags) { const OG::AttributeID id = OG::AttributeID(attribute); - if (!id.isDirect()) { - OG::precondition_failure("non-direct attribute id: %u", id); - } + id.checkIsDirect(); // TODO: data/table } void OGGraphAddInput(OGAttribute attribute1, OGAttribute attribute2, OGInputOptions options, long token) { const OG::AttributeID id = OG::AttributeID(attribute1); - if (!id.isDirect()) { - OG::precondition_failure("non-direct attribute id: %u", id); - } + id.checkIsDirect(); // TODO: data/table } const OGAttributeInfo OGGraphGetAttributeInfo(OGAttribute attribute) { const OG::AttributeID id = OG::AttributeID(attribute); - if (!id.isDirect()) { - OG::precondition_failure("non-direct attribute id: %u", id); - } + id.checkIsDirect(); // TODO return { nullptr, nullptr }; } -void OGGraphMutateAttribute(OGAttribute attribute, const OGTypeID type, bool invalidating) { - // TODO +void OGGraphMutateAttribute(OGAttribute attribute, + const OGTypeID type, + bool invalidating, + const void (*function)(const void * _Nullable context OG_SWIFT_CONTEXT, void *body) OG_SWIFT_CC(swift), + const void * _Nullable context) { + const OG::AttributeID id = OG::AttributeID(attribute); + id.checkIsDirect(); + // attribute_modify(AG::data::ptr, AGSwiftMetadata const*, AG::ClosureFunction, bool } OGAttribute OGGraphGetIndirectDependency(OGAttribute attribute) { diff --git a/Sources/_OpenGraph/Attribute/OGAttribute.h b/Sources/_OpenGraph/Attribute/OGAttribute.h index 6c8a95c2..8597f7f3 100644 --- a/Sources/_OpenGraph/Attribute/OGAttribute.h +++ b/Sources/_OpenGraph/Attribute/OGAttribute.h @@ -64,7 +64,11 @@ const OGAttributeInfo OGGraphGetAttributeInfo(OGAttribute attribute) OG_SWIFT_NA OG_EXPORT OG_REFINED_FOR_SWIFT -void OGGraphMutateAttribute(OGAttribute attribute, const OGTypeID type, bool invalidating/*, closure*/); +void OGGraphMutateAttribute(OGAttribute attribute, + const OGTypeID type, + bool invalidating, + const void (*function)(const void * _Nullable context OG_SWIFT_CONTEXT, void *body) OG_SWIFT_CC(swift), + const void * _Nullable context); OG_EXPORT OG_REFINED_FOR_SWIFT diff --git a/Tests/OpenGraphCompatibilityTests/Attribute/Attribute/AttributeTests.swift b/Tests/OpenGraphCompatibilityTests/Attribute/Attribute/AttributeTests.swift index dd1760ec..bcaa47ed 100644 --- a/Tests/OpenGraphCompatibilityTests/Attribute/Attribute/AttributeTests.swift +++ b/Tests/OpenGraphCompatibilityTests/Attribute/Attribute/AttributeTests.swift @@ -65,5 +65,13 @@ final class AttributeTests: AttributeTestBase { attribute.prefetchValue() attribute.invalidateValue() } + + @Test + func mutateBodyAPI() { + let attribute = Attribute(value: 5) + attribute.mutateBody(as: External.self, invalidating: true) { _ in + + } + } } #endif