basic_types.cpp
Go to the documentation of this file.
3 
4 #include <cstdlib>
5 #include <cstring>
6 #include <clocale>
7 #include <charconv>
8 
9 namespace BT
10 {
11 template <>
12 std::string toStr<NodeStatus>(const NodeStatus& status)
13 {
14  switch(status)
15  {
17  return "SUCCESS";
19  return "FAILURE";
21  return "RUNNING";
22  case NodeStatus::IDLE:
23  return "IDLE";
25  return "SKIPPED";
26  }
27  return "";
28 }
29 
30 template <>
31 [[nodiscard]] std::string toStr<bool>(const bool& value)
32 {
33  return value ? "true" : "false";
34 }
35 
36 template <>
37 std::string toStr<std::string>(const std::string& value)
38 {
39  return value;
40 }
41 
42 std::string toStr(NodeStatus status, bool colored)
43 {
44  if(!colored)
45  {
46  return toStr(status);
47  }
48  else
49  {
50  switch(status)
51  {
53  return "\x1b[32m"
54  "SUCCESS"
55  "\x1b[0m"; // RED
57  return "\x1b[31m"
58  "FAILURE"
59  "\x1b[0m"; // GREEN
61  return "\x1b[33m"
62  "RUNNING"
63  "\x1b[0m"; // YELLOW
65  return "\x1b[34m"
66  "SKIPPED"
67  "\x1b[0m"; // BLUE
68  case NodeStatus::IDLE:
69  return "\x1b[36m"
70  "IDLE"
71  "\x1b[0m"; // CYAN
72  }
73  }
74  return "Undefined";
75 }
76 
77 template <>
78 std::string toStr<PortDirection>(const PortDirection& direction)
79 {
80  switch(direction)
81  {
83  return "Input";
85  return "Output";
87  return "InOut";
88  }
89  return "InOut";
90 }
91 
92 template <>
93 std::string toStr<NodeType>(const NodeType& type)
94 {
95  switch(type)
96  {
97  case NodeType::ACTION:
98  return "Action";
100  return "Condition";
101  case NodeType::DECORATOR:
102  return "Decorator";
103  case NodeType::CONTROL:
104  return "Control";
105  case NodeType::SUBTREE:
106  return "SubTree";
107  default:
108  return "Undefined";
109  }
110 }
111 
112 template <>
113 std::string convertFromString<std::string>(StringView str)
114 {
115  return std::string(str.data(), str.size());
116 }
117 
118 template <>
120 {
121  long result = 0;
122  auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), result);
123  if(ec != std::errc())
124  {
125  throw RuntimeError(StrCat("Can't convert string [", str, "] to integer"));
126  }
127  return result;
128 }
129 
130 template <>
132 {
133  unsigned long result = 0;
134  auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), result);
135  if(ec != std::errc())
136  {
137  throw RuntimeError(StrCat("Can't convert string [", str, "] to integer"));
138  }
139  return result;
140 }
141 
142 template <typename T>
144 {
145  auto res = convertFromString<int64_t>(str);
146  if(res < std::numeric_limits<T>::lowest() || res > std::numeric_limits<T>::max())
147  {
148  throw RuntimeError(
149  StrCat("Value out of bound when converting [", str, "] to integer"));
150  }
151  return res;
152 }
153 
154 template <>
156 {
157  return ConvertWithBoundCheck<int8_t>(str);
158 }
159 
160 template <>
162 {
163  return ConvertWithBoundCheck<int16_t>(str);
164 }
165 
166 template <>
168 {
169  return ConvertWithBoundCheck<int32_t>(str);
170 }
171 
172 template <>
174 {
175  return ConvertWithBoundCheck<uint8_t>(str);
176 }
177 
178 template <>
180 {
181  return ConvertWithBoundCheck<uint16_t>(str);
182 }
183 
184 template <>
186 {
187  return ConvertWithBoundCheck<uint32_t>(str);
188 }
189 
190 template <>
192 {
193  // see issue #120
194  // http://quick-bench.com/DWaXRWnxtxvwIMvZy2DxVPEKJnE
195 
196  std::string old_locale = setlocale(LC_NUMERIC, nullptr);
197  setlocale(LC_NUMERIC, "C");
198  double val = std::stod(str.data());
199  setlocale(LC_NUMERIC, old_locale.c_str());
200  return val;
201 }
202 
203 template <>
205 {
206  std::string old_locale = setlocale(LC_NUMERIC, nullptr);
207  setlocale(LC_NUMERIC, "C");
208  float val = std::stof(str.data());
209  setlocale(LC_NUMERIC, old_locale.c_str());
210  return val;
211 }
212 
213 template <>
214 std::vector<int> convertFromString<std::vector<int>>(StringView str)
215 {
216  auto parts = splitString(str, ';');
217  std::vector<int> output;
218  output.reserve(parts.size());
219  for(const StringView& part : parts)
220  {
221  output.push_back(convertFromString<int>(part));
222  }
223  return output;
224 }
225 
226 template <>
227 std::vector<double> convertFromString<std::vector<double>>(StringView str)
228 {
229  auto parts = splitString(str, ';');
230  std::vector<double> output;
231  output.reserve(parts.size());
232  for(const StringView& part : parts)
233  {
234  output.push_back(convertFromString<double>(part));
235  }
236  return output;
237 }
238 
239 template <>
240 std::vector<std::string> convertFromString<std::vector<std::string>>(StringView str)
241 {
242  auto parts = splitString(str, ';');
243  std::vector<std::string> output;
244  output.reserve(parts.size());
245  for(const StringView& part : parts)
246  {
247  output.push_back(convertFromString<std::string>(part));
248  }
249  return output;
250 }
251 
252 template <>
254 {
255  if(str.size() == 1)
256  {
257  if(str[0] == '0')
258  {
259  return false;
260  }
261  if(str[0] == '1')
262  {
263  return true;
264  }
265  }
266  else if(str.size() == 4)
267  {
268  if(str == "true" || str == "TRUE" || str == "True")
269  {
270  return true;
271  }
272  }
273  else if(str.size() == 5)
274  {
275  if(str == "false" || str == "FALSE" || str == "False")
276  {
277  return false;
278  }
279  }
280  throw RuntimeError("convertFromString(): invalid bool conversion");
281 }
282 
283 template <>
285 {
286  if(str == "IDLE")
287  return NodeStatus::IDLE;
288  if(str == "RUNNING")
289  return NodeStatus::RUNNING;
290  if(str == "SUCCESS")
291  return NodeStatus::SUCCESS;
292  if(str == "FAILURE")
293  return NodeStatus::FAILURE;
294  if(str == "SKIPPED")
295  return NodeStatus::SKIPPED;
296 
297  throw RuntimeError(std::string("Cannot convert this to NodeStatus: ") +
298  static_cast<std::string>(str));
299 }
300 
301 template <>
303 {
304  if(str == "Action")
305  return NodeType::ACTION;
306  if(str == "Condition")
307  return NodeType::CONDITION;
308  if(str == "Control")
309  return NodeType::CONTROL;
310  if(str == "Decorator")
311  return NodeType::DECORATOR;
312  if(str == "SubTree")
313  return NodeType::SUBTREE;
314  return NodeType::UNDEFINED;
315 }
316 
317 template <>
319 {
320  if(str == "Input" || str == "INPUT")
321  return PortDirection::INPUT;
322  if(str == "Output" || str == "OUTPUT")
323  return PortDirection::OUTPUT;
324  if(str == "InOut" || str == "INOUT")
325  return PortDirection::INOUT;
326  throw RuntimeError(std::string("Cannot convert this to PortDirection: ") +
327  static_cast<std::string>(str));
328 }
329 
330 std::ostream& operator<<(std::ostream& os, const NodeType& type)
331 {
332  os << toStr(type);
333  return os;
334 }
335 
336 std::ostream& operator<<(std::ostream& os, const NodeStatus& status)
337 {
338  os << toStr(status);
339  return os;
340 }
341 
342 std::ostream& operator<<(std::ostream& os, const PortDirection& type)
343 {
344  os << toStr(type);
345  return os;
346 }
347 
348 std::vector<StringView> splitString(const StringView& strToSplit, char delimeter)
349 {
350  std::vector<StringView> splitted_strings;
351  splitted_strings.reserve(4);
352 
353  size_t pos = 0;
354  while(pos < strToSplit.size())
355  {
356  size_t new_pos = strToSplit.find_first_of(delimeter, pos);
357  if(new_pos == std::string::npos)
358  {
359  new_pos = strToSplit.size();
360  }
361  const auto sv = StringView{ &strToSplit.data()[pos], new_pos - pos };
362  splitted_strings.push_back(sv);
363  pos = new_pos + 1;
364  }
365  return splitted_strings;
366 }
367 
369 {
370  return direction_;
371 }
372 
373 const std::type_index& TypeInfo::type() const
374 {
375  return type_info_;
376 }
377 
378 const std::string& TypeInfo::typeName() const
379 {
380  return type_str_;
381 }
382 
383 Any TypeInfo::parseString(const char* str) const
384 {
385  if(converter_)
386  {
387  return converter_(str);
388  }
389  return {};
390 }
391 
392 Any TypeInfo::parseString(const std::string& str) const
393 {
394  if(converter_)
395  {
396  return converter_(str);
397  }
398  return {};
399 }
400 
402 {
403  description_ = static_cast<std::string>(description);
404 }
405 
406 const std::string& PortInfo::description() const
407 {
408  return description_;
409 }
410 
412 {
413  return default_value_;
414 }
415 
416 const std::string& PortInfo::defaultValueString() const
417 {
418  return default_value_str_;
419 }
420 
422 {
423  if(str == "_autoremap")
424  {
425  return true;
426  }
427  if(str.empty())
428  {
429  return false;
430  }
431  const char first_char = str.data()[0];
432  if(!std::isalpha(first_char))
433  {
434  return false;
435  }
436  if(str == "name" || str == "ID")
437  {
438  return false;
439  }
440  return true;
441 }
442 
443 Any convertFromJSON(StringView json_text, std::type_index type)
444 {
446  auto res = JsonExporter::get().fromJson(json, type);
447  if(!res)
448  {
449  throw std::runtime_error(res.error());
450  }
451  return res->first;
452 }
453 
455 {
457  if(JsonExporter::get().toJson(value, json))
458  {
459  return StrCat("json:", json.dump());
460  }
461  return nonstd::make_unexpected("toJsonString failed");
462 }
463 
464 bool StartWith(StringView str, StringView prefix)
465 {
466  return str.size() >= prefix.size() &&
467  strncmp(str.data(), prefix.data(), prefix.size()) == 0;
468 }
469 
470 bool StartWith(StringView str, char prefix)
471 {
472  return str.size() >= 1 && str[0] == prefix;
473 }
474 
475 } // namespace BT
BT
Definition: ex01_wrap_legacy.cpp:29
BT::PortInfo::setDescription
void setDescription(StringView description)
Definition: basic_types.cpp:401
BT::TypeInfo::typeName
const std::string & typeName() const
Definition: basic_types.cpp:378
BT::convertFromString< int32_t >
int32_t convertFromString< int32_t >(StringView str)
Definition: basic_types.cpp:167
BT::convertFromString< uint64_t >
uint64_t convertFromString< uint64_t >(StringView str)
Definition: basic_types.cpp:131
BT::PortDirection::INOUT
@ INOUT
BT::NodeType
NodeType
Enumerates the possible types of nodes.
Definition: basic_types.h:20
BT::Any
Definition: safe_any.hpp:36
BT::toJsonString
Expected< std::string > toJsonString(const Any &value)
Definition: basic_types.cpp:454
BT::toStr< bool >
std::string toStr< bool >(const bool &value)
Definition: basic_types.cpp:31
BT::PortInfo::defaultValueString
const std::string & defaultValueString() const
Definition: basic_types.cpp:416
BT::PortInfo::defaultValue
const Any & defaultValue() const
Definition: basic_types.cpp:411
BT::NodeType::DECORATOR
@ DECORATOR
basic_types.h
BT::NodeType::CONDITION
@ CONDITION
BT::Expected
nonstd::expected< T, std::string > Expected
Definition: basic_types.h:85
BT::StringView
std::string_view StringView
Definition: basic_types.h:59
BT::NodeType::SUBTREE
@ SUBTREE
basic_json
namespace for Niels Lohmann
Definition: json.hpp:3411
BT::TypeInfo::type_str_
std::string type_str_
Definition: basic_types.h:389
BT::TypeInfo::converter_
StringConverter converter_
Definition: basic_types.h:388
BT::PortDirection::OUTPUT
@ OUTPUT
BT::convertFromString< uint16_t >
uint16_t convertFromString< uint16_t >(StringView str)
Definition: basic_types.cpp:179
BT::TypeInfo::type_info_
std::type_index type_info_
Definition: basic_types.h:387
BT::convertFromString< int64_t >
int64_t convertFromString< int64_t >(StringView str)
Definition: basic_types.cpp:119
BT::PortDirection
PortDirection
Definition: basic_types.h:52
BT::convertFromString< float >
float convertFromString< float >(StringView str)
Definition: basic_types.cpp:204
BT::toStr< NodeType >
std::string toStr< NodeType >(const NodeType &type)
Definition: basic_types.cpp:93
json_text
static const char * json_text
Definition: gtest_substitution.cpp:6
BT::NodeType::UNDEFINED
@ UNDEFINED
BT::NodeStatus::FAILURE
@ FAILURE
lexy::parse
constexpr auto parse(const Input &input, const ErrorCallback &callback)
Parses the production into a value, invoking the callback on error.
Definition: parse.hpp:171
BT::convertFromString< int8_t >
int8_t convertFromString< int8_t >(StringView str)
Definition: basic_types.cpp:155
BT::convertFromJSON
Any convertFromJSON(StringView json_text, std::type_index type)
convertFromJSON will parse a json string and use JsonExporter to convert its content to a given type....
Definition: basic_types.cpp:443
BT::NodeStatus::SKIPPED
@ SKIPPED
BT::NodeType::ACTION
@ ACTION
BT::PortInfo::description_
std::string description_
Definition: basic_types.h:427
BT::convertFromString< bool >
bool convertFromString< bool >(StringView str)
Definition: basic_types.cpp:253
BT::operator<<
std::ostream & operator<<(std::ostream &os, const BT::NodeStatus &status)
Definition: basic_types.cpp:336
BT::RuntimeError
Definition: exceptions.h:58
json_export.h
BT::splitString
std::vector< StringView > splitString(const StringView &strToSplit, char delimeter)
Definition: basic_types.cpp:348
BT::convertFromString< NodeType >
NodeType convertFromString< NodeType >(StringView str)
Definition: basic_types.cpp:302
BT::JsonExporter::get
static JsonExporter & get()
Definition: json_export.cpp:6
BT::convertFromString< uint32_t >
uint32_t convertFromString< uint32_t >(StringView str)
Definition: basic_types.cpp:185
BT::PortDirection::INPUT
@ INPUT
BT::TypeInfo::type
const std::type_index & type() const
Definition: basic_types.cpp:373
BT::IsAllowedPortName
bool IsAllowedPortName(StringView str)
Definition: basic_types.cpp:421
BT::StrCat
std::string StrCat()
Definition: strcat.hpp:46
BT::PortInfo::direction
PortDirection direction() const
Definition: basic_types.cpp:368
BT::NodeStatus::SUCCESS
@ SUCCESS
BT::NodeStatus::RUNNING
@ RUNNING
BT::PortInfo::default_value_
Any default_value_
Definition: basic_types.h:428
BT::StartWith
bool StartWith(StringView str, StringView prefix)
Definition: basic_types.cpp:464
BT::NodeType::CONTROL
@ CONTROL
BT::PortInfo::direction_
PortDirection direction_
Definition: basic_types.h:426
json
basic_json<> json
default specialization
Definition: json.hpp:3422
BT::NodeStatus::IDLE
@ IDLE
BT::toStr< PortDirection >
std::string toStr< PortDirection >(const PortDirection &direction)
Definition: basic_types.cpp:78
BT::convertFromString< double >
double convertFromString< double >(StringView str)
Definition: basic_types.cpp:191
basic_json::dump
string_t dump(const int indent=-1, const char indent_char=' ', const bool ensure_ascii=false, const error_handler_t error_handler=error_handler_t::strict) const
serialization
Definition: json.hpp:20574
BT::PortInfo::default_value_str_
std::string default_value_str_
Definition: basic_types.h:429
BT::ConvertWithBoundCheck
T ConvertWithBoundCheck(StringView str)
Definition: basic_types.cpp:143
BT::convertFromString< PortDirection >
PortDirection convertFromString< PortDirection >(StringView str)
Definition: basic_types.cpp:318
BT::JsonExporter::fromJson
ExpectedEntry fromJson(const nlohmann::json &source) const
fromJson will return an Entry (value wrappedn in Any + TypeInfo) from a json source....
Definition: json_export.cpp:48
BT::convertFromString< NodeStatus >
NodeStatus convertFromString< NodeStatus >(StringView str)
Definition: basic_types.cpp:284
BT::TypeInfo::parseString
Any parseString(const char *str) const
Definition: basic_types.cpp:383
BT::toStr< NodeStatus >
std::string toStr< NodeStatus >(const NodeStatus &status)
Definition: basic_types.cpp:12
BT::convertFromString< int16_t >
int16_t convertFromString< int16_t >(StringView str)
Definition: basic_types.cpp:161
BT::PortInfo::description
const std::string & description() const
Definition: basic_types.cpp:406
BT::NodeStatus
NodeStatus
Definition: basic_types.h:33
BT::toStr
std::string toStr(const T &value)
toStr is the reverse operation of convertFromString.
Definition: basic_types.h:252
BT::convertFromString< uint8_t >
uint8_t convertFromString< uint8_t >(StringView str)
Definition: basic_types.cpp:173


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:07