Vuo  2.4.0
VuoGenericType.cc
Go to the documentation of this file.
1
10#include "VuoGenericType.hh"
11#include <sstream>
12
14const string VuoGenericType::genericTypeNamePrefix = "VuoGenericType";
15
19VuoGenericType::VuoGenericType(string typeName, vector<string> compatibleSpecializedTypes)
20 : VuoType(typeName)
21{
22 this->compatibleSpecializedTypes = compatibleSpecializedTypes;
23
24 string compatiblesDescription;
25 Compatibility compatibility;
26 vector<string> compatibles = getCompatibleSpecializedTypes(compatibility);
27 if (compatibility == anyType)
28 compatiblesDescription = "any type";
29 else if (compatibility == anyListType)
30 compatiblesDescription = "any list type";
31 else
32 {
33 for (size_t i = 0; i < compatibles.size(); ++i)
34 {
35 compatiblesDescription += compatibles[i];
36 if (compatibles.size() >= 3)
37 {
38 if (i < compatibles.size() - 2)
39 compatiblesDescription += ", ";
40 else if (i == compatibles.size() - 2)
41 compatiblesDescription += ", or ";
42 }
43 else if (compatibles.size() == 2 && i == 0)
44 compatiblesDescription += " or ";
45 }
46 }
47 string suffix = extractSuffixStringFromGenericTypeName(typeName);
48 string typeDescription = (VuoType::isListTypeName(typeName) ? "list of " : string()) + "generic #" + suffix;
49 setDefaultTitle( "(" + typeDescription + " — can connect to " + compatiblesDescription + ")" );
50}
51
56{
57 Compatibility compatibility;
58 vector<string> compatibles = getCompatibleSpecializedTypes(compatibility);
59
60 if (compatibility == anyType ||
61 (compatibility == anyListType && VuoType::isListTypeName(typeName)))
62 return true;
63
64 return find(compatibles.begin(), compatibles.end(), typeName) != compatibles.end();
65}
66
71{
72 Compatibility thisCompatibility;
73 vector<string> thisCompatibles = getCompatibleSpecializedTypes(thisCompatibility);
74 Compatibility otherCompatibility;
75 vector<string> otherCompatibles = otherType->getCompatibleSpecializedTypes(otherCompatibility);
76
77 if (thisCompatibility == anyType || otherCompatibility == anyType ||
78 (thisCompatibility == anyListType && VuoType::isListTypeName(otherType->getModuleKey())) ||
79 (otherCompatibility == anyListType && VuoType::isListTypeName(getModuleKey())))
80 return true;
81
82 std::sort(thisCompatibles.begin(), thisCompatibles.end());
83 std::sort(otherCompatibles.begin(), otherCompatibles.end());
84
85 set<string> bothCompatibles;
86 set_intersection(thisCompatibles.begin(), thisCompatibles.end(),
87 otherCompatibles.begin(), otherCompatibles.end(),
88 std::insert_iterator<set<string> >(bothCompatibles, bothCompatibles.begin() ));
89 return (! bothCompatibles.empty());
90}
91
102{
103 compatibility = (compatibleSpecializedTypes.empty() ?
106 anyType) :
108
109 return compatibleSpecializedTypes;
110}
111
116{
117 string genericTypeName;
118 findGenericTypeName(typeName, 0, genericTypeName);
119 return genericTypeName == typeName;
120}
121
132size_t VuoGenericType::findGenericTypeName(string stringToSearch, size_t startPos, string &genericTypeName)
133{
134 size_t genericStartPos = startPos;
135
136 while (true)
137 {
138 genericStartPos = stringToSearch.find(genericTypeNamePrefix, genericStartPos);
139 if (genericStartPos == string::npos)
140 {
141 genericTypeName = "";
142 break;
143 }
144
145 string suffix;
146 for (size_t i = genericStartPos + genericTypeNamePrefix.length(); i < stringToSearch.length(); ++i)
147 {
148 if ('0' <= stringToSearch[i] && stringToSearch[i] <= '9')
149 suffix += stringToSearch[i];
150 else
151 break;
152 }
153
154 if (! suffix.empty())
155 {
156 string listPrefix;
157 if (genericStartPos >= VuoType::listTypeNamePrefix.length())
158 {
159 size_t listPrefixStartPos = genericStartPos - VuoType::listTypeNamePrefix.length();
160 if (stringToSearch.substr(listPrefixStartPos, VuoType::listTypeNamePrefix.length()) == VuoType::listTypeNamePrefix)
161 {
162 listPrefix = VuoType::listTypeNamePrefix;
163 genericStartPos = listPrefixStartPos;
164 }
165 }
166
167 genericTypeName = listPrefix + genericTypeNamePrefix + suffix;
168 break;
169 }
170
171 genericStartPos += genericTypeNamePrefix.length();
172 }
173
174 return genericStartPos;
175}
176
182string VuoGenericType::replaceInnermostGenericTypeName(string genericTypeName, string replacementTypeName)
183{
184 if (! isGenericTypeName(genericTypeName))
185 return genericTypeName;
186
187 string typeName = genericTypeName;
188 string innermostTypeName = VuoType::extractInnermostTypeName(genericTypeName);
189 size_t innermostTypeNamePos = typeName.find(innermostTypeName);
190 typeName.replace(innermostTypeNamePos, innermostTypeName.length(), replacementTypeName);
191 return typeName;
192}
193
197string VuoGenericType::createGenericTypeName(unsigned int suffix)
198{
199 ostringstream oss;
200 oss << genericTypeNamePrefix << suffix;
201 return oss.str();
202}
203
207void VuoGenericType::sortGenericTypeNames(vector<string> &genericTypeNames)
208{
209 sort(genericTypeNames.begin(), genericTypeNames.end(), isPairOfGenericTypesSorted);
210}
211
215bool VuoGenericType::isPairOfGenericTypesSorted(string type1, string type2)
216{
217 unsigned int suffix1 = extractSuffixFromGenericTypeName(type1);
218 unsigned int suffix2 = extractSuffixFromGenericTypeName(type2);
219 return (suffix1 < suffix2);
220}
221
225string VuoGenericType::extractSuffixStringFromGenericTypeName(string genericTypeName)
226{
227 if (! isGenericTypeName(genericTypeName))
228 return "";
229
230 size_t prefixStartPos = genericTypeName.find(genericTypeNamePrefix);
231 size_t suffixStartPos = prefixStartPos + genericTypeNamePrefix.length();
232 return genericTypeName.substr(suffixStartPos);
233}
234
238unsigned int VuoGenericType::extractSuffixFromGenericTypeName(string genericTypeName)
239{
240 string suffixStr = extractSuffixStringFromGenericTypeName(genericTypeName);
241 unsigned int suffix = 0;
242 istringstream iss(suffixStr);
243 iss >> suffix;
244 return suffix;
245}