Vuo  2.4.1
VuoCompilerPublishedNodeClass.cc
Go to the documentation of this file.
1
10#include <sstream>
11
12#include "VuoCompiler.hh"
18#include "VuoCompilerType.hh"
19#include "VuoGenericType.hh"
20#include "VuoNode.hh"
21#include "VuoNodeClass.hh"
22#include "VuoPublishedPort.hh"
23#include "VuoStringUtilities.hh"
24#include "VuoType.hh"
25
30 VuoCompilerSpecializedNodeClass(nodeClassName, module)
31{
32}
33
38 VuoCompilerSpecializedNodeClass(compilerNodeClass)
39{
40}
41
47{
48}
49
54VuoNodeClass * VuoCompilerPublishedNodeClass::newNodeClass(const string &nodeClassName, VuoCompiler *compiler, dispatch_queue_t llvmQueue, VuoCompilerPublishedNodeClass *singleton)
55{
56 vector<string> portNames;
57 vector<string> typeNames;
58 bool parsedOk = singleton->parseNodeClassName(nodeClassName, portNames, typeNames);
59 if (! parsedOk)
60 return NULL;
61
62 vector<VuoType *> types;
63 for (string typeName : typeNames)
64 {
65 VuoCompilerType *type = (typeName == "event" ? nullptr : compiler->getType(typeName));
66 types.push_back(type ? type->getBase() : nullptr);
67 }
68
69 return newNodeClass(portNames, types, llvmQueue, singleton);
70}
71
78VuoNodeClass * VuoCompilerPublishedNodeClass::newNodeClass(const vector<VuoPublishedPort *> &publishedPorts, dispatch_queue_t llvmQueue, VuoCompilerPublishedNodeClass *singleton)
79{
80 vector<string> portNames = singleton->formUniquePortNames(publishedPorts);
81
82 vector<VuoType *> types;
83 for (VuoPublishedPort *publishedPort : publishedPorts)
84 {
85 VuoType *type = static_cast<VuoCompilerPort *>(publishedPort->getCompiler())->getDataVuoType();
86 types.push_back(type);
87 }
88
89 return newNodeClass(portNames, types, llvmQueue, singleton);
90}
91
95VuoNodeClass * VuoCompilerPublishedNodeClass::newNodeClass(const vector<string> &portNames, const vector<VuoType *> &types, dispatch_queue_t llvmQueue, VuoCompilerPublishedNodeClass *singleton)
96{
97 string nodeClassName = singleton->buildNodeClassName(portNames, types);
98
99 bool isFullySpecialized = true;
100 for (VuoType *type : types)
101 {
102 if (dynamic_cast<VuoGenericType *>(type))
103 {
104 isFullySpecialized = false;
105 break;
106 }
107 }
108
109 __block VuoNodeClass *nodeClass;
111 {
112 dispatch_sync(llvmQueue, ^{
113 nodeClass = singleton->newNodeClassWithImplementation(nodeClassName, portNames, types);
114 });
115 }
116 else
117 {
118 nodeClass = singleton->newNodeClassWithoutImplementation(nodeClassName, portNames, types);
119 }
120
121 return nodeClass;
122}
123
128bool VuoCompilerPublishedNodeClass::parseNodeClassName(string nodeClassName, vector<string> &portNames, vector<string> &typeNames)
129{
130 string countAndNamesAndTypesStr = VuoStringUtilities::substrAfter(nodeClassName, getNodeClassNamePrefix() + ".");
131 if (countAndNamesAndTypesStr.empty())
132 return false;
133
134 vector<string> countAndNamesAndTypes = VuoStringUtilities::split(countAndNamesAndTypesStr, '.');
135 if (countAndNamesAndTypes.empty())
136 return false;
137
138 string countStr = countAndNamesAndTypes[0];
139 int count = atoi(countStr.c_str());
140 if (count != (countAndNamesAndTypes.size() - 1) / 2)
141 return false;
142
143 for (int i = 0; i < count; ++i)
144 {
145 string portName = countAndNamesAndTypes.at(1 + i);
146 portNames.push_back(portName);
147
148 string typeName = countAndNamesAndTypes.at(1 + count + i);
149 typeNames.push_back(typeName);
150 }
151
152 return true;
153}
154
158string VuoCompilerPublishedNodeClass::buildNodeClassName(const vector<string> &portNames, const vector<VuoType *> &types)
159{
160 vector<string> nodeClassNameParts;
161 nodeClassNameParts.push_back(getNodeClassNamePrefix());
162
163 ostringstream oss;
164 oss << portNames.size();
165 nodeClassNameParts.push_back(oss.str());
166
167 for (string portName : portNames)
168 nodeClassNameParts.push_back(portName);
169
170 for (VuoType *type : types)
171 nodeClassNameParts.push_back(type ? type->getModuleKey() : "event");
172
173 return VuoStringUtilities::join(nodeClassNameParts, '.');
174}
175
179string VuoCompilerPublishedNodeClass::buildNodeClassNameFromPorts(const vector<VuoPublishedPort *> &publishedPorts)
180{
181 vector<string> portNames = formUniquePortNames(publishedPorts);
182
183 vector<VuoType *> types;
184 for (VuoPublishedPort *publishedPort : publishedPorts)
185 {
186 VuoType *type = static_cast<VuoCompilerPort *>(publishedPort->getCompiler())->getDataVuoType();
187 types.push_back(type);
188 }
189
190 return buildNodeClassName(portNames, types);
191}
192
199vector<string> VuoCompilerPublishedNodeClass::formUniquePortNames(const vector<VuoPublishedPort *> &publishedPorts)
200{
201 set<string> takenPortNames = getReservedPortNames();
202
203 vector<string> portNames;
204 for (VuoPublishedPort *publishedPort : publishedPorts)
205 {
206 string uniquePortName = VuoStringUtilities::formUniqueIdentifier(takenPortNames, publishedPort->getClass()->getName());
207 portNames.push_back(uniquePortName);
208 }
209
210 return portNames;
211}
212
217{
218 vector<string> portNames;
219 vector<string> typeNames;
220 bool ok = parseNodeClassName(backingNodeClassName, portNames, typeNames);
221 if (! ok)
222 return NULL;
223
224 vector<VuoPublishedPort *> publishedPorts;
225 for (int i = 0; i < portNames.size(); ++i)
226 {
227 string portName = portNames[i];
228 string typeName = typeNames[i];
229 VuoType *type = (typeName == "event" ? NULL : compiler->getType(typeName)->getBase());
230 VuoCompilerPublishedPort *publishedPort = VuoCompilerPublishedPort::newPort(portName, type);
231 publishedPorts.push_back( static_cast<VuoPublishedPort *>(publishedPort->getBase()) );
232 }
233
234 return (dynamic_cast<VuoCompilerPublishedInputNodeClass *>(this) ?
235 compiler->createPublishedInputNode(publishedPorts)->getCompiler() :
236 compiler->createPublishedOutputNode(publishedPorts)->getCompiler());
237}
238
243{
245 return NULL;
246}
247
252{
253 return getNodeClassNamePrefix();
254}
255
260{
261 return "";
262}
263
268{
269 return NULL;
270}
271
275string VuoCompilerPublishedNodeClass::createUnspecializedNodeClassName(set<VuoPortClass *> portClassesToUnspecialize)
276{
278 return "";
279}
280
285string VuoCompilerPublishedNodeClass::createSpecializedNodeClassNameWithReplacement(string genericTypeName, string specializedTypeName)
286{
288 return "";
289}