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

#include <smacc_type_info.hpp>

Collaboration diagram for smacc2::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 32 of file smacc_type_info.hpp.

Member Typedef Documentation

◆ Ptr

Definition at line 35 of file smacc_type_info.hpp.

Constructor & Destructor Documentation

◆ TypeInfo()

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

Definition at line 50 of file smacc_type_info.hpp.

References codedtype, finaltype, and tkey.

Member Function Documentation

◆ getFromStdTypeInfo()

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

Definition at line 76 of file string_type_walker.cpp.

77{
79}
static TypeInfo::Ptr getTypeInfoFromString(std::string inputtext)
std::string demangleSymbol()

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

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

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

◆ getFullName()

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

Definition at line 65 of file smacc_type_info.hpp.

65{ return this->finaltype; }

References finaltype.

◆ getNonTemplatedTypeName()

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

Definition at line 59 of file smacc_type_info.hpp.

60 {
61 auto index = this->finaltype.find("<");
62 return this->finaltype.substr(0, index);
63 }

References finaltype.

◆ getTypeInfoFromString()

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

Definition at line 81 of file string_type_walker.cpp.

82{
83 auto it = typeInfoDatabase.find(inputtext);
84
85 if (it != typeInfoDatabase.end()) return typeInfoDatabase[inputtext];
86
87 bool ok = false;
88 int typecount = 0;
89 std::string originalinputtext = inputtext;
90 std::map<std::string, std::string> typesdict;
91 std::map<std::string, std::string> typesdict_content;
92
93 // ITERATE on the current string to replace internal template types with ids (ie $Tx) replacements
94 // until plain template type
95
96 while (!ok)
97 {
98 //simpletypeRE = r"[^<>,\s]+<[^<>]+>"
99 //print ("input: " + inputtext)
100
101 const char * simpletypeRE = "[^\\<\\>,\\s]+\\<[^\\<\\>]+\\>";
102 //std::cout << inputtext << std::endl;
103
104 // locate moste outer template
105 std::smatch matches;
106 std::regex regex(simpletypeRE); // matches words beginning by "sub"
107
108 std::regex_search(inputtext, matches, regex);
109
110 // checking ending condition
111 if (matches.size() == 0)
112 {
113 if (typesdict.size() == 0)
114 {
115 auto tkey = "$T" + std::to_string(typecount);
116 typesdict[tkey] = inputtext;
117 }
118 break;
119 }
120 else
121 {
122 // find and replace all coincidences typestr to tokenid
123 int i = 0;
124 for (auto & m : matches)
125 {
126 std::string tstr = m;
127 auto tkey = "$T" + std::to_string(typecount);
128 replace(inputtext, tstr, tkey);
129
130 typecount++;
131 typesdict[tkey] = tstr;
132
133 i++;
134 }
135 }
136 }
137
138 // ---------- SORT by token key to replace back in a single pass --------
139 std::vector<std::pair<std::string, std::string>> orderedTypedict;
140 for (const auto & item : typesdict)
141 {
142 orderedTypedict.emplace_back(item);
143 }
144
145 std::sort(
146 orderedTypedict.begin(), orderedTypedict.end(),
147 [](auto & a, auto & b) { return std::stoi(a.first.substr(2)) > std::stoi(b.first.substr(2)); });
148 // -----------------------------------------------------------------
149
150 std::set<std::string> allbasetypes;
151 for (auto & typeentry : orderedTypedict /*typesdict*/)
152 {
153 auto flat = typeentry.second;
154
155 size_t startindex = flat.find("<");
156 size_t endindex = flat.find(">");
157 if (startindex != std::string::npos)
158 {
159 flat = flat.substr(
160 startindex + 1,
161 endindex - startindex - 1); // gets the list of template parameters in csv format
162
163 typesdict_content[typeentry.first] = flat;
164 std::vector<std::string> localbasetypes;
165
166 std::istringstream iss(flat);
167 std::string token;
168 while (std::getline(iss, token, ','))
169 {
170 boost::trim(token);
171 localbasetypes.push_back(token);
172 }
173
174 for (auto & b : localbasetypes)
175 {
176 size_t found = b.find("$");
177
178 if (found == std::string::npos)
179 {
180 allbasetypes.insert(b);
181 }
182 }
183 }
184
185 // for b in allbasetypes:
186 // typesdict[b] = b
187
188 //refresh
189 for (auto & b : allbasetypes)
190 {
191 typesdict[b] = b;
192 }
193 }
194 // types = []
195 // for tkey in typesdict:
196 // finaltype = replace_back(typesdict[tkey], typesdict)
197 // t = TypeInfo(tkey, typesdict[tkey], finaltype)
198 // types.append(t)
199
200 // print t
201
202 // append leaf types
203 for (auto & b : allbasetypes)
204 {
205 orderedTypedict.push_back({b, b});
206 }
207
208 //std::cout << "---------- TYPES -------" << std::endl;
209 std::vector<TypeInfo::Ptr> types;
210 std::vector<std::string> tokens;
211
212 for (auto t : orderedTypedict) // must be ordered. to avoid issue, ie: $11 to be replaced in $T14
213 {
214 //auto t = *it;
215 auto & tkey = t.first;
216 auto & tval = t.second;
217 auto finaltype = replace_back(tval, orderedTypedict);
218 auto tinfo = std::make_shared<TypeInfo>(tkey, tval, finaltype);
219 types.push_back(tinfo);
220 tokens.push_back(tkey);
221
222 //std::cout << "replacing back: " << finaltype << std::endl;
223 }
224
225 // --------- FINDING THE CURRENT TYPE -------------
226 TypeInfo::Ptr roottype = nullptr;
227 for (auto & t : types)
228 {
229 // std::cout << t->finaltype << " vs " <<originalinputtext;
230 if (t->finaltype == originalinputtext)
231 {
232 roottype = t;
233 break;
234 }
235 }
236
237 if (roottype == nullptr && globalNh_ != nullptr)
238 {
239 std::stringstream ss;
240 ss << "---------------" << std::endl;
241 ss << "TYPE STRING WALKER, type was not properly interpreted: " << std::endl
242 << " - " << inputtext.c_str() << std::endl
243 << " - " << originalinputtext << std::endl;
244
245 auto strmsg = ss.str();
246 RCLCPP_WARN_STREAM(globalNh_->get_logger(), strmsg);
247 }
248
249 // std::sort(types.begin(), types.end(),[](auto& a, auto& b)
250 // {
251 // return a->getFullName().size() > b->getFullName().size();
252 // });
253
254 /*
255 std::cout<<"types order:" << std::endl;
256 for(auto t: types)
257 {
258 std::cout<< t->codedtype << std::endl;
259 }
260 std::cout<<"---------" << std::endl;
261
262 std::cout<<"types order:" << std::endl;
263 for(auto t: types)
264 {
265 std::cout<< t->finaltype << std::endl;
266 }
267 std::cout<<"---------" << std::endl;
268 */
269
270 // Replacing back tokens in strings to get the whole final name
271 for (size_t i = 0; i < types.size(); i++)
272 {
273 auto t = types[i];
274 auto ttoken = tokens[i];
275
276 //auto t = types[it1];
277 std::vector<std::pair<int, TypeInfo::Ptr>> unorderedTemplateParameters;
278
279 //std::cout << "original typestr: " << codedtypecopy << std::endl;
280
281 //auto codedtypecopy = t->codedtype;
282 auto codedtypecopy = typesdict_content[ttoken];
283
284 // std::cout << "original typestr: " << codedtypecopy << std::endl;
285 // std::cout << "original token: " << ttoken << std::endl;
286 // std::cout << "original typestr: " << typesdict_content[ttoken] << std::endl;
287
288 // size_t startindex = codedtypecopy.find("<");
289 // size_t endindex = codedtypecopy.find(">");
290
291 // if (startindex != std::string::npos)
292 // {
293 // codedtypecopy = codedtypecopy.substr(startindex + 1, endindex - startindex - 1);
294 // }
295 //std::cout << "original typestr: " << codedtypecopy << std::endl;
296
297 for (auto & t2 : types)
298 //for (auto it2= types.size() -1; it2 >=0; it2--)
299 {
300 //std::cout << it2 << std::endl;
301 //auto t2 = types[it2];
302 //std::cout << t2->getFullName() << std::endl;
303 if (t == t2) continue;
304
305 auto index = codedtypecopy.find(t2->tkey);
306 if (index != std::string::npos)
307 {
308 auto pair = std::make_pair(index, t2); // this line is important for the order of templates
309 //auto pair = std::make_pair(0, t2);
310 //std::cout << "matches: " << t2->tkey <<std::endl;
311 unorderedTemplateParameters.push_back(pair);
312 replace(codedtypecopy, t2->tkey, ""); // consume token
313 //std::cout << "codedtypecopy: " << codedtypecopy << std::endl;
314 }
315 }
316
317 std::sort(
318 unorderedTemplateParameters.begin(), unorderedTemplateParameters.end(),
319 [](auto & a, auto & b) -> bool { return a.first <= b.first; });
320
321 //RCLCPP_DEBUG_STREAM(nh_->get_logger(),"------------------");
322 //RCLCPP_DEBUG_STREAM(nh_->get_logger(),"CREATING TYPE:" << t->getFullName());
323
324 for (auto & item : unorderedTemplateParameters)
325 {
326 //RCLCPP_DEBUG_STREAM(nh_->get_logger()," - template parameter: " << item.second->getFullName());
327 t->templateParameters.push_back(item.second);
328 }
329 //RCLCPP_DEBUG_STREAM(nh_->get_logger(),"------------------");
330 }
331
332 //RCLCPP_DEBUG_STREAM(nh_->get_logger(),"ADDING TYPE TO DATABASE: " << inputtext);
333 //RCLCPP_DEBUG_STREAM(nh_->get_logger(),"Current Database");
334 for (auto & en : typeInfoDatabase)
335 {
336 if (globalNh_ != nullptr) RCLCPP_DEBUG_STREAM(globalNh_->get_logger(), "- " << en.first);
337 }
338
339 typeInfoDatabase[originalinputtext] = roottype;
340
341 if (roottype == nullptr && globalNh_ != nullptr)
342 {
343 std::stringstream ss;
344 ss << "---------------" << std::endl;
345 ss << "TYPE STRING WALKER, type was not properly interpreted: " << std::endl
346 << " - " << inputtext.c_str() << std::endl
347 << " - " << originalinputtext << std::endl;
348
349 ss << "---- current type (ordered types)" << std::endl;
350
351 for (auto & en : orderedTypedict)
352 {
353 ss << "- " << en.first << ": " << en.second << std::endl;
354 }
355 ss << "---------------" << std::endl;
356
357 ss << "------current type types --------" << std::endl;
358
359 for (auto & t : types)
360 {
361 ss << t->finaltype << std::endl;
362 }
363
364 ss << "----total typeinfo database" << std::endl;
365
366 for (auto & en : typeInfoDatabase)
367 {
368 ss << "- " << en.first << ": " << en.second << std::endl;
369 }
370
371 auto strmsg = ss.str();
372 RCLCPP_WARN_STREAM(globalNh_->get_logger(), strmsg);
373 }
374
375 return roottype;
376}
std::shared_ptr< TypeInfo > Ptr
static std::map< std::string, Ptr > typeInfoDatabase
rclcpp::Node::SharedPtr globalNh_
Definition: reflection.cpp:28
bool replace(std::string &str, const std::string &from, const std::string &to)
std::string replace_back(std::string roottype, std::vector< std::pair< std::string, std::string > > &orderedtypesdict)

