SMACC
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Private Attributes | List of all members
smacc::introspection::TypeInfo Class Reference

#include <string_type_walker.h>

Collaboration diagram for smacc::introspection::TypeInfo:
Collaboration graph

Public Types

typedef std::shared_ptr< TypeInfoPtr
 

Public Member Functions

 TypeInfo (std::string tkey, std::string codedtype, std::string finaltype)
 
std::string toString ()
 
std::string getNonTemplatedTypeName ()
 
const std::string & getFullName ()
 

Static Public Member Functions

static TypeInfo::Ptr getTypeInfoFromString (std::string inputtext)
 
static TypeInfo::Ptr getFromStdTypeInfo (const std::type_info &tid)
 
template<typename T >
static TypeInfo::Ptr getTypeInfoFromType ()
 

Public Attributes

std::vector< PtrtemplateParameters
 

Static Public Attributes

static std::map< std::string, PtrtypeInfoDatabase
 

Private Attributes

std::string tkey
 
std::string codedtype
 
std::string finaltype
 

Detailed Description

Definition at line 18 of file string_type_walker.h.

Member Typedef Documentation

◆ Ptr

Definition at line 21 of file string_type_walker.h.

Constructor & Destructor Documentation

◆ TypeInfo()

smacc::introspection::TypeInfo::TypeInfo ( std::string  tkey,
std::string  codedtype,
std::string  finaltype 
)
inline

Definition at line 36 of file string_type_walker.h.

References codedtype, finaltype, and tkey.

Member Function Documentation

◆ getFromStdTypeInfo()

TypeInfo::Ptr smacc::introspection::TypeInfo::getFromStdTypeInfo ( const std::type_info &  tid)
static

Definition at line 64 of file string_type_walker.cpp.

65{
67}
static TypeInfo::Ptr getTypeInfoFromString(std::string inputtext)
std::string demangleSymbol()
Definition: introspection.h:75

References smacc::introspection::demangleSymbol(), and getTypeInfoFromString().

Referenced by smacc::introspection::StateReactorHandler::addInputEvent(), smacc::utils::cleanShortTypeName(), smacc::ISmaccClient::getType(), and getTypeInfoFromType().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getFullName()

const std::string & smacc::introspection::TypeInfo::getFullName ( )
inline

Definition at line 55 of file string_type_walker.h.

56 {
57 return this->finaltype;
58 }

References finaltype.

◆ getNonTemplatedTypeName()

std::string smacc::introspection::TypeInfo::getNonTemplatedTypeName ( )
inline

Definition at line 48 of file string_type_walker.h.

49 {
50 auto index = this->finaltype.find("<");
51 return this->finaltype.substr(0, index);
52 }

References finaltype.

◆ getTypeInfoFromString()

TypeInfo::Ptr smacc::introspection::TypeInfo::getTypeInfoFromString ( std::string  inputtext)
static

Definition at line 69 of file string_type_walker.cpp.

