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 796f211f..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: GraphContext, - body: _AttributeBody.Type, - valueType: Metadata, - flags: _AttributeType.Flags, - update: AttributeUpdateBlock - ) -> Int { - // TODO: __AGGraphInternAttributeType - 0 - } + ctx: UnownedGraphContext, + body: Metadata, + makeAttributeType: () -> UnsafePointer<_AttributeType> + ) -> Int } @_silgen_name("OAGGraphSetInvalidationCallback") 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 diff --git a/Sources/OpenAttributeGraphCxx/Graph/OAGGraph.cpp b/Sources/OpenAttributeGraphCxx/Graph/OAGGraph.cpp index b7c4f661..de5736ac 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) { @@ -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/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 #include +#include #include // Note: Place all structure declaration in a single place to avoid header cycle dependency typedef struct OAG_BRIDGED_TYPE(id) OAGGraphStorage * OAGGraphRef OAG_SWIFT_NAME(Graph); typedef struct OAG_BRIDGED_TYPE(id) OAGSubgraphStorage * OAGSubgraphRef OAG_SWIFT_NAME(Subgraph); -typedef struct OAG_BRIDGED_TYPE(id) OAGGraphContextStorage * OAGGraphContextRef OAG_SWIFT_NAME(GraphContext); +typedef struct OAGGraphContextStorage * OAGUnownedGraphContextRef OAG_SWIFT_STRUCT OAG_SWIFT_NAME(UnownedGraphContext); struct OAGGraphStorage; -struct OAGGraphContextStorage; struct OAGSubgraphStorage; +struct OAGGraphContextStorage; typedef uint32_t OAGAttribute OAG_SWIFT_STRUCT OAG_SWIFT_NAME(AnyAttribute); @@ -63,12 +64,18 @@ void OAGGraphSetContext(OAGGraphRef graph, const void * _Nullable context) OAG_S OAG_EXPORT OAG_REFINED_FOR_SWIFT -OAGGraphContextRef OAGGraphGetGraphContext(OAGGraphRef graph) OAG_SWIFT_NAME(getter:OAGGraphRef.graphContext(self:)); +OAGUnownedGraphContextRef OAGGraphGetGraphContext(OAGGraphRef graph) OAG_SWIFT_NAME(getter:OAGGraphRef.graphContext(self:)); 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:)); diff --git a/Sources/OpenAttributeGraphCxx/include/OpenAttributeGraph/OAGGraphContext.h b/Sources/OpenAttributeGraphCxx/include/OpenAttributeGraph/OAGGraphContext.h index 150b7b41..f6bd96da 100644 --- a/Sources/OpenAttributeGraphCxx/include/OpenAttributeGraph/OAGGraphContext.h +++ b/Sources/OpenAttributeGraphCxx/include/OpenAttributeGraph/OAGGraphContext.h @@ -18,7 +18,7 @@ OAG_EXTERN_C_BEGIN OAG_EXPORT OAG_REFINED_FOR_SWIFT -OAGGraphRef OAGGraphContextGetGraph(OAGGraphContextRef context) OAG_SWIFT_NAME(getter:OAGGraphContextRef.graph(self:)); +OAGGraphRef OAGGraphContextGetGraph(void *context); OAG_EXTERN_C_END diff --git a/Sources/OpenAttributeGraphCxx/include/OpenAttributeGraph/OAGSubgraph.h b/Sources/OpenAttributeGraphCxx/include/OpenAttributeGraph/OAGSubgraph.h index e49efa4c..167e2eab 100644 --- a/Sources/OpenAttributeGraphCxx/include/OpenAttributeGraph/OAGSubgraph.h +++ b/Sources/OpenAttributeGraphCxx/include/OpenAttributeGraph/OAGSubgraph.h @@ -42,7 +42,7 @@ void OAGSubgraphSetCurrent(_Nullable OAGSubgraphRef cf_subgraph) OAG_SWIFT_NAME( OAG_EXPORT OAG_REFINED_FOR_SWIFT -_Nullable OAGGraphContextRef OAGSubgraphGetCurrentGraphContext(void) OAG_SWIFT_NAME(getter:OAGSubgraphRef.currentGraphContext()); +_Nullable OAGUnownedGraphContextRef OAGSubgraphGetCurrentGraphContext(void) OAG_SWIFT_NAME(getter:OAGSubgraphRef.currentGraphContext()); OAG_EXPORT OAG_REFINED_FOR_SWIFT diff --git a/Sources/OpenAttributeGraphCxx/include/OpenAttributeGraphCxx/Graph/Graph.hpp b/Sources/OpenAttributeGraphCxx/include/OpenAttributeGraphCxx/Graph/Graph.hpp index 37dbc0d8..1e6b1d33 100644 --- a/Sources/OpenAttributeGraphCxx/include/OpenAttributeGraphCxx/Graph/Graph.hpp +++ b/Sources/OpenAttributeGraphCxx/include/OpenAttributeGraphCxx/Graph/Graph.hpp @@ -64,6 +64,11 @@ class Graph final { _context = context; } + OAG_INLINE OAG_CONSTEXPR + OAGUniqueID get_id() OAG_NOEXCEPT { + return _id; + } + OAG_INLINE void set_invalidation_callback(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..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 { @@ -19,18 +21,19 @@ 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; 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 { - return _cf_subgraph; + _Nullable OAGSubgraphRef to_cf() const OAG_NOEXCEPT { + return reinterpret_cast(_object); } // MARK: - pthread related @@ -46,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); } @@ -75,13 +78,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 +101,7 @@ class Subgraph final { struct OAGSubgraphStorage { CFRuntimeBase base; - OAG::Subgraph *subgraph; + OAG::Subgraph *_Nullable subgraph; }; namespace OAG { @@ -108,3 +111,5 @@ class SubgraphObject final { } #endif /* Subgraph_hpp */ + +OAG_ASSUME_NONNULL_END