forked from kovacsv/VisualScriptEngineWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNE_Node.cpp
More file actions
381 lines (316 loc) · 8.5 KB
/
NE_Node.cpp
File metadata and controls
381 lines (316 loc) · 8.5 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include "NE_Node.hpp"
#include "NE_InputSlot.hpp"
#include "NE_OutputSlot.hpp"
#include "NE_Debug.hpp"
#include "NE_MemoryStream.hpp"
namespace NE
{
SERIALIZATION_INFO (Node, 1);
NodeEvaluator::NodeEvaluator ()
{
}
NodeEvaluator::~NodeEvaluator ()
{
}
Node::Node () :
nodeId (NullNodeId),
inputSlots (),
outputSlots (),
nodeEvaluator (nullptr)
{
}
Node::~Node ()
{
}
bool Node::IsEmpty () const
{
return nodeId == NullNodeId && inputSlots.IsEmpty () && outputSlots.IsEmpty ();
}
const NodeId& Node::GetId () const
{
return nodeId;
}
bool Node::HasInputSlot (const SlotId& slotId) const
{
return inputSlots.Contains (slotId);
}
bool Node::HasOutputSlot (const SlotId& slotId) const
{
return outputSlots.Contains (slotId);
}
bool Node::IsInputSlotConnected (const SlotId& slotId) const
{
if (DBGERROR (nodeEvaluator == nullptr)) {
return false;
}
InputSlotConstPtr inputSlot = inputSlots.Get (slotId);
if (DBGERROR (inputSlot == nullptr)) {
return false;
}
return nodeEvaluator->HasConnectedOutputSlots (inputSlot);
}
InputSlotConstPtr Node::GetInputSlot (const SlotId& slotId) const
{
return inputSlots.Get (slotId);
}
OutputSlotConstPtr Node::GetOutputSlot (const SlotId& slotId) const
{
return outputSlots.Get (slotId);
}
size_t Node::GetInputSlotCount () const
{
return inputSlots.Count ();
}
size_t Node::GetOutputSlotCount () const
{
return outputSlots.Count ();
}
void Node::EnumerateInputSlots (const std::function<bool (InputSlotPtr)>& processor)
{
inputSlots.Enumerate (processor);
}
void Node::EnumerateOutputSlots (const std::function<bool (OutputSlotPtr)>& processor)
{
outputSlots.Enumerate (processor);
}
void Node::EnumerateInputSlots (const std::function<bool (InputSlotConstPtr)>& processor) const
{
inputSlots.Enumerate (processor);
}
void Node::EnumerateOutputSlots (const std::function<bool (OutputSlotConstPtr)>& processor) const
{
outputSlots.Enumerate (processor);
}
ValueConstPtr Node::Evaluate (EvaluationEnv& env) const
{
if (DBGERROR (nodeEvaluator == nullptr)) {
return nullptr;
}
CalculationStatus calcStatus = GetCalculationStatus ();
if (calcStatus == CalculationStatus::Calculated) {
return nodeEvaluator->GetCalculatedNodeValue (nodeId);
}
if (calcStatus != CalculationStatus::NeedToCalculate) {
return nullptr;
}
ValueConstPtr value = Calculate (env);
nodeEvaluator->SetCalculatedNodeValue (nodeId, value);
ProcessCalculatedValue (value, env);
return value;
}
ValueConstPtr Node::GetCalculatedValue () const
{
if (DBGERROR (nodeEvaluator == nullptr)) {
return nullptr;
}
if (DBGERROR (!nodeEvaluator->HasCalculatedNodeValue (nodeId))) {
return nullptr;
}
return nodeEvaluator->GetCalculatedNodeValue (nodeId);
}
bool Node::HasCalculatedValue () const
{
if (DBGERROR (nodeEvaluator == nullptr)) {
return false;
}
return nodeEvaluator->HasCalculatedNodeValue (GetId ());
}
Node::CalculationStatus Node::GetCalculationStatus () const
{
if (DBGERROR (nodeEvaluator == nullptr)) {
return CalculationStatus::NeedToCalculate;
}
if (nodeEvaluator->HasCalculatedNodeValue (nodeId)) {
return CalculationStatus::Calculated;
}
if (nodeEvaluator->IsCalculationEnabled () || IsForceCalculated ()) {
return CalculationStatus::NeedToCalculate;
} else {
return CalculationStatus::NeedToCalculateButDisabled;
}
}
void Node::InvalidateValue () const
{
if (DBGERROR (nodeEvaluator == nullptr)) {
return;
}
nodeEvaluator->InvalidateNodeValue (GetId ());
}
Stream::Status Node::Read (InputStream& inputStream)
{
if (DBGERROR (!IsEmpty ())) {
return Stream::Status::Error;
}
ObjectHeader header (inputStream);
nodeId.Read (inputStream);
size_t inputSlotCount = 0;
inputStream.Read (inputSlotCount);
for (size_t i = 0; i < inputSlotCount; ++i) {
InputSlotPtr inputSlot (ReadDynamicObject<InputSlot> (inputStream));
if (DBGERROR (inputSlot == nullptr)) {
return Stream::Status::Error;
}
if (DBGERROR (!RegisterInputSlot (inputSlot))) {
return Stream::Status::Error;
}
}
size_t outputSlotCount = 0;
inputStream.Read (outputSlotCount);
for (size_t i = 0; i < outputSlotCount; ++i) {
OutputSlotPtr outputSlot (ReadDynamicObject<OutputSlot> (inputStream));
if (DBGERROR (outputSlot == nullptr)) {
return Stream::Status::Error;
}
if (DBGERROR (!RegisterOutputSlot (outputSlot))) {
return Stream::Status::Error;
}
}
return inputStream.GetStatus ();
}
Stream::Status Node::Write (OutputStream& outputStream) const
{
ObjectHeader header (outputStream, serializationInfo);
nodeId.Write (outputStream);
outputStream.Write (inputSlots.Count ());
inputSlots.Enumerate ([&] (const InputSlotConstPtr& inputSlot) {
WriteDynamicObject (outputStream, inputSlot.get ());
return true;
});
outputStream.Write (outputSlots.Count ());
outputSlots.Enumerate ([&] (const OutputSlotConstPtr& outputSlot) {
WriteDynamicObject (outputStream, outputSlot.get ());
return true;
});
return outputStream.GetStatus ();
}
ValueConstPtr Node::GetInputSlotDefaultValue (const SlotId& slotId) const
{
InputSlotConstPtr inputSlot = inputSlots.Get (slotId);
if (DBGERROR (inputSlot == nullptr)) {
return nullptr;
}
return inputSlot->GetDefaultValue ();
}
void Node::SetInputSlotDefaultValue (const SlotId& slotId, const ValueConstPtr& newDefaultValue)
{
InputSlotPtr inputSlot = inputSlots.Get (slotId);
if (DBGERROR (inputSlot == nullptr)) {
return;
}
inputSlot->SetDefaultValue (newDefaultValue);
}
bool Node::RegisterInputSlot (const InputSlotPtr& newInputSlot)
{
if (DBGERROR (inputSlots.Contains (newInputSlot->GetId ()))) {
return false;
}
if (DBGERROR (newInputSlot->HasOwnerNode ())) {
return false;
}
if (DBGERROR (!inputSlots.Insert (newInputSlot))) {
return false;
}
if (DBGERROR (!newInputSlot->SetOwnerNode (this))) {
return false;
}
return true;
}
bool Node::RegisterOutputSlot (const OutputSlotPtr& newOutputSlot)
{
if (DBGERROR (outputSlots.Contains (newOutputSlot->GetId ()))) {
return false;
}
if (DBGERROR (newOutputSlot->HasOwnerNode ())) {
return false;
}
if (DBGERROR (!outputSlots.Insert (newOutputSlot))) {
return false;
}
if (DBGERROR (!newOutputSlot->SetOwnerNode (this))) {
return false;
}
return true;
}
ValueConstPtr Node::EvaluateInputSlot (const SlotId& slotId, EvaluationEnv& env) const
{
if (DBGERROR (!HasInputSlot (slotId))) {
return nullptr;
}
InputSlotConstPtr inputSlot = GetInputSlot (slotId);
if (DBGERROR (inputSlot == nullptr)) {
return nullptr;
}
return EvaluateInputSlot (inputSlot, env);
}
void Node::SetId (const NodeId& newNodeId)
{
nodeId = newNodeId;
}
void Node::SetEvaluator (const NodeEvaluatorConstPtr& newNodeEvaluator)
{
nodeEvaluator = newNodeEvaluator;
}
bool Node::IsEvaluatorSet () const
{
return nodeEvaluator != nullptr;
}
void Node::ClearEvaluator ()
{
nodeId = NullNodeId;
nodeEvaluator = nullptr;
}
bool Node::IsForceCalculated () const
{
return false;
}
void Node::ProcessCalculatedValue (const ValueConstPtr&, EvaluationEnv&) const
{
}
ValueConstPtr Node::EvaluateInputSlot (const InputSlotConstPtr& inputSlot, EvaluationEnv& env) const
{
if (DBGERROR (nodeEvaluator == nullptr)) {
return nullptr;
}
if (!nodeEvaluator->HasConnectedOutputSlots (inputSlot)) {
return inputSlot->GetDefaultValue ();
}
std::vector<OutputSlotConstPtr> connectedOutputSlots;
nodeEvaluator->EnumerateConnectedOutputSlots (inputSlot, [&] (const OutputSlotConstPtr& outputSlot) {
connectedOutputSlots.push_back (outputSlot);
});
OutputSlotConnectionMode outputSlotConnectionMode = inputSlot->GetOutputSlotConnectionMode ();
if (outputSlotConnectionMode == OutputSlotConnectionMode::Single) {
DBGASSERT (connectedOutputSlots.size () == 1);
return connectedOutputSlots[0]->Evaluate (env);
} else if (inputSlot->GetOutputSlotConnectionMode () == OutputSlotConnectionMode::Multiple) {
ListValuePtr result (new ListValue ());
for (const OutputSlotConstPtr& outputSlot : connectedOutputSlots) {
result->Push (outputSlot->Evaluate (env));
}
return result;
}
DBGBREAK ();
return nullptr;
}
NodePtr Node::Clone (const NodeConstPtr& node)
{
MemoryOutputStream outputStream;
if (DBGERROR (!WriteDynamicObject (outputStream, node.get ()))) {
return nullptr;
}
MemoryInputStream inputStream (outputStream.GetBuffer ());
NodePtr result (ReadDynamicObject<Node> (inputStream));
if (DBGERROR (result == nullptr)) {
return nullptr;
}
return result;
}
bool Node::IsEqual (const NodeConstPtr& aNode, const NodeConstPtr& bNode)
{
MemoryOutputStream aStream;
MemoryOutputStream bStream;
aNode->Write (aStream);
bNode->Write (bStream);
return aStream.GetBuffer () == bStream.GetBuffer ();
}
}