-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathOAGSubgraph.cpp
More file actions
255 lines (224 loc) · 7.52 KB
/
OAGSubgraph.cpp
File metadata and controls
255 lines (224 loc) · 7.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//
// OAGSubgraph.cpp
// OpenAttributeGraphCxx
#include <OpenAttributeGraph/OAGSubgraph.h>
#include <OpenAttributeGraph/OAGGraph.h>
#include <OpenAttributeGraphCxx/Graph/Subgraph.hpp>
#include <OpenAttributeGraph/OAGGraphContext.h>
#include <OpenAttributeGraphCxx/Misc/assert.hpp>
#include <OpenAttributeGraphCxx/Misc/env.hpp>
#include <pthread.h>
#if !OAG_TARGET_OS_WASI
#include <dispatch/dispatch.h>
#endif
namespace {
CFRuntimeClass &subgraph_type_id() {
static auto dealloc = [](const void* ptr) {
OAGSubgraphRef storage = (OAGSubgraphRef)ptr;
auto subgraph = storage->subgraph;
if (subgraph == nullptr) {
return;
}
subgraph->clear_object();
subgraph->invalidate_and_delete_(false);
};
static CFRuntimeClass klass = {
0,
"OAGSubgraph",
NULL, // init
NULL, // copy
dealloc,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
0,
};
return klass;
}
}
CFTypeID OAGSubgraphGetTypeID() {
static CFTypeID type = _CFRuntimeRegisterClass(&subgraph_type_id());
return type;
}
OAGSubgraphRef OAGSubgraphCreate(OAGGraphRef cf_graph) {
return OAGSubgraphCreate2(cf_graph, OAGAttributeNil);
}
OAGSubgraphRef OAGSubgraphCreate2(OAGGraphRef cf_graph, OAGAttribute attribute) {
OAG::Graph::Context &context = OAG::Graph::Context::from_cf(cf_graph);
const CFIndex extraSize = sizeof(OAGSubgraphStorage)-sizeof(CFRuntimeBase);
#if OAG_TARGET_CPU_WASM32
// FIXME: extraSize will always be 8 thus it will fail on WASM. Investate later.
static_assert(extraSize == 8);
#else
static_assert(extraSize == sizeof(void *));
#endif
OAGSubgraphRef cf_subgrah = (OAGSubgraphRef)_CFRuntimeCreateInstance(kCFAllocatorDefault, OAGSubgraphGetTypeID(), extraSize, nullptr);
if (cf_subgrah == nullptr) {
OAG::precondition_failure("memory allocation failure.");
}
cf_subgrah->subgraph = new OAG::Subgraph((OAG::SubgraphObject *)cf_subgrah, context, attribute);
return cf_subgrah;
}
_Nullable OAGSubgraphRef OAGSubgraphGetCurrent() {
OAG::Subgraph *subgraph = OAG::Subgraph::get_current();
if (subgraph == nullptr) {
return nullptr;
}
return subgraph->to_cf();
}
void OAGSubgraphSetCurrent(_Nullable OAGSubgraphRef cf_subgraph) {
OAG::Subgraph *old_subgraph = OAG::Subgraph::get_current();
if (cf_subgraph == nullptr) {
OAG::Subgraph::set_current(nullptr);
} else {
OAG::Subgraph *subgraph = cf_subgraph->subgraph;
OAG::Subgraph::set_current(subgraph);
if (subgraph != nullptr) {
CFRetain(cf_subgraph);
}
}
if (old_subgraph != nullptr) {
OAGSubgraphRef old_cf_Subgraph = old_subgraph->to_cf();
if (old_cf_Subgraph) {
CFRelease(old_cf_Subgraph);
}
}
}
OAGUnownedGraphContextRef OAGSubgraphGetCurrentGraphContext() {
OAG::Subgraph *subgraph = OAG::Subgraph::get_current();
if (subgraph == nullptr) {
return nullptr;
}
OAG::Graph &graph = subgraph->get_graph();
return reinterpret_cast<OAGUnownedGraphContextRef>(&graph);
}
void OAGSubgraphInvalidate(OAGSubgraphRef cf_subgraph) {
if (cf_subgraph->subgraph == nullptr) {
return;
}
cf_subgraph->subgraph->invalidate_and_delete_(false);
}
bool OAGSubgraphIsValid(OAGSubgraphRef cf_subgraph) {
if (cf_subgraph->subgraph == nullptr) {
return false;
}
return !cf_subgraph->subgraph->isInvalid();
}
OAGGraphRef OAGSubgraphGetGraph(OAGSubgraphRef cf_subgraph) {
if (cf_subgraph->subgraph == nullptr) {
OAG::precondition_failure("accessing invalidated subgraph");
}
// TODO
return nullptr;
}
void OAGSubgraphAddChild(OAGSubgraphRef parent, OAGSubgraphRef child) {
OAGSubgraphAddChild2(parent, child, 0);
}
void OAGSubgraphAddChild2(OAGSubgraphRef parent, OAGSubgraphRef child, uint8_t tag) {
// TODO
}
void OAGSubgraphRemoveChild(OAGSubgraphRef parent, OAGSubgraphRef child) {
// TODO
}
OAGSubgraphRef OAGSubgraphGetChild(OAGSubgraphRef cf_subgraph, uint32_t index, uint8_t *_Nullable tag_out) {
// TODO
return nullptr;
}
uint32_t OAGSubgraphGetChildCount(OAGSubgraphRef cf_subgraph) {
// TODO
return 0;
}
OAGSubgraphRef OAGSubgraphGetParent(OAGSubgraphRef cf_subgraph, int64_t index) {
// TODO
return nullptr;
}
uint64_t OAGSubgraphGetParentCount(OAGSubgraphRef cf_subgraph) {
// TODO
return 0;
}
bool OAGSubgraphIsAncestor(OAGSubgraphRef cf_subgraph, OAGSubgraphRef other) {
// TODO
return false;
}
void OAGSubgraphApply(OAGSubgraphRef cf_subgraph,
OAGAttributeFlags flags,
const void (*function)(const void * _Nullable context OAG_SWIFT_CONTEXT, OAGAttribute attribute) OAG_SWIFT_CC(swift),
const void * _Nullable context) {
if (cf_subgraph->subgraph == nullptr) {
return;
}
return cf_subgraph->subgraph->apply(flags, OAG::ClosureFunction<void, OAGAttribute>(function, context));
}
void OAGSubgraphUpdate(OAGSubgraphRef cf_subgraph, OAGAttributeFlags flags) {
OAG::Subgraph *subgraph = cf_subgraph->subgraph;
if (subgraph == nullptr) {
return;
}
// subgraph->update(flags);
}
bool OAGSubgraphIsDirty(OAGSubgraphRef cf_subgraph, OAGAttributeFlags flags) {
OAG::Subgraph *subgraph = cf_subgraph->subgraph;
if (subgraph == nullptr) {
return false;
}
// TODO
return false;
}
OAGObserverID OAGSubgraphAddObserver(OAGSubgraphRef cf_subgraph,
const void (*function)(const void * _Nullable context OAG_SWIFT_CONTEXT) OAG_SWIFT_CC(swift),
const void * _Nullable context) {
OAG::Subgraph *subgraph = cf_subgraph->subgraph;
if (subgraph == nullptr) {
OAG::precondition_failure("accessing invalidated subgraph");
}
return subgraph->add_observer(OAG::ClosureFunction<void>(function, context));
}
void OAGSubgraphRemoveObserver(OAGSubgraphRef cf_subgraph, OAGObserverID observerID) {
OAG::Subgraph *subgraph = cf_subgraph->subgraph;
if (subgraph == nullptr) {
return;
}
subgraph->remove_observer(observerID);
}
#if !OAG_TARGET_OS_WASI
static bool should_record_tree;
static dispatch_once_t should_record_tree_once;
void init_should_record_tree(void * _Nullable context) {
should_record_tree = OAG::get_env("OAG_TREE") != 0;
}
#endif
bool OAGSubgraphShouldRecordTree() {
#if !OAG_TARGET_OS_WASI
dispatch_once_f(&should_record_tree_once, NULL, init_should_record_tree);
return should_record_tree;
#else
return false;
#endif
}
void OAGSubgraphSetShouldRecordTree() {
#if !OAG_TARGET_OS_WASI
dispatch_once_f(&should_record_tree_once, NULL, init_should_record_tree);
should_record_tree = true;
#endif
}
void OAGSubgraphBeginTreeElement(OAGAttribute attribute, OAGTypeID type, uint32_t flags) {
OAG::Subgraph * subgraph = OAG::Subgraph::get_current();
if (subgraph) {
subgraph->begin_tree(attribute, reinterpret_cast<OAG::swift::metadata const*>(type), flags);
}
}
void OAGSubgraphAddTreeValue(OAGAttribute attribute, OAGTypeID type, const char * key, uint32_t flags) {
OAG::Subgraph * subgraph = OAG::Subgraph::get_current();
if (subgraph) {
subgraph->add_tree_value(attribute, reinterpret_cast<OAG::swift::metadata const*>(type), key, flags);
}
}
void OAGSubgraphEndTreeElement(OAGAttribute attribute) {
OAG::Subgraph * subgraph = OAG::Subgraph::get_current();
if (subgraph) {
subgraph->end_tree(attribute);
}
}