24#include <boost/algorithm/string.hpp>
25#include <boost/algorithm/string/find_iterator.hpp>
26#include <boost/algorithm/string/trim.hpp>
37namespace introspection
39bool replace(std::string & str,
const std::string & from,
const std::string & to)
41 size_t start_pos = str.find(from);
42 if (start_pos == std::string::npos)
return false;
43 str.replace(start_pos, from.length(), to);
48 std::string roottype, std::vector<std::pair<std::string, std::string>> & orderedtypesdict)
50 while (roottype.find(
"$") != std::string::npos)
52 for (
auto & t : orderedtypesdict)
54 auto & tkey = t.first;
55 auto & tval = t.second;
89 std::string originalinputtext = inputtext;
90 std::map<std::string, std::string> typesdict;
91 std::map<std::string, std::string> typesdict_content;
101 const char * simpletypeRE =
"[^\\<\\>,\\s]+\\<[^\\<\\>]+\\>";
106 std::regex regex(simpletypeRE);
108 std::regex_search(inputtext, matches, regex);
111 if (matches.size() == 0)
113 if (typesdict.size() == 0)
115 auto tkey =
"$T" + std::to_string(typecount);
116 typesdict[
tkey] = inputtext;
124 for (
auto & m : matches)
126 std::string tstr = m;
127 auto tkey =
"$T" + std::to_string(typecount);
131 typesdict[
tkey] = tstr;
139 std::vector<std::pair<std::string, std::string>> orderedTypedict;
140 for (
const auto & item : typesdict)
142 orderedTypedict.emplace_back(item);
146 orderedTypedict.begin(), orderedTypedict.end(),
147 [](
auto & a,
auto & b) { return std::stoi(a.first.substr(2)) > std::stoi(b.first.substr(2)); });
150 std::set<std::string> allbasetypes;
151 for (
auto & typeentry : orderedTypedict )
153 auto flat = typeentry.second;
155 size_t startindex = flat.find(
"<");
156 size_t endindex = flat.find(
">");
157 if (startindex != std::string::npos)
161 endindex - startindex - 1);
163 typesdict_content[typeentry.first] = flat;
164 std::vector<std::string> localbasetypes;
166 std::istringstream iss(flat);
168 while (std::getline(iss, token,
','))
171 localbasetypes.push_back(token);
174 for (
auto & b : localbasetypes)
176 size_t found = b.find(
"$");
178 if (found == std::string::npos)
180 allbasetypes.insert(b);
189 for (
auto & b : allbasetypes)
203 for (
auto & b : allbasetypes)
205 orderedTypedict.push_back({b, b});
209 std::vector<TypeInfo::Ptr> types;
210 std::vector<std::string> tokens;
212 for (
auto t : orderedTypedict)
215 auto &
tkey = t.first;
216 auto & tval = t.second;
218 auto tinfo = std::make_shared<TypeInfo>(
tkey, tval,
finaltype);
219 types.push_back(tinfo);
220 tokens.push_back(
tkey);
227 for (
auto & t : types)
230 if (t->finaltype == originalinputtext)
237 if (roottype ==
nullptr &&
globalNh_ !=
nullptr)
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;
245 auto strmsg = ss.str();
246 RCLCPP_WARN_STREAM(
globalNh_->get_logger(), strmsg);
271 for (
size_t i = 0; i < types.size(); i++)
274 auto ttoken = tokens[i];
277 std::vector<std::pair<int, TypeInfo::Ptr>> unorderedTemplateParameters;
282 auto codedtypecopy = typesdict_content[ttoken];
297 for (
auto & t2 : types)
303 if (t == t2)
continue;
305 auto index = codedtypecopy.find(t2->tkey);
306 if (index != std::string::npos)
308 auto pair = std::make_pair(index, t2);
311 unorderedTemplateParameters.push_back(pair);
312 replace(codedtypecopy, t2->tkey,
"");
318 unorderedTemplateParameters.begin(), unorderedTemplateParameters.end(),
319 [](
auto & a,
auto & b) ->
bool { return a.first <= b.first; });
324 for (
auto & item : unorderedTemplateParameters)
327 t->templateParameters.push_back(item.second);
336 if (
globalNh_ !=
nullptr) RCLCPP_DEBUG_STREAM(
globalNh_->get_logger(),
"- " << en.first);
341 if (roottype ==
nullptr &&
globalNh_ !=
nullptr)
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;
349 ss <<
"---- current type (ordered types)" << std::endl;
351 for (
auto & en : orderedTypedict)
353 ss <<
"- " << en.first <<
": " << en.second << std::endl;
355 ss <<
"---------------" << std::endl;
357 ss <<
"------current type types --------" << std::endl;
359 for (
auto & t : types)
361 ss << t->finaltype << std::endl;
364 ss <<
"----total typeinfo database" << std::endl;
368 ss <<
"- " << en.first <<
": " << en.second << std::endl;
371 auto strmsg = ss.str();
372 RCLCPP_WARN_STREAM(
globalNh_->get_logger(), strmsg);
static TypeInfo::Ptr getTypeInfoFromString(std::string inputtext)
std::shared_ptr< TypeInfo > Ptr
static TypeInfo::Ptr getFromStdTypeInfo(const std::type_info &tid)
static std::map< std::string, Ptr > typeInfoDatabase
rclcpp::Node::SharedPtr globalNh_
bool replace(std::string &str, const std::string &from, const std::string &to)
std::string demangleSymbol()
std::string replace_back(std::string roottype, std::vector< std::pair< std::string, std::string > > &orderedtypesdict)