-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathOGSubgraph.cpp
More file actions
215 lines (192 loc) · 6.26 KB
/
OGSubgraph.cpp
File metadata and controls
215 lines (192 loc) · 6.26 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
//
// OGSubgraph.cpp
//
//
// Created by Kyle on 2024/2/15.
//
#include "OGSubgraph.h"
#include "OGGraph.h"
#include "Subgraph.hpp"
#include "OGGraphContext.h"
#include "../Util/assert.hpp"
#include "../Util/env.hpp"
#include <pthread.h>
#if !OG_TARGET_OS_WASI
#include <dispatch/dispatch.h>
#endif
namespace {
CFRuntimeClass &subgraph_type_id() {
static auto dealloc = [](const void* ptr) {
OGSubgraphRef storage = (OGSubgraphRef)ptr;
auto subgraph = storage->subgraph;
if (subgraph == nullptr) {
return;
}
subgraph->clear_object();
subgraph->invalidate_and_delete_(false);
};
static CFRuntimeClass klass = {
0,
"OGSubgraph",
NULL, // init
NULL, // copy
dealloc,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
0,
};
return klass;
}
}
CFTypeID OGSubgraphGetTypeID() {
static CFTypeID type = _CFRuntimeRegisterClass(&subgraph_type_id());
return type;
}
OGSubgraphRef OGSubgraphCreate(OGGraphRef cf_graph) {
return OGSubgraphCreate2(cf_graph, OGAttributeNil);
}
OGSubgraphRef OGSubgraphCreate2(OGGraphRef cf_graph, OGAttribute attribute) {
OG::Graph::Context &context = OG::Graph::Context::from_cf(cf_graph);
const CFIndex extraSize = sizeof(OGSubgraphStorage)-sizeof(CFRuntimeBase);
#if OG_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
OGSubgraphRef cf_subgrah = (OGSubgraphRef)_CFRuntimeCreateInstance(kCFAllocatorDefault, OGSubgraphGetTypeID(), extraSize, nullptr);
if (cf_subgrah == nullptr) {
OG::precondition_failure("memory allocation failure.");
}
cf_subgrah->subgraph = new OG::Subgraph((OG::SubgraphObject *)cf_subgrah, context, attribute);
return cf_subgrah;
}
_Nullable OGSubgraphRef OGSubgraphGetCurrent() {
OG::Subgraph *subgraph = OG::Subgraph::get_current();
if (subgraph == nullptr) {
return nullptr;
}
return subgraph->to_cf();
}
void OGSubgraphSetCurrent(_Nullable OGSubgraphRef cf_subgraph) {
OG::Subgraph *old_subgraph = OG::Subgraph::get_current();
if (cf_subgraph == nullptr) {
OG::Subgraph::set_current(nullptr);
} else {
OG::Subgraph *subgraph = cf_subgraph->subgraph;
OG::Subgraph::set_current(subgraph);
if (subgraph != nullptr) {
CFRetain(cf_subgraph);
}
}
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::get_current();
if (subgraph == nullptr) {
return nullptr;
}
return subgraph->get_context();
}
void OGSubgraphInvalidate(OGSubgraphRef cf_subgraph) {
if (cf_subgraph->subgraph == nullptr) {
return;
}
cf_subgraph->subgraph->invalidate_and_delete_(false);
}
bool OGSubgraphIsValid(OGSubgraphRef cf_subgraph) {
if (cf_subgraph->subgraph == nullptr) {
return false;
}
return !cf_subgraph->subgraph->isInvalid();
}
OGGraphRef OGSubgraphGetGraph(OGSubgraphRef cf_subgraph) {
if (cf_subgraph->subgraph == nullptr) {
OG::precondition_failure("accessing invalidated subgraph");
}
return OGGraphContextGetGraph(cf_subgraph->subgraph->get_context());
}
void OGSubgraphAddChild(OGSubgraphRef parent, OGSubgraphRef child) {
// TODO
}
void OGSubgraphApply(OGSubgraphRef cf_subgraph,
OGAttributeFlags flags,
const void (*function)(const void * _Nullable context OG_SWIFT_CONTEXT, OGAttribute attribute) OG_SWIFT_CC(swift),
const void * _Nullable context) {
if (cf_subgraph->subgraph == nullptr) {
return;
}
return cf_subgraph->subgraph->apply(flags, OG::ClosureFunction<void, OGAttribute>(function, context));
}
void OGSubgraphUpdate(OGSubgraphRef cf_subgraph, OGAttributeFlags flags) {
OG::Subgraph *subgraph = cf_subgraph->subgraph;
if (subgraph == nullptr) {
return;
}
// subgraph->update(flags);
}
bool OGSubgraphIsDirty(OGSubgraphRef cf_subgraph, uint32_t unknown) {
OG::Subgraph *subgraph = cf_subgraph->subgraph;
if (subgraph == nullptr) {
return false;
}
// TODO
return false;
}
OGUniqueID OGSubgraphAddObserver(OGSubgraphRef cf_subgraph,
const void (*function)(const void * _Nullable context OG_SWIFT_CONTEXT) OG_SWIFT_CC(swift),
const void * _Nullable context) {
OG::Subgraph *subgraph = cf_subgraph->subgraph;
if (subgraph == nullptr) {
OG::precondition_failure("accessing invalidated subgraph");
}
return subgraph->add_observer(OG::ClosureFunction<void>(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, reinterpret_cast<OG::swift::metadata const*>(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, reinterpret_cast<OG::swift::metadata const*>(type), key, flags);
}
}
void OGSubgraphEndTreeElement(OGAttribute attribute) {
OG::Subgraph * subgraph = OG::Subgraph::get_current();
if (subgraph) {
subgraph->end_tree(attribute);
}
}