70{
71 auto it = typeInfoDatabase.find(inputtext);
72
73 if (it != typeInfoDatabase.end())
74 return typeInfoDatabase[inputtext];
75
76 bool ok = false;
77 int typecount = 0;
78 std::string originalinputtext = inputtext;
79 std::map<std::string, std::string> typesdict;
80 std::map<std::string, std::string> typesdict_content;
81
82 while (!ok)
83 {
84 // simpletypeRE = r"[^<>,\s]+<[^<>]+>"
85 // print ("input: " + inputtext)
86
87 const char* simpletypeRE = "[^\\<\\>,\\s]+\\<[^\\<\\>]+\\>";
88 // std::cout << inputtext << std::endl;
89
90 // locate moste outer template
91 std::smatch matches;
92 std::regex regex(simpletypeRE); // matches words beginning by "sub"
93
94 // matches = [m for m in enumerate(re.finditer(simpletypeRE, inputtext))]
95 std::regex_search(inputtext, matches, regex);
96
97 // if len(matches) == 0:
98 // if len(typesdict) == 0:
99 // tkey = "$T" + str(typecount)
100 // typesdict[tkey] = inputtext
101 // break
102
103 if (matches.size() == 0)
104 {
105 if (typesdict.size() == 0)
106 {
107 auto tkey = "$T" + std::to_string(typecount);
108 typesdict[tkey] = inputtext;
109 }
110 break;
111 }
112
113 // for i, m in matches:
114 // tstr = m.group(0)
115 // tkey = "$T" + str(typecount)
116 // inputtext = inputtext.replace(tstr, tkey)
117 // print("updating input text: " + inputtext)
118 // typecount += 1
119 // typesdict[tkey] = tstr
120
121 // find and replace internal templates with tokens
122 int i = 0;
123 for (auto& m : matches)
124 {
125 std::string tstr = m;
126 auto tkey = "$T" + std::to_string(typecount);
127 replace(inputtext, tstr, tkey);
128
129 // std::cout << "updating input text:" << inputtext << std::endl;
130
131 typecount++;
132 typesdict[tkey] = tstr;
133
134 i++;
135 }
136 }
137
138 // allbasetypes = set()
139 // for tkey in typesdict.keys():
140 // flat = typesdict[tkey]
141 // print flat
142 // startindex = flat.index("<")
143 // flat = flat[startindex + 1:-1]
144 // basetypes = [t.strip() for t in flat.split(",")]
145 // for b in basetypes:
146 // if "$" not in b:
147 // allbasetypes.add(b)
148
149 // SORT by token key to replace back in a single pass
150 std::vector<std::pair<std::string, std::string>> orderedTypedict;
151 for (const auto& item : typesdict)
152 {
153 orderedTypedict.emplace_back(item);
154 }
155
156 std::sort(orderedTypedict.begin(), orderedTypedict.end(),
157 [](auto& a, auto& b) { return std::stoi(a.first.substr(2)) < std::stoi(b.first.substr(2)); });
158
159 std::set<std::string> allbasetypes;
160 for (auto& typeentry : orderedTypedict /*typesdict*/)
161 {
162 auto flat = typeentry.second;
163 // std::cout << flat << std::endl;
164
165 size_t startindex = flat.find("<");
166 size_t endindex = flat.find(">");
167 if (startindex != std::string::npos)
168 {
169 flat = flat.substr(startindex + 1,
170 endindex - startindex - 1); // gets the list of template parameters in csv format
171
172 // std::cout << typeentry.first <<":" << flat << std::endl;
173 typesdict_content[typeentry.first] = flat;
174 std::vector<std::string> localbasetypes;
175
176 std::istringstream iss(flat);
177 std::string token;
178 while (std::getline(iss, token, ','))
179 {
180 boost::trim(token);
181 localbasetypes.push_back(token);
182 // std::cout << "base type: " << token << std::endl;
183 }
184
185 for (auto& b : localbasetypes)
186 {
187 size_t found = b.find("$");
188
189 if (found == std::string::npos)
190 {
191 allbasetypes.insert(b);
192 }
193 }
194 }
195
196 // for b in allbasetypes:
197 // typesdict[b] = b
198
199 // refresh
200 for (auto& b : allbasetypes)
201 {
202 typesdict[b] = b;
203 }
204 }
205 // types = []
206 // for tkey in typesdict:
207 // finaltype = replace_back(typesdict[tkey], typesdict)
208 // t = TypeInfo(tkey, typesdict[tkey], finaltype)
209 // types.append(t)
210
211 // print t
212
213 // append leaf types
214 for (auto& b : allbasetypes)
215 {
216 orderedTypedict.push_back({ b, b });
217 }
218
219 // std::cout << "---------- TYPES -------" << std::endl;
220 std::vector<TypeInfo::Ptr> types;
221 std::vector<std::string> tokens;
222
223 for (auto t : orderedTypedict) // must be ordered. to avoid issue, ie: $11 to be replaced in $T14
224 {
225 // auto t = *it;
226 auto& tkey = t.first;
227 auto& tval = t.second;
228 auto finaltype = replace_back(tval, orderedTypedict);
229 auto tinfo = std::make_shared<TypeInfo>(tkey, tval, finaltype);
230 types.push_back(tinfo);
231 tokens.push_back(tkey);
232
233 // std::cout << "replacing back: " << finaltype << std::endl;
234 }
235
236 TypeInfo::Ptr roottype = nullptr;
237 for (auto& t : types)
238 {
239 // std::cout << t->finaltype << " vs " <<originalinputtext;
240 if (t->finaltype == originalinputtext)
241 {
242 roottype = t;
243 break;
244 }
245 }
246
247 // std::sort(types.begin(), types.end(),[](auto& a, auto& b)
248 // {
249 // return a->getFullName().size() > b->getFullName().size();
250 // });
251
252 /*
253 std::cout<<"types order:" << std::endl;
254 for(auto t: types)
255 {
256 std::cout<< t->codedtype << std::endl;
257 }
258 std::cout<<"---------" << std::endl;
259
260 std::cout<<"types order:" << std::endl;
261 for(auto t: types)
262 {
263 std::cout<< t->finaltype << std::endl;
264 }
265 std::cout<<"---------" << std::endl;
266 */
267
268 for (size_t i = 0; i < types.size(); i++)
269 {
270 auto t = types[i];
271 auto ttoken = tokens[i];
272
273 // auto t = types[it1];
274 std::vector<std::pair<int, TypeInfo::Ptr>> unorderedTemplateParameters;
275
276 // std::cout << "original typestr: " << codedtypecopy << std::endl;
277
278 // auto codedtypecopy = t->codedtype;
279 auto codedtypecopy = typesdict_content[ttoken];
280
281 // std::cout << "original typestr: " << codedtypecopy << std::endl;
282 // std::cout << "original token: " << ttoken << std::endl;
283 // std::cout << "original typestr: " << typesdict_content[ttoken] << std::endl;
284
285 // size_t startindex = codedtypecopy.find("<");
286 // size_t endindex = codedtypecopy.find(">");
287
288 // if (startindex != std::string::npos)
289 // {
290 // codedtypecopy = codedtypecopy.substr(startindex + 1, endindex - startindex - 1);
291 // }
292 // std::cout << "original typestr: " << codedtypecopy << std::endl;
293
294 for (auto& t2 : types)
295 // for (auto it2= types.size() -1; it2 >=0; it2--)
296 {
297 // std::cout << it2 << std::endl;
298 // auto t2 = types[it2];
299 // std::cout << t2->getFullName() << std::endl;
300 if (t == t2)
301 continue;
302
303 auto index = codedtypecopy.find(t2->tkey);
304 if (index != std::string::npos)
305 {
306 auto pair = std::make_pair(index, t2); // this line is important for the order of templates
307 // auto pair = std::make_pair(0, t2);
308 // std::cout << "matches: " << t2->tkey <<std::endl;
309 unorderedTemplateParameters.push_back(pair);
310 replace(codedtypecopy, t2->tkey, ""); // consume token
311 // std::cout << "codedtypecopy: " << codedtypecopy << std::endl;
312 }
313 }
314
315 std::sort(unorderedTemplateParameters.begin(), unorderedTemplateParameters.end(),
316 [](auto& a, auto& b) -> bool { return a.first <= b.first; });
317
318 ROS_DEBUG_STREAM("------------------");
319 ROS_DEBUG_STREAM("CREATING TYPE:" << t->getFullName());
320
321 for (auto& item : unorderedTemplateParameters)
322 {
323 ROS_DEBUG_STREAM(" - template parameter: " << item.second->getFullName());
324 t->templateParameters.push_back(item.second);
325 }
326 ROS_DEBUG_STREAM("------------------");
327 }
328
329 ROS_DEBUG_STREAM("ADDING TYPE TO DATABASE: " << inputtext);
330 ROS_DEBUG_STREAM("Current Database");
331 for (auto& en : typeInfoDatabase)
332 {
333 ROS_DEBUG_STREAM("- " << en.first);
334 }
335
336 typeInfoDatabase[originalinputtext] = roottype;
337 return roottype;
338}
std::shared_ptr< TypeInfo > Ptr
static std::map< std::string, Ptr > typeInfoDatabase
std::string replace_back(std::string roottype, std::vector< std::pair< std::string, std::string > > &orderedtypesdict)
bool replace(std::string &str, const std::string &from, const std::string &to)

