diff --git a/AG/AttributeGraph.xcframework/ios-arm64-arm64e/AttributeGraph.framework/Headers/AGSubgraph.h b/AG/AttributeGraph.xcframework/ios-arm64-arm64e/AttributeGraph.framework/Headers/AGSubgraph.h index 9f05665a..1f0edeed 100644 --- a/AG/AttributeGraph.xcframework/ios-arm64-arm64e/AttributeGraph.framework/Headers/AGSubgraph.h +++ b/AG/AttributeGraph.xcframework/ios-arm64-arm64e/AttributeGraph.framework/Headers/AGSubgraph.h @@ -82,6 +82,26 @@ AGUniqueID AGSubgraphAddObserver(AGSubgraphRef cf_subgraph, const void (*function)(const void * _Nullable context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), const void * _Nullable context); +AG_EXPORT +AG_REFINED_FOR_SWIFT +bool AGSubgraphShouldRecordTree(void) AG_SWIFT_NAME(getter:AGSubgraphRef.shouldRecordTree()); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +void AGSubgraphSetShouldRecordTree(void) AG_SWIFT_NAME(AGSubgraphRef.setShouldRecordTree()); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +void AGSubgraphBeginTreeElement(AGAttribute attribute, AGTypeID type, uint32_t flags); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +void AGSubgraphAddTreeValue(AGAttribute attribute, AGTypeID type, const char * key, uint32_t flags); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +void AGSubgraphEndTreeElement(AGAttribute attribute); + AG_EXTERN_C_END AG_ASSUME_NONNULL_END diff --git a/AG/AttributeGraph.xcframework/ios-arm64-arm64e/AttributeGraph.framework/Modules/AttributeGraph.swiftmodule/arm64-apple-ios.swiftinterface b/AG/AttributeGraph.xcframework/ios-arm64-arm64e/AttributeGraph.framework/Modules/AttributeGraph.swiftmodule/arm64-apple-ios.swiftinterface index 1b710b93..4e2bbb77 100644 --- a/AG/AttributeGraph.xcframework/ios-arm64-arm64e/AttributeGraph.framework/Modules/AttributeGraph.swiftmodule/arm64-apple-ios.swiftinterface +++ b/AG/AttributeGraph.xcframework/ios-arm64-arm64e/AttributeGraph.framework/Modules/AttributeGraph.swiftmodule/arm64-apple-ios.swiftinterface @@ -403,6 +403,12 @@ extension AttributeGraph.AGSubgraph { public func forEach(_ flags: AGAttributeFlags, _ callback: (AGAttribute) -> Void) } +extension AttributeGraph.AGSubgraph { + public static func beginTreeElement(value: AttributeGraph.Attribute, flags: Swift.UInt32) + public static func endTreeElement(value: AttributeGraph.Attribute) + public static func addTreeValue(_ value: AttributeGraph.Attribute, forKey key: Swift.UnsafePointer, flags: Swift.UInt32) +} + // MARK: - Rule Related // MARK: - Rule diff --git a/AG/AttributeGraph.xcframework/ios-arm64-arm64e/AttributeGraph.framework/Modules/AttributeGraph.swiftmodule/arm64e-apple-ios.swiftinterface b/AG/AttributeGraph.xcframework/ios-arm64-arm64e/AttributeGraph.framework/Modules/AttributeGraph.swiftmodule/arm64e-apple-ios.swiftinterface index 1b710b93..4e2bbb77 100644 --- a/AG/AttributeGraph.xcframework/ios-arm64-arm64e/AttributeGraph.framework/Modules/AttributeGraph.swiftmodule/arm64e-apple-ios.swiftinterface +++ b/AG/AttributeGraph.xcframework/ios-arm64-arm64e/AttributeGraph.framework/Modules/AttributeGraph.swiftmodule/arm64e-apple-ios.swiftinterface @@ -403,6 +403,12 @@ extension AttributeGraph.AGSubgraph { public func forEach(_ flags: AGAttributeFlags, _ callback: (AGAttribute) -> Void) } +extension AttributeGraph.AGSubgraph { + public static func beginTreeElement(value: AttributeGraph.Attribute, flags: Swift.UInt32) + public static func endTreeElement(value: AttributeGraph.Attribute) + public static func addTreeValue(_ value: AttributeGraph.Attribute, forKey key: Swift.UnsafePointer, flags: Swift.UInt32) +} + // MARK: - Rule Related // MARK: - Rule diff --git a/AG/AttributeGraph.xcframework/ios-arm64-x86_64-simulator/AttributeGraph.framework/Headers/AGSubgraph.h b/AG/AttributeGraph.xcframework/ios-arm64-x86_64-simulator/AttributeGraph.framework/Headers/AGSubgraph.h index 9f05665a..1f0edeed 100644 --- a/AG/AttributeGraph.xcframework/ios-arm64-x86_64-simulator/AttributeGraph.framework/Headers/AGSubgraph.h +++ b/AG/AttributeGraph.xcframework/ios-arm64-x86_64-simulator/AttributeGraph.framework/Headers/AGSubgraph.h @@ -82,6 +82,26 @@ AGUniqueID AGSubgraphAddObserver(AGSubgraphRef cf_subgraph, const void (*function)(const void * _Nullable context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), const void * _Nullable context); +AG_EXPORT +AG_REFINED_FOR_SWIFT +bool AGSubgraphShouldRecordTree(void) AG_SWIFT_NAME(getter:AGSubgraphRef.shouldRecordTree()); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +void AGSubgraphSetShouldRecordTree(void) AG_SWIFT_NAME(AGSubgraphRef.setShouldRecordTree()); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +void AGSubgraphBeginTreeElement(AGAttribute attribute, AGTypeID type, uint32_t flags); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +void AGSubgraphAddTreeValue(AGAttribute attribute, AGTypeID type, const char * key, uint32_t flags); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +void AGSubgraphEndTreeElement(AGAttribute attribute); + AG_EXTERN_C_END AG_ASSUME_NONNULL_END diff --git a/AG/AttributeGraph.xcframework/ios-arm64-x86_64-simulator/AttributeGraph.framework/Modules/AttributeGraph.swiftmodule/arm64-apple-ios-simulator.swiftinterface b/AG/AttributeGraph.xcframework/ios-arm64-x86_64-simulator/AttributeGraph.framework/Modules/AttributeGraph.swiftmodule/arm64-apple-ios-simulator.swiftinterface index 07a413fd..aa6a7c6a 100644 --- a/AG/AttributeGraph.xcframework/ios-arm64-x86_64-simulator/AttributeGraph.framework/Modules/AttributeGraph.swiftmodule/arm64-apple-ios-simulator.swiftinterface +++ b/AG/AttributeGraph.xcframework/ios-arm64-x86_64-simulator/AttributeGraph.framework/Modules/AttributeGraph.swiftmodule/arm64-apple-ios-simulator.swiftinterface @@ -403,6 +403,12 @@ extension AttributeGraph.AGSubgraph { public func forEach(_ flags: AGAttributeFlags, _ callback: (AGAttribute) -> Void) } +extension AttributeGraph.AGSubgraph { + public static func beginTreeElement(value: AttributeGraph.Attribute, flags: Swift.UInt32) + public static func endTreeElement(value: AttributeGraph.Attribute) + public static func addTreeValue(_ value: AttributeGraph.Attribute, forKey key: Swift.UnsafePointer, flags: Swift.UInt32) +} + // MARK: - Rule Related // MARK: - Rule diff --git a/AG/AttributeGraph.xcframework/ios-arm64-x86_64-simulator/AttributeGraph.framework/Modules/AttributeGraph.swiftmodule/x86_64-apple-ios-simulator.swiftinterface b/AG/AttributeGraph.xcframework/ios-arm64-x86_64-simulator/AttributeGraph.framework/Modules/AttributeGraph.swiftmodule/x86_64-apple-ios-simulator.swiftinterface index e07ef058..901e19a4 100644 --- a/AG/AttributeGraph.xcframework/ios-arm64-x86_64-simulator/AttributeGraph.framework/Modules/AttributeGraph.swiftmodule/x86_64-apple-ios-simulator.swiftinterface +++ b/AG/AttributeGraph.xcframework/ios-arm64-x86_64-simulator/AttributeGraph.framework/Modules/AttributeGraph.swiftmodule/x86_64-apple-ios-simulator.swiftinterface @@ -403,6 +403,12 @@ extension AttributeGraph.AGSubgraph { public func forEach(_ flags: AGAttributeFlags, _ callback: (AGAttribute) -> Void) } +extension AttributeGraph.AGSubgraph { + public static func beginTreeElement(value: AttributeGraph.Attribute, flags: Swift.UInt32) + public static func endTreeElement(value: AttributeGraph.Attribute) + public static func addTreeValue(_ value: AttributeGraph.Attribute, forKey key: Swift.UnsafePointer, flags: Swift.UInt32) +} + // MARK: - Rule Related // MARK: - Rule diff --git a/AG/AttributeGraph.xcframework/macos-arm64e-arm64-x86_64/AttributeGraph.framework/Headers/AGSubgraph.h b/AG/AttributeGraph.xcframework/macos-arm64e-arm64-x86_64/AttributeGraph.framework/Headers/AGSubgraph.h index 9f05665a..1f0edeed 100644 --- a/AG/AttributeGraph.xcframework/macos-arm64e-arm64-x86_64/AttributeGraph.framework/Headers/AGSubgraph.h +++ b/AG/AttributeGraph.xcframework/macos-arm64e-arm64-x86_64/AttributeGraph.framework/Headers/AGSubgraph.h @@ -82,6 +82,26 @@ AGUniqueID AGSubgraphAddObserver(AGSubgraphRef cf_subgraph, const void (*function)(const void * _Nullable context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), const void * _Nullable context); +AG_EXPORT +AG_REFINED_FOR_SWIFT +bool AGSubgraphShouldRecordTree(void) AG_SWIFT_NAME(getter:AGSubgraphRef.shouldRecordTree()); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +void AGSubgraphSetShouldRecordTree(void) AG_SWIFT_NAME(AGSubgraphRef.setShouldRecordTree()); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +void AGSubgraphBeginTreeElement(AGAttribute attribute, AGTypeID type, uint32_t flags); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +void AGSubgraphAddTreeValue(AGAttribute attribute, AGTypeID type, const char * key, uint32_t flags); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +void AGSubgraphEndTreeElement(AGAttribute attribute); + AG_EXTERN_C_END AG_ASSUME_NONNULL_END diff --git a/AG/AttributeGraph.xcframework/macos-arm64e-arm64-x86_64/AttributeGraph.framework/Modules/AttributeGraph.swiftmodule/arm64-apple-macos.swiftinterface b/AG/AttributeGraph.xcframework/macos-arm64e-arm64-x86_64/AttributeGraph.framework/Modules/AttributeGraph.swiftmodule/arm64-apple-macos.swiftinterface index 340900cc..8918fcc3 100644 --- a/AG/AttributeGraph.xcframework/macos-arm64e-arm64-x86_64/AttributeGraph.framework/Modules/AttributeGraph.swiftmodule/arm64-apple-macos.swiftinterface +++ b/AG/AttributeGraph.xcframework/macos-arm64e-arm64-x86_64/AttributeGraph.framework/Modules/AttributeGraph.swiftmodule/arm64-apple-macos.swiftinterface @@ -403,6 +403,12 @@ extension AttributeGraph.AGSubgraph { public func forEach(_ flags: AGAttributeFlags, _ callback: (AGAttribute) -> Void) } +extension AttributeGraph.AGSubgraph { + public static func beginTreeElement(value: AttributeGraph.Attribute, flags: Swift.UInt32) + public static func endTreeElement(value: AttributeGraph.Attribute) + public static func addTreeValue(_ value: AttributeGraph.Attribute, forKey key: Swift.UnsafePointer, flags: Swift.UInt32) +} + // MARK: - Rule Related // MARK: - Rule diff --git a/AG/AttributeGraph.xcframework/macos-arm64e-arm64-x86_64/AttributeGraph.framework/Modules/AttributeGraph.swiftmodule/arm64e-apple-macos.swiftinterface b/AG/AttributeGraph.xcframework/macos-arm64e-arm64-x86_64/AttributeGraph.framework/Modules/AttributeGraph.swiftmodule/arm64e-apple-macos.swiftinterface index 198a1220..837c3577 100644 --- a/AG/AttributeGraph.xcframework/macos-arm64e-arm64-x86_64/AttributeGraph.framework/Modules/AttributeGraph.swiftmodule/arm64e-apple-macos.swiftinterface +++ b/AG/AttributeGraph.xcframework/macos-arm64e-arm64-x86_64/AttributeGraph.framework/Modules/AttributeGraph.swiftmodule/arm64e-apple-macos.swiftinterface @@ -403,6 +403,12 @@ extension AttributeGraph.AGSubgraph { public func forEach(_ flags: AGAttributeFlags, _ callback: (AGAttribute) -> Void) } +extension AttributeGraph.AGSubgraph { + public static func beginTreeElement(value: AttributeGraph.Attribute, flags: Swift.UInt32) + public static func endTreeElement(value: AttributeGraph.Attribute) + public static func addTreeValue(_ value: AttributeGraph.Attribute, forKey key: Swift.UnsafePointer, flags: Swift.UInt32) +} + // MARK: - Rule Related // MARK: - Rule diff --git a/AG/AttributeGraph.xcframework/macos-arm64e-arm64-x86_64/AttributeGraph.framework/Modules/AttributeGraph.swiftmodule/x86_64-apple-macos.swiftinterface b/AG/AttributeGraph.xcframework/macos-arm64e-arm64-x86_64/AttributeGraph.framework/Modules/AttributeGraph.swiftmodule/x86_64-apple-macos.swiftinterface index 71edf61a..cd5ece6d 100644 --- a/AG/AttributeGraph.xcframework/macos-arm64e-arm64-x86_64/AttributeGraph.framework/Modules/AttributeGraph.swiftmodule/x86_64-apple-macos.swiftinterface +++ b/AG/AttributeGraph.xcframework/macos-arm64e-arm64-x86_64/AttributeGraph.framework/Modules/AttributeGraph.swiftmodule/x86_64-apple-macos.swiftinterface @@ -403,6 +403,12 @@ extension AttributeGraph.AGSubgraph { public func forEach(_ flags: AGAttributeFlags, _ callback: (AGAttribute) -> Void) } +extension AttributeGraph.AGSubgraph { + public static func beginTreeElement(value: AttributeGraph.Attribute, flags: Swift.UInt32) + public static func endTreeElement(value: AttributeGraph.Attribute) + public static func addTreeValue(_ value: AttributeGraph.Attribute, forKey key: Swift.UnsafePointer, flags: Swift.UInt32) +} + // MARK: - Rule Related // MARK: - Rule diff --git a/AG/Sources/Headers/AGSubgraph.h b/AG/Sources/Headers/AGSubgraph.h index 9f05665a..1f0edeed 100644 --- a/AG/Sources/Headers/AGSubgraph.h +++ b/AG/Sources/Headers/AGSubgraph.h @@ -82,6 +82,26 @@ AGUniqueID AGSubgraphAddObserver(AGSubgraphRef cf_subgraph, const void (*function)(const void * _Nullable context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), const void * _Nullable context); +AG_EXPORT +AG_REFINED_FOR_SWIFT +bool AGSubgraphShouldRecordTree(void) AG_SWIFT_NAME(getter:AGSubgraphRef.shouldRecordTree()); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +void AGSubgraphSetShouldRecordTree(void) AG_SWIFT_NAME(AGSubgraphRef.setShouldRecordTree()); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +void AGSubgraphBeginTreeElement(AGAttribute attribute, AGTypeID type, uint32_t flags); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +void AGSubgraphAddTreeValue(AGAttribute attribute, AGTypeID type, const char * key, uint32_t flags); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +void AGSubgraphEndTreeElement(AGAttribute attribute); + AG_EXTERN_C_END AG_ASSUME_NONNULL_END diff --git a/AG/Sources/Modules/AttributeGraph.swiftmodule/template.swiftinterface b/AG/Sources/Modules/AttributeGraph.swiftmodule/template.swiftinterface index 3deeafab..19d6370f 100644 --- a/AG/Sources/Modules/AttributeGraph.swiftmodule/template.swiftinterface +++ b/AG/Sources/Modules/AttributeGraph.swiftmodule/template.swiftinterface @@ -399,6 +399,12 @@ extension AttributeGraph.AGSubgraph { public func forEach(_ flags: AGAttributeFlags, _ callback: (AGAttribute) -> Void) } +extension AttributeGraph.AGSubgraph { + public static func beginTreeElement(value: AttributeGraph.Attribute, flags: Swift.UInt32) + public static func endTreeElement(value: AttributeGraph.Attribute) + public static func addTreeValue(_ value: AttributeGraph.Attribute, forKey key: Swift.UnsafePointer, flags: Swift.UInt32) +} + // MARK: - Rule Related // MARK: - Rule diff --git a/Sources/OpenGraph/Graph/OGSubgraph.swift b/Sources/OpenGraph/Graph/OGSubgraph.swift index db970107..36deccdb 100644 --- a/Sources/OpenGraph/Graph/OGSubgraph.swift +++ b/Sources/OpenGraph/Graph/OGSubgraph.swift @@ -30,17 +30,25 @@ extension OGSubgraph { public func forEach(_ flags: OGAttributeFlags, _ callback: (OGAttribute) -> Void) { OGSubgraph.apply(self, flags: flags, callback: callback) } - - /*public*/ static func beginTreeElement(value _: Attribute, flags _: UInt32) { - fatalError("TODO") +} + +extension OGSubgraph { + public static func beginTreeElement(value: Attribute, flags: UInt32) { + if shouldRecordTree { + __OGSubgraphBeginTreeElement(value.identifier, OGTypeID(Value.self), flags) + } } - /*public*/ static func addTreeValue(_: Attribute, forKey _: UnsafePointer, flags _: UInt32) { - fatalError("TODO") + public static func addTreeValue(_ value: Attribute, forKey key: UnsafePointer, flags: UInt32) { + if shouldRecordTree { + __OGSubgraphAddTreeValue(value.identifier, OGTypeID(Value.self), key, flags) + } } - /*public*/ static func endTreeElement(value _: Attribute) { - fatalError("TODO") + public static func endTreeElement(value: Attribute) { + if shouldRecordTree { + __OGSubgraphEndTreeElement(value.identifier) + } } } diff --git a/Sources/_OpenGraph/Graph/OGSubgraph.cpp b/Sources/_OpenGraph/Graph/OGSubgraph.cpp index c2f57a8f..19cf5e78 100644 --- a/Sources/_OpenGraph/Graph/OGSubgraph.cpp +++ b/Sources/_OpenGraph/Graph/OGSubgraph.cpp @@ -8,9 +8,13 @@ #include "OGSubgraph.h" #include "OGGraph.h" #include "Subgraph.hpp" +#include "OGGraphContext.h" #include "../Util/assert.hpp" +#include "../Util/env.hpp" #include -#include "OGGraphContext.h" +#if !OG_TARGET_OS_WASI +#include +#endif namespace { CFRuntimeClass &subgraph_type_id() { @@ -68,7 +72,7 @@ OGSubgraphRef OGSubgraphCreate2(OGGraphRef cf_graph, OGAttribute attribute) { } _Nullable OGSubgraphRef OGSubgraphGetCurrent() { - OG::Subgraph* subgraph = (OG::Subgraph*)pthread_getspecific(OG::Subgraph::current_key()); + OG::Subgraph *subgraph = OG::Subgraph::get_current(); if (subgraph == nullptr) { return nullptr; } @@ -76,25 +80,26 @@ _Nullable OGSubgraphRef OGSubgraphGetCurrent() { } void OGSubgraphSetCurrent(_Nullable OGSubgraphRef cf_subgraph) { - OG::Subgraph* oldSubgraph = (OG::Subgraph*)pthread_getspecific(OG::Subgraph::current_key()); + OG::Subgraph *old_subgraph = OG::Subgraph::get_current(); if (cf_subgraph == nullptr) { - pthread_setspecific(OG::Subgraph::current_key(), nullptr); + OG::Subgraph::set_current(nullptr); } else { - pthread_setspecific(OG::Subgraph::current_key(), cf_subgraph->subgraph); - if (cf_subgraph->subgraph != nullptr) { + OG::Subgraph *subgraph = cf_subgraph->subgraph; + OG::Subgraph::set_current(subgraph); + if (subgraph != nullptr) { CFRetain(cf_subgraph); } } - if (oldSubgraph != nullptr) { - OGSubgraphRef cf_oldSubgraph = oldSubgraph->to_cf(); - if (cf_oldSubgraph) { - CFRelease(cf_oldSubgraph); + if (old_subgraph != nullptr) { + OGSubgraphRef old_cf_Subgraph = old_subgraph->to_cf(); + if (old_cf_Subgraph) { + CFRelease(old_cf_Subgraph); } } } OGGraphContextRef OGSubgraphGetCurrentGraphContext() { - OG::Subgraph* subgraph = (OG::Subgraph*)pthread_getspecific(OG::Subgraph::current_key()); + OG::Subgraph *subgraph = OG::Subgraph::get_current(); if (subgraph == nullptr) { return nullptr; } @@ -162,3 +167,49 @@ OGUniqueID OGSubgraphAddObserver(OGSubgraphRef cf_subgraph, } return subgraph->add_observer(OG::ClosureFunction(function, context)); } + +#if !OG_TARGET_OS_WASI +static bool should_record_tree; +static dispatch_once_t should_record_tree_once; + +void init_should_record_tree(void *) { + should_record_tree = OG::get_env("OG_TREE") != 0; +} +#endif + +bool OGSubgraphShouldRecordTree() { + #if !OG_TARGET_OS_WASI + dispatch_once_f(&should_record_tree_once, NULL, init_should_record_tree); + return should_record_tree; + #else + return false; + #endif +} + +void OGSubgraphSetShouldRecordTree() { + #if !OG_TARGET_OS_WASI + dispatch_once_f(&should_record_tree_once, NULL, init_should_record_tree); + should_record_tree = true; + #endif +} + +void OGSubgraphBeginTreeElement(OGAttribute attribute, OGTypeID type, uint32_t flags) { + OG::Subgraph * subgraph = OG::Subgraph::get_current(); + if (subgraph) { + subgraph->begin_tree(attribute, type, flags); + } +} + +void OGSubgraphAddTreeValue(OGAttribute attribute, OGTypeID type, const char * key, uint32_t flags) { + OG::Subgraph * subgraph = OG::Subgraph::get_current(); + if (subgraph) { + subgraph->add_tree_value(attribute, type, key, flags); + } +} + +void OGSubgraphEndTreeElement(OGAttribute attribute) { + OG::Subgraph * subgraph = OG::Subgraph::get_current(); + if (subgraph) { + subgraph->end_tree(attribute); + } +} diff --git a/Sources/_OpenGraph/Graph/OGSubgraph.h b/Sources/_OpenGraph/Graph/OGSubgraph.h index 3cd2ddd4..73e95d9c 100644 --- a/Sources/_OpenGraph/Graph/OGSubgraph.h +++ b/Sources/_OpenGraph/Graph/OGSubgraph.h @@ -82,6 +82,26 @@ OGUniqueID OGSubgraphAddObserver(OGSubgraphRef cf_subgraph, const void (*function)(const void * _Nullable context OG_SWIFT_CONTEXT) OG_SWIFT_CC(swift), const void * _Nullable context); +OG_EXPORT +OG_REFINED_FOR_SWIFT +bool OGSubgraphShouldRecordTree(void) OG_SWIFT_NAME(getter:OGSubgraphRef.shouldRecordTree()); + +OG_EXPORT +OG_REFINED_FOR_SWIFT +void OGSubgraphSetShouldRecordTree(void) OG_SWIFT_NAME(OGSubgraphRef.setShouldRecordTree()); + +OG_EXPORT +OG_REFINED_FOR_SWIFT +void OGSubgraphBeginTreeElement(OGAttribute attribute, OGTypeID type, uint32_t flags); + +OG_EXPORT +OG_REFINED_FOR_SWIFT +void OGSubgraphAddTreeValue(OGAttribute attribute, OGTypeID type, const char * key, uint32_t flags); + +OG_EXPORT +OG_REFINED_FOR_SWIFT +void OGSubgraphEndTreeElement(OGAttribute attribute); + OG_EXTERN_C_END OG_ASSUME_NONNULL_END diff --git a/Sources/_OpenGraph/Graph/Subgraph.cpp b/Sources/_OpenGraph/Graph/Subgraph.cpp index aa142b84..43588802 100644 --- a/Sources/_OpenGraph/Graph/Subgraph.cpp +++ b/Sources/_OpenGraph/Graph/Subgraph.cpp @@ -26,7 +26,8 @@ void OG::Subgraph::apply(OGAttributeFlags flags, OG::ClosureFunction observer) const // TODO return OGMakeUniqueID(); } + +void OG::Subgraph::begin_tree(OG::AttributeID id, OG::swift::metadata const* type, unsigned int flags) const OG_NOEXCEPT { + // TODO +} + +void OG::Subgraph::add_tree_value(OG::AttributeID id, OG::swift::metadata const *type, const char* key, uint32_t flags) const OG_NOEXCEPT { + // TODO +} + +void OG::Subgraph::end_tree(OG::AttributeID id) const OG_NOEXCEPT { + // TODO +} diff --git a/Sources/_OpenGraph/Graph/Subgraph.hpp b/Sources/_OpenGraph/Graph/Subgraph.hpp index 129ba21a..9fa62e5e 100644 --- a/Sources/_OpenGraph/Graph/Subgraph.hpp +++ b/Sources/_OpenGraph/Graph/Subgraph.hpp @@ -9,10 +9,11 @@ #define Subgraph_hpp #include "OGBase.h" -#include "../Attribute/AttributeID.hpp" #include "Graph.hpp" -#include +#include "../Attribute/AttributeID.hpp" #include "../Data/ClosureFunction.hpp" +#include "../Runtime/metadata.hpp" +#include typedef struct OG_BRIDGED_TYPE(id) OGSubgraphStorage * OGSubgraphRef; @@ -37,7 +38,8 @@ class Subgraph final { // MARK: - pthread related - static void make_current_subgraph_key() { + OG_INLINE + const static void make_current_subgraph_key() OG_NOEXCEPT { pthread_key_create(&_current_subgraph_key, nullptr); } @@ -46,6 +48,16 @@ class Subgraph final { return _current_subgraph_key; } + OG_INLINE OG_CONSTEXPR + static Subgraph *get_current() OG_NOEXCEPT { + return (OG::Subgraph*)pthread_getspecific(OG::Subgraph::current_key()); + } + + OG_INLINE OG_CONSTEXPR + static int set_current(Subgraph *subgraph) OG_NOEXCEPT { + return pthread_setspecific(OG::Subgraph::current_key(), subgraph); + } + // MARK: - Public API void clear_object() const OG_NOEXCEPT; void invalidate_and_delete_(bool) const OG_NOEXCEPT; @@ -54,6 +66,10 @@ class Subgraph final { OGUniqueID add_observer(OG::ClosureFunction observer) const OG_NOEXCEPT; + void begin_tree(OG::AttributeID id, OG::swift::metadata const *type, uint32_t flags) const OG_NOEXCEPT; + void add_tree_value(OG::AttributeID id, OG::swift::metadata const *type, const char* key, uint32_t flags) const OG_NOEXCEPT; + void end_tree(OG::AttributeID id) const OG_NOEXCEPT; + // MARK: - Init and deinit Subgraph(SubgraphObject*, OG::Graph::Context&, OG::AttributeID); diff --git a/Sources/_OpenGraph/Runtime/metadata.cpp b/Sources/_OpenGraph/Runtime/metadata.cpp new file mode 100644 index 00000000..833834e2 --- /dev/null +++ b/Sources/_OpenGraph/Runtime/metadata.cpp @@ -0,0 +1,8 @@ +// +// metadata.cpp +// +// +// Created by Kyle on 2024/4/4. +// + +#include "metadata.hpp" diff --git a/Sources/_OpenGraph/Runtime/metadata.hpp b/Sources/_OpenGraph/Runtime/metadata.hpp new file mode 100644 index 00000000..03637f85 --- /dev/null +++ b/Sources/_OpenGraph/Runtime/metadata.hpp @@ -0,0 +1,19 @@ +// +// metadata.hpp +// +// +// Created by Kyle on 2024/4/4. +// + +#ifndef metadata_hpp +#define metadata_hpp + +#include "OGBase.h" +#include "OGSwiftMetadata.h" + +namespace OG { +namespace swift { +using metadata = OGSwiftMetadata; // FIXME: swift::Metadata +} +} +#endif /* metadata_hpp */ diff --git a/Sources/_OpenGraph/Util/env.hpp b/Sources/_OpenGraph/Util/env.hpp new file mode 100644 index 00000000..c7eb5dab --- /dev/null +++ b/Sources/_OpenGraph/Util/env.hpp @@ -0,0 +1,25 @@ +// +// env.hpp +// +// +// Created by Kyle on 2024/4/4. +// + +#ifndef env_hpp +#define env_hpp + +#include "OGBase.h" + +namespace OG { +OG_INLINE +int get_env(const char *name) { + char *value = getenv(name); + if (value) { + return atoi(value); + } else { + return 0; + } +} +} + +#endif /* env_hpp */ diff --git a/Tests/OpenGraphCompatibilityTests/Graph/SubgraphTests.swift b/Tests/OpenGraphCompatibilityTests/Graph/SubgraphTests.swift new file mode 100644 index 00000000..8d4466f9 --- /dev/null +++ b/Tests/OpenGraphCompatibilityTests/Graph/SubgraphTests.swift @@ -0,0 +1,29 @@ +// +// SubgraphTests.swift +// OpenGraphCompatibilityTests + +import Testing + +struct SubgraphTests { + @Test + func shouldRecordTree() { + let key = compatibilityTestEnabled ? "AG_TREE" : "OG_TREE" + setenv(key, "0", 1) + #expect(OGSubgraph.shouldRecordTree == false) + + OGSubgraph.setShouldRecordTree() + #expect(OGSubgraph.shouldRecordTree == true) + } + + @Test + func treeElementAPICheck() { + let graph = OGGraph() + let subgraph = OGSubgraph(graph: graph) + subgraph.apply { + let value = Attribute(value: ()) + OGSubgraph.beginTreeElement(value: value, flags: 0) + OGSubgraph.addTreeValue(value, forKey: "", flags: 0) + OGSubgraph.endTreeElement(value: value) + } + } +} diff --git a/Tests/OpenGraphCompatibilityTests/Scaffolding.swift b/Tests/OpenGraphCompatibilityTests/Scaffolding.swift index 33aadda4..f3269527 100644 --- a/Tests/OpenGraphCompatibilityTests/Scaffolding.swift +++ b/Tests/OpenGraphCompatibilityTests/Scaffolding.swift @@ -1,8 +1,6 @@ // // Scaffolding.swift -// -// -// +// OpenGraphCompatibilityTests import Testing import XCTest diff --git a/Tests/OpenGraphTempTests/Scaffolding.swift b/Tests/OpenGraphTempTests/Scaffolding.swift index 33aadda4..326d2490 100644 --- a/Tests/OpenGraphTempTests/Scaffolding.swift +++ b/Tests/OpenGraphTempTests/Scaffolding.swift @@ -1,14 +1,14 @@ // // Scaffolding.swift -// -// -// +// OpenGraphTempTests + import Testing import XCTest -final class AllTests: XCTestCase { - func testAll() async { - await XCTestScaffold.runAllTests(hostedBy: self) - } -} +// See https://github.com/apple/swift-testing/issues/329 +//final class AllTests: XCTestCase { +// func testAll() async { +// await XCTestScaffold.runAllTests(hostedBy: self) +// } +//} diff --git a/Tests/OpenGraphTests/DummyTests.swift b/Tests/OpenGraphTests/DummyTests.swift index a426def7..0f015532 100644 --- a/Tests/OpenGraphTests/DummyTests.swift +++ b/Tests/OpenGraphTests/DummyTests.swift @@ -1,8 +1,6 @@ // // DummyTests.swift -// -// -// +// OpenGraphTests @testable import OpenGraph import Testing diff --git a/Tests/OpenGraphTests/Scaffolding.swift b/Tests/OpenGraphTests/Scaffolding.swift index 33aadda4..eb8a98f7 100644 --- a/Tests/OpenGraphTests/Scaffolding.swift +++ b/Tests/OpenGraphTests/Scaffolding.swift @@ -1,14 +1,13 @@ // // Scaffolding.swift -// -// -// +// OpenGraphTests import Testing import XCTest -final class AllTests: XCTestCase { - func testAll() async { - await XCTestScaffold.runAllTests(hostedBy: self) - } -} +// See https://github.com/apple/swift-testing/issues/329 +//final class AllTests: XCTestCase { +// func testAll() async { +// await XCTestScaffold.runAllTests(hostedBy: self) +// } +//}