From f6113317d06f9ef479dade2fc515e07187f2052e Mon Sep 17 00:00:00 2001 From: James Moschou Date: Sun, 24 Aug 2025 15:22:40 +0200 Subject: [PATCH 1/4] Update implementation of OAGUnownedGraphContextRef # Conflicts: # Sources/OpenGraphCxx/Graph/OGGraph.cpp # Sources/OpenGraphCxx/Graph/OGGraphContext.cpp # Sources/OpenGraphCxx/Graph/OGSubgraph.cpp # Sources/OpenGraphCxx/Graph/Subgraph.cpp # Sources/OpenGraphCxx/include/OpenGraph/OGGraph.h # Sources/OpenGraphCxx/include/OpenGraph/OGGraphContext.h # Sources/OpenGraphCxx/include/OpenGraph/OGSubgraph.h # Sources/OpenGraphCxx/include/OpenGraphCxx/Graph/Graph.hpp # Sources/OpenGraphCxx/include/OpenGraphCxx/Graph/Subgraph.hpp --- Sources/OpenAttributeGraph/Graph/Graph.swift | 2 +- .../OpenAttributeGraphCxx/Graph/OAGGraph.cpp | 4 ++-- .../Graph/OAGGraphContext.cpp | 2 +- .../Graph/OAGSubgraph.cpp | 8 +++++--- .../OpenAttributeGraphCxx/Graph/Subgraph.cpp | 5 ++--- .../include/OpenAttributeGraph/OAGGraph.h | 6 +++--- .../OpenAttributeGraph/OAGGraphContext.h | 2 +- .../include/OpenAttributeGraph/OAGSubgraph.h | 2 +- .../OpenAttributeGraphCxx/Graph/Graph.hpp | 7 ++++++- .../OpenAttributeGraphCxx/Graph/Subgraph.hpp | 19 ++++++++++--------- 10 files changed, 32 insertions(+), 25 deletions(-) diff --git a/Sources/OpenAttributeGraph/Graph/Graph.swift b/Sources/OpenAttributeGraph/Graph/Graph.swift index 796f211f..1f047be9 100644 --- a/Sources/OpenAttributeGraph/Graph/Graph.swift +++ b/Sources/OpenAttributeGraph/Graph/Graph.swift @@ -9,7 +9,7 @@ public import OpenAttributeGraphCxx extension Graph { public static func typeIndex( - ctx: GraphContext, + ctx: UnownedGraphContext, body: _AttributeBody.Type, valueType: Metadata, flags: _AttributeType.Flags, diff --git a/Sources/OpenAttributeGraphCxx/Graph/OAGGraph.cpp b/Sources/OpenAttributeGraphCxx/Graph/OAGGraph.cpp index b7c4f661..dda1ac31 100644 --- a/Sources/OpenAttributeGraphCxx/Graph/OAGGraph.cpp +++ b/Sources/OpenAttributeGraphCxx/Graph/OAGGraph.cpp @@ -110,11 +110,11 @@ void OAGGraphSetContext(OAGGraphRef graph, const void * _Nullable context) { graph->context.set_context(context); } -OAGGraphContextRef OAGGraphGetGraphContext(OAGGraphRef graph) { +OAGUnownedGraphContextRef OAGGraphGetGraphContext(OAGGraphRef graph) { if (graph->context.isInvalid()) { OAG::precondition_failure("invalidated graph"); } - return reinterpret_cast(reinterpret_cast(graph) + sizeof(CFRuntimeBase)); + return reinterpret_cast(&graph->context.get_graph()); } void OAGGraphInvalidate(OAGGraphRef graph) { diff --git a/Sources/OpenAttributeGraphCxx/Graph/OAGGraphContext.cpp b/Sources/OpenAttributeGraphCxx/Graph/OAGGraphContext.cpp index 02cc8e11..e5b570f5 100644 --- a/Sources/OpenAttributeGraphCxx/Graph/OAGGraphContext.cpp +++ b/Sources/OpenAttributeGraphCxx/Graph/OAGGraphContext.cpp @@ -5,6 +5,6 @@ #include #include -OAGGraphRef OAGGraphContextGetGraph(OAGGraphContextRef context) { +OAGGraphRef OAGGraphContextGetGraph(void *context) { return reinterpret_cast(reinterpret_cast(context) - sizeof(CFRuntimeBase)); } diff --git a/Sources/OpenAttributeGraphCxx/Graph/OAGSubgraph.cpp b/Sources/OpenAttributeGraphCxx/Graph/OAGSubgraph.cpp index 4b24503e..6874ff0e 100644 --- a/Sources/OpenAttributeGraphCxx/Graph/OAGSubgraph.cpp +++ b/Sources/OpenAttributeGraphCxx/Graph/OAGSubgraph.cpp @@ -95,12 +95,13 @@ void OAGSubgraphSetCurrent(_Nullable OAGSubgraphRef cf_subgraph) { } } -OAGGraphContextRef OAGSubgraphGetCurrentGraphContext() { +OAGUnownedGraphContextRef OAGSubgraphGetCurrentGraphContext() { OAG::Subgraph *subgraph = OAG::Subgraph::get_current(); if (subgraph == nullptr) { return nullptr; } - return subgraph->get_context(); + OAG::Graph &graph = subgraph->get_graph(); + return reinterpret_cast(&graph); } void OAGSubgraphInvalidate(OAGSubgraphRef cf_subgraph) { @@ -121,7 +122,8 @@ OAGGraphRef OAGSubgraphGetGraph(OAGSubgraphRef cf_subgraph) { if (cf_subgraph->subgraph == nullptr) { OAG::precondition_failure("accessing invalidated subgraph"); } - return OAGGraphContextGetGraph(cf_subgraph->subgraph->get_context()); + // TODO + return nullptr; } void OAGSubgraphAddChild(OAGSubgraphRef parent, OAGSubgraphRef child) { diff --git a/Sources/OpenAttributeGraphCxx/Graph/Subgraph.cpp b/Sources/OpenAttributeGraphCxx/Graph/Subgraph.cpp index 4083744c..92b3db62 100644 --- a/Sources/OpenAttributeGraphCxx/Graph/Subgraph.cpp +++ b/Sources/OpenAttributeGraphCxx/Graph/Subgraph.cpp @@ -23,9 +23,8 @@ void OAG::Subgraph::apply(OAGAttributeFlags flags, OAG::ClosureFunction invalidation_callback) OAG_NOEXCEPT { _invalidation_callback = invalidation_callback; @@ -181,7 +186,7 @@ struct OAGGraphStorage { }; struct OAGGraphContextStorage { - OAG::Graph::Context context; + OAG::Graph graph; }; OAG_ASSUME_NONNULL_END diff --git a/Sources/OpenAttributeGraphCxx/include/OpenAttributeGraphCxx/Graph/Subgraph.hpp b/Sources/OpenAttributeGraphCxx/include/OpenAttributeGraphCxx/Graph/Subgraph.hpp index a798d8c0..225976a1 100644 --- a/Sources/OpenAttributeGraphCxx/include/OpenAttributeGraphCxx/Graph/Subgraph.hpp +++ b/Sources/OpenAttributeGraphCxx/include/OpenAttributeGraphCxx/Graph/Subgraph.hpp @@ -19,8 +19,9 @@ class SubgraphObject; class Subgraph final { private: - OAGSubgraphRef _cf_subgraph; - OAGGraphContextStorage& _context; + SubgraphObject *_Nullable _object; + Graph& _graph; + OAGUniqueID _graph_context_id; // TODO bool _isInvalid; static pthread_key_t _current_subgraph_key; @@ -30,7 +31,7 @@ class Subgraph final { static Subgraph *from_cf(OAGSubgraphRef cf_subgraph) OAG_NOEXCEPT; OAGSubgraphRef to_cf() const OAG_NOEXCEPT { - return _cf_subgraph; + return reinterpret_cast(_object); } // MARK: - pthread related @@ -75,13 +76,13 @@ class Subgraph final { // MARK: - Getter and setter OAG_INLINE OAG_CONSTEXPR - const OAGGraphContextRef get_context() const OAG_NOEXCEPT { - return &_context; + const OAG::Graph &get_graph() const OAG_NOEXCEPT { + return _graph; } - + OAG_INLINE OAG_CONSTEXPR - OAGGraphContextRef get_context() OAG_NOEXCEPT { - return &_context; + OAG::Graph &get_graph() OAG_NOEXCEPT { + return _graph; } OAG_INLINE OAG_CONSTEXPR @@ -98,7 +99,7 @@ class Subgraph final { struct OAGSubgraphStorage { CFRuntimeBase base; - OAG::Subgraph *subgraph; + OAG::Subgraph *_Nullable subgraph; }; namespace OAG { From d67e2bbee096566a6ebebfa7ec26ea8e4e3edf87 Mon Sep 17 00:00:00 2001 From: James Moschou Date: Sun, 24 Aug 2025 15:34:25 +0200 Subject: [PATCH 2/4] Add OAGGraphInternAttributeType --- .../Attribute/Attribute/Attribute.swift | 11 ++++++----- Sources/OpenAttributeGraph/Graph/Graph.swift | 12 ++++-------- Sources/OpenAttributeGraphCxx/Graph/OAGGraph.cpp | 7 +++++++ .../include/OpenAttributeGraph/OAGGraph.h | 7 +++++++ 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/Sources/OpenAttributeGraph/Attribute/Attribute/Attribute.swift b/Sources/OpenAttributeGraph/Attribute/Attribute/Attribute.swift index 716eb10b..1d1e0cb5 100644 --- a/Sources/OpenAttributeGraph/Attribute/Attribute/Attribute.swift +++ b/Sources/OpenAttributeGraph/Attribute/Attribute/Attribute.swift @@ -114,11 +114,12 @@ public struct Attribute { } let index = Graph.typeIndex( ctx: context, - body: Body.self, - valueType: Metadata(Value.self), - flags: flags, - update: update - ) + body: Metadata(Body.self) + ) { + let pointer = UnsafeMutablePointer<_AttributeType>.allocate(capacity: 1) + // TODO + return UnsafePointer(pointer) + } identifier = OAGGraphCreateAttribute(index: index, body: body, value: value) #endif } diff --git a/Sources/OpenAttributeGraph/Graph/Graph.swift b/Sources/OpenAttributeGraph/Graph/Graph.swift index 1f047be9..d0fc0900 100644 --- a/Sources/OpenAttributeGraph/Graph/Graph.swift +++ b/Sources/OpenAttributeGraph/Graph/Graph.swift @@ -8,16 +8,12 @@ public import OpenAttributeGraphCxx extension Graph { + @_silgen_name("OAGGraphInternAttributeType") public static func typeIndex( ctx: UnownedGraphContext, - body: _AttributeBody.Type, - valueType: Metadata, - flags: _AttributeType.Flags, - update: AttributeUpdateBlock - ) -> Int { - // TODO: __AGGraphInternAttributeType - 0 - } + body: Metadata, + makeAttributeType: () -> UnsafePointer<_AttributeType> + ) -> Int } @_silgen_name("OAGGraphSetInvalidationCallback") diff --git a/Sources/OpenAttributeGraphCxx/Graph/OAGGraph.cpp b/Sources/OpenAttributeGraphCxx/Graph/OAGGraph.cpp index dda1ac31..de5736ac 100644 --- a/Sources/OpenAttributeGraphCxx/Graph/OAGGraph.cpp +++ b/Sources/OpenAttributeGraphCxx/Graph/OAGGraph.cpp @@ -125,6 +125,13 @@ void OAGGraphInvalidate(OAGGraphRef graph) { graph->context.setInvalid(true); } +uint32_t OAGGraphInternAttributeType(OAGUnownedGraphContextRef graph, OAGTypeID type, + const OAGAttributeType *(*make_attribute_type)(const void *context OAG_SWIFT_CONTEXT) OAG_SWIFT_CC(swift), + const void *context) { + // TODO + return 0; +} + void OAGGraphInvalidateAllValues(OAGGraphRef graph) { if (graph->context.isInvalid()) { OAG::precondition_failure("invalidated graph"); diff --git a/Sources/OpenAttributeGraphCxx/include/OpenAttributeGraph/OAGGraph.h b/Sources/OpenAttributeGraphCxx/include/OpenAttributeGraph/OAGGraph.h index 10f818cb..d02dd1f7 100644 --- a/Sources/OpenAttributeGraphCxx/include/OpenAttributeGraph/OAGGraph.h +++ b/Sources/OpenAttributeGraphCxx/include/OpenAttributeGraph/OAGGraph.h @@ -7,6 +7,7 @@ #include #include +#include #include // Note: Place all structure declaration in a single place to avoid header cycle dependency @@ -69,6 +70,12 @@ OAG_EXPORT OAG_REFINED_FOR_SWIFT void OAGGraphInvalidate(OAGGraphRef graph) OAG_SWIFT_NAME(OAGGraphRef.invalidate(self:)); +OAG_EXPORT +OAG_REFINED_FOR_SWIFT +uint32_t OAGGraphInternAttributeType(OAGUnownedGraphContextRef graph, OAGTypeID type, + const OAGAttributeType * _Nonnull (* _Nonnull make_attribute_type)(const void * _Nullable context OAG_SWIFT_CONTEXT) OAG_SWIFT_CC(swift), + const void * _Nullable context); + OAG_EXPORT OAG_REFINED_FOR_SWIFT void OAGGraphInvalidateAllValues(OAGGraphRef graph) OAG_SWIFT_NAME(OAGGraphRef.invalidateAllValues(self:)); From 9d1140ac7b6816a299ed8629a3ddcf42784e7573 Mon Sep 17 00:00:00 2001 From: James Moschou Date: Sun, 24 Aug 2025 17:14:56 +0200 Subject: [PATCH 3/4] Add OAG_ASSUME_NONNULL_BEGIN/END guards to Subgraph.hpp --- .../include/OpenAttributeGraphCxx/Graph/Subgraph.hpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Sources/OpenAttributeGraphCxx/include/OpenAttributeGraphCxx/Graph/Subgraph.hpp b/Sources/OpenAttributeGraphCxx/include/OpenAttributeGraphCxx/Graph/Subgraph.hpp index 225976a1..9b9f1f9d 100644 --- a/Sources/OpenAttributeGraphCxx/include/OpenAttributeGraphCxx/Graph/Subgraph.hpp +++ b/Sources/OpenAttributeGraphCxx/include/OpenAttributeGraphCxx/Graph/Subgraph.hpp @@ -12,6 +12,8 @@ #include #include +OAG_ASSUME_NONNULL_BEGIN + typedef struct OAG_BRIDGED_TYPE(id) OAGSubgraphStorage * OAGSubgraphRef; namespace OAG { @@ -28,9 +30,9 @@ class Subgraph final { public: // MARK: - CF related - static Subgraph *from_cf(OAGSubgraphRef cf_subgraph) OAG_NOEXCEPT; + static Subgraph *_Nullable from_cf(OAGSubgraphRef cf_subgraph) OAG_NOEXCEPT; - OAGSubgraphRef to_cf() const OAG_NOEXCEPT { + _Nullable OAGSubgraphRef to_cf() const OAG_NOEXCEPT { return reinterpret_cast(_object); } @@ -47,12 +49,12 @@ class Subgraph final { } OAG_INLINE OAG_CONSTEXPR - static Subgraph *get_current() OAG_NOEXCEPT { + static Subgraph *_Nullable get_current() OAG_NOEXCEPT { return (OAG::Subgraph*)pthread_getspecific(OAG::Subgraph::current_key()); } OAG_INLINE OAG_CONSTEXPR - static int set_current(Subgraph *subgraph) OAG_NOEXCEPT { + static int set_current(Subgraph *_Nullable subgraph) OAG_NOEXCEPT { return pthread_setspecific(OAG::Subgraph::current_key(), subgraph); } @@ -109,3 +111,5 @@ class SubgraphObject final { } #endif /* Subgraph_hpp */ + +OAG_ASSUME_NONNULL_END From 5aa6545111feb74d8364de24facbc7543dee1356 Mon Sep 17 00:00:00 2001 From: James Moschou Date: Sun, 7 Sep 2025 16:03:38 +0200 Subject: [PATCH 4/4] Update documentation link --- .../OpenAttributeGraph.docc/Graph/Dependency-Graphs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/OpenAttributeGraph/OpenAttributeGraph.docc/Graph/Dependency-Graphs.md b/Sources/OpenAttributeGraph/OpenAttributeGraph.docc/Graph/Dependency-Graphs.md index 3a2316be..1cc95dc0 100644 --- a/Sources/OpenAttributeGraph/OpenAttributeGraph.docc/Graph/Dependency-Graphs.md +++ b/Sources/OpenAttributeGraph/OpenAttributeGraph.docc/Graph/Dependency-Graphs.md @@ -22,7 +22,7 @@ Graphs automatically track dependencies as attributes access other attributes du ### Graph Management -- ``Graph/typeIndex(ctx:body:valueType:flags:update:)`` +- ``Graph/typeIndex(ctx:body:makeAttributeType:)`` - ``Subgraph/currentGraphContext`` ### Update Coordination