Vuo  2.4.1
VuoCompositionDiff.cc
Go to the documentation of this file.
1
10#include "VuoCompositionDiff.hh"
11
13#include "VuoRuntimeState.hh"
14
19{
20 diff = NULL;
21}
22
27{
28 free(diff);
29}
30
37{
38 free(this->diff);
39 this->diff = diff;
40}
41
47string VuoCompositionDiff::joinPortIdentifier(const string &nodeIdentifier, const string &portName)
48{
49 return nodeIdentifier + ":" + portName;
50}
51
55string VuoCompositionDiff::convertIdentifierToPath(const char *compositionIdentifier, const char *nodeIdentifier)
56{
57 return string(compositionIdentifier) + "/" + nodeIdentifier;
58}
59
63void VuoCompositionDiff::convertPathToIdentifier(const char *nodePath, string &compositionIdentifier, string &nodeIdentifier)
64{
65 string nodePathStr = nodePath;
66 size_t pos = nodePathStr.rfind("/");
67 if (pos == string::npos)
68 return;
69
70 compositionIdentifier = nodePathStr.substr(0, pos);
71 nodeIdentifier = nodePathStr.substr(pos+1);
72}
73
81VuoCompositionDiff::ChangeType VuoCompositionDiff::findNode(const char *compositionIdentifier, const char *nodeIdentifier,
82 json_object **replacementObj)
83{
84 if (! diff)
85 return ChangeStartStop;
86
87 json_object *diffJson = json_tokener_parse(diff);
88 if (! diffJson)
89 {
90 VUserLog("Couldn't parse the composition diff: %s", diff);
91 return ChangeNone;
92 }
93
94 string nodePath = convertIdentifierToPath(compositionIdentifier, nodeIdentifier);
95
96 set<string> keysFound;
97 int numChanges = json_object_array_length(diffJson);
98 for (int i = 0; i < numChanges; ++i)
99 {
100 json_object *change = json_object_array_get_idx(diffJson, i);
101 json_object_object_foreach(change, key, val)
102 {
103 if (! strcmp(nodePath.c_str(), json_object_get_string(val)))
104 {
105 keysFound.insert(key);
106
107 if (! strcmp(key, "replace") || ! strcmp(key, "with") || ! strcmp(key, "move") || ! strcmp(key, "to"))
108 {
109 json_object_get(change);
110 *replacementObj = change;
111 }
112 }
113 }
114 }
115
116 json_object_put(diffJson);
117
118 if (keysFound.find("add") != keysFound.end())
119 return ChangeAdd;
120 else if (keysFound.find("remove") != keysFound.end())
121 return ChangeRemove;
122 else if (keysFound.find("replace") != keysFound.end() || keysFound.find("with") != keysFound.end())
123 return ChangeReplace;
124 else if (keysFound.find("move") != keysFound.end() || keysFound.find("to") != keysFound.end())
125 return ChangeMove;
126
127 return ChangeNone;
128}
129
136bool VuoCompositionDiff::isNodeBeingRemovedOrReplaced(const char *compositionIdentifier, const char *nodeIdentifier,
137 json_object **replacementObj)
138{
139 enum ChangeType changeType = findNode(compositionIdentifier, nodeIdentifier, replacementObj);
140 return changeType == ChangeStartStop || changeType == ChangeRemove || changeType == ChangeReplace;
141}
142
149bool VuoCompositionDiff::isNodeBeingAddedOrReplaced(const char *compositionIdentifier, const char *nodeIdentifier,
150 json_object **replacementObj)
151{
152 enum ChangeType changeType = findNode(compositionIdentifier, nodeIdentifier, replacementObj);
153 return changeType == ChangeStartStop || changeType == ChangeAdd || changeType == ChangeReplace;
154}
155
159bool VuoCompositionDiff::isPortBeingReplaced(const char *portName, json_object *replacementObj)
160{
161 json_object *portMappingArray = NULL;
162 {
163 json_object *o;
164 if (json_object_object_get_ex(replacementObj, "ports", &o))
165 portMappingArray = o;
166 }
167 if (! portMappingArray)
168 return false;
169
170 int numPortMappings = json_object_array_length(portMappingArray);
171 for (int i = 0; i < numPortMappings; ++i)
172 {
173 json_object *portMapping = json_object_array_get_idx(portMappingArray, i);
174 json_object *o;
175
176 if (json_object_object_get_ex(portMapping, "map", &o) && ! strcmp(json_object_get_string(o), portName))
177 return true;
178 }
179
180 return false;
181}
182
186bool VuoCompositionDiff::isPortReplacingAnother(const char *portName, json_object *replacementObj,
187 string &oldNodeIdentifier, string &oldPortIdentifier)
188{
189 json_object *portMappingArray = NULL;
190 {
191 json_object *o;
192
193 if (json_object_object_get_ex(replacementObj, "replace", &o))
194 {
195 const char *oldNodePath = json_object_get_string(o);
196 string oldCompositionIdentifier;
197 convertPathToIdentifier(oldNodePath, oldCompositionIdentifier, oldNodeIdentifier);
198 }
199
200 if (json_object_object_get_ex(replacementObj, "ports", &o))
201 portMappingArray = o;
202 }
203 if (! portMappingArray)
204 return false;
205
206 string oldPortName;
207 bool foundPort = false;
208 int numPortMappings = json_object_array_length(portMappingArray);
209 for (int i = 0; i < numPortMappings; ++i)
210 {
211 json_object *portMapping = json_object_array_get_idx(portMappingArray, i);
212 json_object *o;
213
214 if (json_object_object_get_ex(portMapping, "to", &o) && ! strcmp(json_object_get_string(o), portName))
215 {
216 if (json_object_object_get_ex(portMapping, "map", &o))
217 oldPortName = json_object_get_string(o);
218
219 foundPort = true;
220 break;
221 }
222 }
223 if (! foundPort)
224 return false;
225
226 oldPortIdentifier = joinPortIdentifier(oldNodeIdentifier, oldPortName);
227 return true;
228}
229
233bool VuoCompositionDiff::isNodeBeingMovedToHere(const char *newCompositionIdentifier, const char *nodeIdentifier, json_object *replacementObj,
234 string &oldCompositionIdentifier)
235{
236 string moveFrom;
237 string moveTo;
238
239 json_object *o;
240 if (json_object_object_get_ex(replacementObj, "move", &o))
241 moveFrom = json_object_get_string(o);
242
243 if (json_object_object_get_ex(replacementObj, "to", &o))
244 moveTo = json_object_get_string(o);
245
246 if (moveTo == convertIdentifierToPath(newCompositionIdentifier, nodeIdentifier))
247 {
248 string unused;
249 convertPathToIdentifier(moveFrom.c_str(), oldCompositionIdentifier, unused);
250 return true;
251 }
252
253 return false;
254}
255
259bool VuoCompositionDiff::isPortBeingCopied(const char *portName, json_object *replacementObj,
260 string &destinationCompositionIdentifier, string &destinationPortIdentifier)
261{
262 json_object *portMappingArray = NULL;
263 {
264 json_object *o;
265 if (json_object_object_get_ex(replacementObj, "ports", &o))
266 portMappingArray = o;
267 }
268 if (! portMappingArray)
269 return false;
270
271 int numPortMappings = json_object_array_length(portMappingArray);
272 for (int i = 0; i < numPortMappings; ++i)
273 {
274 json_object *portMapping = json_object_array_get_idx(portMappingArray, i);
275 json_object *o;
276
277 if (json_object_object_get_ex(portMapping, "copy", &o) && ! strcmp(json_object_get_string(o), portName))
278 {
279 if (json_object_object_get_ex(portMapping, "to", &o))
280 {
281 const char *destinationPortPath = json_object_get_string(o);
282 convertPathToIdentifier(destinationPortPath, destinationCompositionIdentifier, destinationPortIdentifier);
283 }
284
285 return true;
286 }
287 }
288
289 return false;
290}
291
297{
298 return ! diff;
299}
300
301extern "C"
302{
306void vuoSetCompositionDiff(VuoCompositionState *compositionState, char *diff)
307{
310}
311
315bool vuoIsNodeBeingRemovedOrReplaced(VuoCompositionState *compositionState, const char *nodeIdentifier, json_object **replacementObj)
316{
319 nodeIdentifier, replacementObj);
320}
321
325bool vuoIsNodeBeingAddedOrReplaced(VuoCompositionState *compositionState, const char *nodeIdentifier, json_object **replacementObj)
326{
329 nodeIdentifier, replacementObj);
330}
331
332}