Skip to content

Commit bb067c9

Browse files
committed
Implement context pointer for Graph
1 parent 98c970f commit bb067c9

5 files changed

Lines changed: 45 additions & 7 deletions

File tree

Sources/ComputeCxx/Graph/AGGraph.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@ AGGraphRef AGGraphContextGetGraph(AGUnownedGraphContextRef storage) {
7979
return graph_context->to_cf();
8080
}
8181

82+
#pragma mark - User context
83+
84+
const void *AGGraphGetContext(AGGraphRef graph) {
85+
auto graph_context = AG::Graph::Context::from_cf(graph);
86+
return graph_context->context();
87+
}
88+
89+
void AGGraphSetContext(AGGraphRef graph, const void *context) {
90+
auto graph_context = AG::Graph::Context::from_cf(graph);
91+
graph_context->set_context(context);
92+
}
93+
8294
#pragma mark - Attribute types
8395

8496
uint32_t AGGraphInternAttributeType(AGUnownedGraphRef unowned_graph, AGTypeID type,

Sources/ComputeCxx/Graph/AGGraph.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ typedef struct AGGraphContextStorage *AGUnownedGraphContextRef AG_SWIFT_STRUCT;
1818

1919
CF_EXPORT
2020
CF_REFINED_FOR_SWIFT
21-
CFTypeID AGGraphGetTypeID() AG_SWIFT_NAME(getter : AGGraphRef.typeID());
21+
CFTypeID AGGraphGetTypeID() CF_SWIFT_NAME(getter:AGGraphRef.typeID());
2222

2323
// MARK: Graph Context
2424

@@ -39,6 +39,17 @@ CF_REFINED_FOR_SWIFT
3939
AGGraphRef AGGraphContextGetGraph(AGUnownedGraphContextRef context)
4040
CF_SWIFT_NAME(getter:AGUnownedGraphContextRef.graph(self:));
4141

42+
// MARK: User context
43+
44+
CF_EXPORT
45+
CF_REFINED_FOR_SWIFT
46+
const void *_Nullable AGGraphGetContext(AGGraphRef graph) CF_SWIFT_NAME(getter:AGGraphRef.context(self:));
47+
48+
CF_EXPORT
49+
CF_REFINED_FOR_SWIFT
50+
void AGGraphSetContext(AGGraphRef graph, const void *_Nullable context)
51+
CF_SWIFT_NAME(setter:AGGraphRef.context(self:_:));
52+
4253
// MARK: Attribute types
4354

4455
CF_EXPORT

Sources/ComputeCxx/Graph/Context.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@
77

88
namespace AG {
99

10-
Graph::Context::Context(Graph *graph) {
11-
_graph = graph;
12-
_context_info = nullptr;
13-
_unique_id = AGMakeUniqueID();
14-
10+
Graph::Context::Context(Graph *graph) : _graph(graph), _unique_id(AGMakeUniqueID()) {
1511
Graph::retain(graph);
1612
graph->_contexts_by_id.insert(_unique_id, this);
1713
}

Sources/ComputeCxx/Graph/Context.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace AG {
1414
class Graph::Context {
1515
private:
1616
Graph *_graph;
17-
const void *_context_info;
17+
const void *_context = nullptr;
1818
uint64_t _unique_id;
1919

2020
bool _invalidated;
@@ -28,6 +28,9 @@ class Graph::Context {
2828

2929
Graph &graph() const { return *_graph; };
3030

31+
const void *_Nullable context() const { return _context; };
32+
void set_context(const void *_Nullable context) { _context = context; };
33+
3134
bool invalidated() const { return _invalidated; };
3235
void set_invalidated(bool invalidated) { _invalidated = invalidated; };
3336
};

Tests/ComputeTests/Shared/Graph/GraphTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@ struct GraphTests {
2020
}
2121

2222
}
23+
24+
@Suite
25+
struct ContextTests {
26+
27+
@Test
28+
func storesContextPointer() {
29+
let graph = Graph()
30+
#expect(graph.context == nil)
31+
32+
withUnsafePointer(to: "Value") { pointer in
33+
graph.context = UnsafeRawPointer(pointer)
34+
#expect(graph.context == UnsafeRawPointer(pointer))
35+
}
36+
}
37+
38+
}
2339

2440
@Suite
2541
struct InternAttributeTypeTests {

0 commit comments

Comments
 (0)