References finaltype, smacc2::introspection::globalNh_, smacc2::introspection::replace(), smacc2::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 smacc2::introspection::TypeInfo::getTypeInfoFromType ( )
inlinestatic

Definition at line 42 of file smacc_type_info.hpp.

43 {
44 return TypeInfo::getFromStdTypeInfo(typeid(T));
45 }
static TypeInfo::Ptr getFromStdTypeInfo(const std::type_info &tid)

References getFromStdTypeInfo().

Referenced by smacc2::introspection::processTransition().

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

◆ toString()

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

Definition at line 57 of file smacc_type_info.hpp.

57{ return this->tkey + ":" + this->finaltype; }

References finaltype, and tkey.

Member Data Documentation

◆ codedtype

std::string smacc2::introspection::TypeInfo::codedtype
private

Definition at line 71 of file smacc_type_info.hpp.

Referenced by TypeInfo().

◆ finaltype

std::string smacc2::introspection::TypeInfo::finaltype
private

◆ templateParameters

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

Definition at line 48 of file smacc_type_info.hpp.

◆ tkey

std::string smacc2::introspection::TypeInfo::tkey
private

Definition at line 70 of file smacc_type_info.hpp.

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

◆ typeInfoDatabase

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

Definition at line 67 of file smacc_type_info.hpp.

Referenced by getTypeInfoFromString().


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