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


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Wed Apr 16 2025 02:20:55