Vuo 2.4.4
Loading...
Searching...
No Matches
VuoGenericType.cc
Go to the documentation of this file.
1
10#include "VuoGenericType.hh"
11#include "VuoStringUtilities.hh"
12#include <sstream>
13
15const string VuoGenericType::genericTypeNamePrefix = "VuoGenericType";
16
20VuoGenericType::VuoGenericType(string typeName, vector<string> compatibleSpecializedTypes)
21 : VuoType(typeName)
22{
23 this->compatibleSpecializedTypes = compatibleSpecializedTypes;
24
25 string compatiblesDescription;
26 Compatibility compatibility;
27 vector<string> compatibles = getCompatibleSpecializedTypes(compatibility);
28 if (compatibility == anyType)
29 compatiblesDescription = "any type";
30 else if (compatibility == anyListType)
31 compatiblesDescription = "any list type";
32 else
33 {
34 for (size_t i = 0; i < compatibles.size(); ++i)
35 {
36 compatiblesDescription += compatibles[i];
37 if (compatibles.size() >= 3)
38 {
39 if (i < compatibles.size() - 2)
40 compatiblesDescription += ", ";
41 else if (i == compatibles.size() - 2)
42 compatiblesDescription += ", or ";
43 }
44 else if (compatibles.size() == 2 && i == 0)
45 compatiblesDescription += " or ";
46 }
47 }
48 string suffix = extractSuffixStringFromGenericTypeName(typeName);
49 string typeDescription = "Generic #" + suffix + (VuoType::isListTypeName(typeName) ? " List" : "");
50 setDefaultTitle( "(" + typeDescription + " — can connect to " + compatiblesDescription + ")" );
51}
52
57{
58 Compatibility compatibility;
59 vector<string> compatibles = getCompatibleSpecializedTypes(compatibility);
60
61 if ((compatibility == anyType && ! VuoType::isListTypeName(typeName)) ||
62 (compatibility == anyListType && VuoType::isListTypeName(typeName)))
63 return true;
64
65 return find(compatibles.cbegin(), compatibles.cend(), typeName) != compatibles.cend();
66}
67
72{
73 Compatibility thisCompatibility;
74 vector<string> thisCompatibles = getCompatibleSpecializedTypes(thisCompatibility);
75 Compatibility otherCompatibility;
76 vector<string> otherCompatibles = otherType->getCompatibleSpecializedTypes(otherCompatibility);
77
78 if ((thisCompatibility == anyType && ! VuoType::isListTypeName(otherType->getModuleKey())) ||
79 (otherCompatibility == anyType && ! VuoType::isListTypeName(getModuleKey())) ||
80 (thisCompatibility == anyListType && VuoType::isListTypeName(otherType->getModuleKey())) ||
81 (otherCompatibility == anyListType && VuoType::isListTypeName(getModuleKey())))
82 return true;
83
84 std::sort(thisCompatibles.begin(), thisCompatibles.end());
85 std::sort(otherCompatibles.begin(), otherCompatibles.end());
86
87 set<string> bothCompatibles;
88 set_intersection(thisCompatibles.begin(), thisCompatibles.end(),
89 otherCompatibles.begin(), otherCompatibles.end(),
90 std::insert_iterator<set<string> >(bothCompatibles, bothCompatibles.begin() ));
91 return (! bothCompatibles.empty());
92}
93
104{
105 compatibility = (compatibleSpecializedTypes.empty() ?
108 anyType) :
110
111 return compatibleSpecializedTypes;
112}
113
118{
119 string genericTypeName;
120 findGenericTypeName(typeName, 0, genericTypeName);
121 return genericTypeName == typeName;
122}
123
127vector<string> VuoGenericType::findGenericTypeNames(const string &stringToSearch)
128{
129 vector<string> genericTypeNames;
130
131 size_t pos = 0;
132 string genericTypeName;
133 while ((pos = VuoGenericType::findGenericTypeName(stringToSearch, pos, genericTypeName)) != string::npos)
134 {
135 pos += genericTypeName.length();
136 genericTypeName = VuoType::extractInnermostTypeName(genericTypeName);
137 if (find(genericTypeNames.begin(), genericTypeNames.end(), genericTypeName) == genericTypeNames.end())
138 genericTypeNames.push_back(genericTypeName);
139 }
140
141 return genericTypeNames;
142}
143
154size_t VuoGenericType::findGenericTypeName(string stringToSearch, size_t startPos, string &genericTypeName)
155{
156 size_t genericStartPos = startPos;
157
158 while (true)
159 {
160 genericStartPos = stringToSearch.find(genericTypeNamePrefix, genericStartPos);
161 if (genericStartPos == string::npos)
162 {
163 genericTypeName = "";
164 break;
165 }
166
167 string suffix;
168 for (size_t i = genericStartPos + genericTypeNamePrefix.length(); i < stringToSearch.length(); ++i)
169 {
170 if ('0' <= stringToSearch[i] && stringToSearch[i] <= '9')
171 suffix += stringToSearch[i];
172 else
173 break;
174 }
175
176 if (! suffix.empty())
177 {
178 string listPrefix;
179 if (genericStartPos >= VuoType::listTypeNamePrefix.length())
180 {
181 size_t listPrefixStartPos = genericStartPos - VuoType::listTypeNamePrefix.length();
182 if (stringToSearch.substr(listPrefixStartPos, VuoType::listTypeNamePrefix.length()) == VuoType::listTypeNamePrefix)
183 {
184 listPrefix = VuoType::listTypeNamePrefix;
185 genericStartPos = listPrefixStartPos;
186 }
187 }
188
189 genericTypeName = listPrefix + genericTypeNamePrefix + suffix;
190 break;
191 }
192
193 genericStartPos += genericTypeNamePrefix.length();
194 }
195
196 return genericStartPos;
197}
198
204string VuoGenericType::replaceInnermostGenericTypeName(string genericTypeName, string replacementTypeName)
205{
206 if (! isGenericTypeName(genericTypeName))
207 return genericTypeName;
208
209 string typeName = genericTypeName;
210 string innermostTypeName = VuoType::extractInnermostTypeName(genericTypeName);
211 size_t innermostTypeNamePos = typeName.find(innermostTypeName);
212 typeName.replace(innermostTypeNamePos, innermostTypeName.length(), replacementTypeName);
213 return typeName;
214}
215
227bool VuoGenericType::replaceGenericTypeNamesInString(string &stringToSearch, const map<string, string> &specializedForGenericTypeName,
228 vector<string> &orderedGenericTypeNames)
229{
230 if (orderedGenericTypeNames.empty())
231 {
232 std::transform(specializedForGenericTypeName.begin(), specializedForGenericTypeName.end(),
233 std::inserter(orderedGenericTypeNames, orderedGenericTypeNames.end()),
234 [](const pair<string, string> &p) { return p.first; });
235
236 std::sort(orderedGenericTypeNames.begin(), orderedGenericTypeNames.end(),
237 [](const string &name1, const string &name2) { return name1.length() > name2.length(); });
238 }
239
240 size_t replacementCount = 0;
241 for (const string &genericTypeName : orderedGenericTypeNames)
242 {
243 auto specializedTypeNameIter = specializedForGenericTypeName.find(genericTypeName);
244 if (specializedTypeNameIter != specializedForGenericTypeName.end())
245 replacementCount += VuoStringUtilities::replaceAll(stringToSearch, genericTypeName, specializedTypeNameIter->second);
246 }
247
248 return replacementCount > 0;
249}
250
254string VuoGenericType::createGenericTypeName(unsigned int suffix)
255{
256 ostringstream oss;
257 oss << genericTypeNamePrefix << suffix;
258 return oss.str();
259}
260
264void VuoGenericType::sortGenericTypeNames(vector<string> &genericTypeNames)
265{
266 sort(genericTypeNames.begin(), genericTypeNames.end(), isPairOfGenericTypesSorted);
267}
268
272bool VuoGenericType::isPairOfGenericTypesSorted(string type1, string type2)
273{
274 unsigned int suffix1 = extractSuffixFromGenericTypeName(type1);
275 unsigned int suffix2 = extractSuffixFromGenericTypeName(type2);
276 return (suffix1 < suffix2);
277}
278
282string VuoGenericType::extractSuffixStringFromGenericTypeName(string genericTypeName)
283{
284 if (! isGenericTypeName(genericTypeName))
285 return "";
286
287 size_t prefixStartPos = genericTypeName.find(genericTypeNamePrefix);
288 size_t suffixStartPos = prefixStartPos + genericTypeNamePrefix.length();
289 return genericTypeName.substr(suffixStartPos);
290}
291
295unsigned int VuoGenericType::extractSuffixFromGenericTypeName(string genericTypeName)
296{
297 string suffixStr = extractSuffixStringFromGenericTypeName(genericTypeName);
298 unsigned int suffix = 0;
299 istringstream iss(suffixStr);
300 iss >> suffix;
301 return suffix;
302}