References finaltype, smacc::introspection::replace(), smacc::introspection::replace_back(), tkey, and typeInfoDatabase.

Referenced by getFromStdTypeInfo().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getTypeInfoFromType()

template<typename T >
static TypeInfo::Ptr smacc::introspection::TypeInfo::getTypeInfoFromType ( )
inlinestatic

Definition at line 28 of file string_type_walker.h.

29 {
30 return TypeInfo::getFromStdTypeInfo(typeid(T));
31 }
static TypeInfo::Ptr getFromStdTypeInfo(const std::type_info &tid)

References getFromStdTypeInfo().

Here is the call graph for this function:

◆ toString()

std::string smacc::introspection::TypeInfo::toString ( )
inline

Definition at line 43 of file string_type_walker.h.

44 {
45 return this->tkey + ":" + this->finaltype;
46 }

References finaltype, and tkey.

Member Data Documentation

◆ codedtype

std::string smacc::introspection::TypeInfo::codedtype
private

Definition at line 64 of file string_type_walker.h.

Referenced by TypeInfo().

◆ finaltype

std::string smacc::introspection::TypeInfo::finaltype
private

◆ templateParameters

std::vector<Ptr> smacc::introspection::TypeInfo::templateParameters

Definition at line 34 of file string_type_walker.h.

◆ tkey

std::string smacc::introspection::TypeInfo::tkey
private

Definition at line 63 of file string_type_walker.h.

Referenced by getTypeInfoFromString(), toString(), and TypeInfo().

◆ typeInfoDatabase

std::map< std::string, TypeInfo::Ptr > smacc::introspection::TypeInfo::typeInfoDatabase
static

Definition at line 60 of file string_type_walker.h.

Referenced by getTypeInfoFromString().


The documentation for this class was generated from the following files: