basic_types.cpp
Go to the documentation of this file.
2 #include <cstdlib>
3 #include <cstring>
4 #include <clocale>
5 
6 namespace BT
7 {
8 
9 template <>
10 std::string toStr<NodeStatus>(NodeStatus status)
11 {
12  switch (status)
13  {
15  return "SUCCESS";
17  return "FAILURE";
19  return "RUNNING";
20  case NodeStatus::IDLE:
21  return "IDLE";
22  }
23  return "";
24 }
25 
26 std::string toStr(std::string value)
27 {
28  return value;
29 }
30 
31 std::string toStr(NodeStatus status, bool colored)
32 {
33  if (!colored)
34  {
35  return toStr(status);
36  }
37  else
38  {
39  switch (status)
40  {
42  return "\x1b[32m"
43  "SUCCESS"
44  "\x1b[0m"; // RED
46  return "\x1b[31m"
47  "FAILURE"
48  "\x1b[0m"; // GREEN
50  return "\x1b[33m"
51  "RUNNING"
52  "\x1b[0m"; // YELLOW
53  case NodeStatus::IDLE:
54  return "\x1b[36m"
55  "IDLE"
56  "\x1b[0m"; // CYAN
57  }
58  }
59  return "Undefined";
60 }
61 
62 
63 
64 template <>
65 std::string toStr<PortDirection>(PortDirection direction)
66 {
67  switch(direction)
68  {
69  case PortDirection::INPUT: return "Input";
70  case PortDirection::OUTPUT: return "Output";
71  case PortDirection::INOUT: return "InOut";
72  }
73  return "InOut";
74 }
75 
76 
77 template<> std::string toStr<NodeType>(NodeType type)
78 {
79  switch (type)
80  {
81  case NodeType::ACTION:
82  return "Action";
84  return "Condition";
86  return "Decorator";
87  case NodeType::CONTROL:
88  return "Control";
89  case NodeType::SUBTREE:
90  return "SubTree";
91  default:
92  return "Undefined";
93  }
94 }
95 
96 
97 template <>
98 std::string convertFromString<std::string>(StringView str)
99 {
100  return std::string( str.data(), str.size() );
101 }
102 
103 
104 template <>
106 {
107  return std::stoi(str.data());
108 }
109 
110 template <>
112 {
113  return std::stol(str.data());
114 }
115 
116 template <>
118 {
119  return unsigned(std::stoul(str.data()));
120 }
121 
122 template <>
124 {
125  return std::stoul(str.data());
126 }
127 
128 template <>
130 {
131  // see issue #120
132  // http://quick-bench.com/DWaXRWnxtxvwIMvZy2DxVPEKJnE
133 
134  std::string old_locale = setlocale(LC_NUMERIC,nullptr);
135  setlocale(LC_NUMERIC,"C");
136  double val = std::stod(str.data());
137  setlocale(LC_NUMERIC, old_locale.c_str());
138  return val;
139 }
140 
141 template <>
143 {
144  std::string old_locale = setlocale(LC_NUMERIC,nullptr);
145  setlocale(LC_NUMERIC,"C");
146  float val = std::stof(str.data());
147  setlocale(LC_NUMERIC, old_locale.c_str());
148  return val;
149 }
150 
151 template <>
152 std::vector<int> convertFromString<std::vector<int>>(StringView str)
153 {
154  auto parts = splitString(str, ';');
155  std::vector<int> output;
156  output.reserve( parts.size() );
157  for(const StringView& part: parts)
158  {
159  char* end;
160  output.push_back( std::strtol( part.data(), &end, 10 ) );
161  }
162  return output;
163 }
164 
165 template <>
166 std::vector<double> convertFromString<std::vector<double>>(StringView str)
167 {
168  auto parts = splitString(str, ';');
169  std::vector<double> output;
170  output.reserve( parts.size() );
171  for(const StringView& part: parts)
172  {
173  char* end;
174  output.push_back( std::strtod( part.data(), &end ) );
175  }
176  return output;
177 }
178 
179 template <>
181 {
182  if (str.size() == 1)
183  {
184  if (str[0] == '0')
185  {
186  return false;
187  }
188  if (str[0] == '1')
189  {
190  return true;
191  }
192  }
193  else if (str.size() == 4)
194  {
195  if (str == "true" || str == "TRUE" || str == "True")
196  {
197  return true;
198  }
199  }
200  else if (str.size() == 5)
201  {
202  if (str == "false" || str == "FALSE" || str == "False")
203  {
204  return false;
205  }
206  }
207  throw RuntimeError("convertFromString(): invalid bool conversion");
208 }
209 
210 template <>
212 {
213  if( str == "IDLE" ) return NodeStatus::IDLE;
214  if( str == "RUNNING" ) return NodeStatus::RUNNING;
215  if( str == "SUCCESS" ) return NodeStatus::SUCCESS;
216  if( str == "FAILURE" ) return NodeStatus::FAILURE;
217  throw RuntimeError(std::string("Cannot convert this to NodeStatus: ") + static_cast<std::string>(str) );
218 }
219 
220 template <>
222 {
223  if( str == "Action" ) return NodeType::ACTION;
224  if( str == "Condition" ) return NodeType::CONDITION;
225  if( str == "Control" ) return NodeType::CONTROL;
226  if( str == "Decorator" ) return NodeType::DECORATOR;
227  if( str == "SubTree" || str == "SubTreePlus" ) return NodeType::SUBTREE;
228  return NodeType::UNDEFINED;
229 }
230 
231 template <>
233 {
234  if( str == "Input" || str == "INPUT" ) return PortDirection::INPUT;
235  if( str == "Output" || str == "OUTPUT") return PortDirection::OUTPUT;
236  return PortDirection::INOUT;
237 }
238 
239 
240 std::ostream& operator<<(std::ostream& os, const NodeType& type)
241 {
242  os << toStr(type);
243  return os;
244 }
245 
246 std::ostream& operator<<(std::ostream& os, const NodeStatus& status)
247 {
248  os << toStr(status);
249  return os;
250 }
251 
252 std::ostream& operator<<(std::ostream& os, const PortDirection& type)
253 {
254  os << toStr(type);
255  return os;
256 }
257 
258 std::vector<StringView> splitString(const StringView &strToSplit, char delimeter)
259 {
260  std::vector<StringView> splitted_strings;
261  splitted_strings.reserve(4);
262 
263  size_t pos = 0;
264  while( pos < strToSplit.size())
265  {
266  size_t new_pos = strToSplit.find_first_of(delimeter, pos);
267  if( new_pos == std::string::npos)
268  {
269  new_pos = strToSplit.size();
270  }
271  StringView sv = { &strToSplit.data()[pos], new_pos - pos };
272  splitted_strings.push_back( sv );
273  pos = new_pos + 1;
274  }
275  return splitted_strings;
276 }
277 
279 {
280  return _type;
281 }
282 
283 const std::type_info* PortInfo::type() const
284 {
285  return _info;
286 }
287 
288 Any PortInfo::parseString(const char *str) const
289 {
290  if( _converter)
291  {
292  return _converter(str);
293  }
294  return {};
295 }
296 
297 Any PortInfo::parseString(const std::string &str) const
298 {
299  if( _converter)
300  {
301  return _converter(str);
302  }
303  return {};
304 }
305 
307 {
308  description_ = static_cast<std::string>(description);
309 }
310 
311 void PortInfo::setDefaultValue(StringView default_value_as_string)
312 {
313  default_value_ = static_cast<std::string>(default_value_as_string);
314 }
315 
316 const std::string &PortInfo::description() const
317 {
318  return description_;
319 }
320 
321 const std::string &PortInfo::defaultValue() const
322 {
323  return default_value_;
324 }
325 
326 
327 
328 } // end namespace
NodeStatus convertFromString< NodeStatus >(StringView str)
PortDirection convertFromString< PortDirection >(StringView str)
StringConverter _converter
Definition: basic_types.h:254
float convertFromString< float >(StringView str)
const std::type_info * _info
Definition: basic_types.h:253
PortDirection direction() const
std::string toStr(T value)
Definition: basic_types.h:136
std::string default_value_
Definition: basic_types.h:256
std::string description_
Definition: basic_types.h:255
const std::string & description() const
std::string toStr< NodeType >(NodeType type)
Definition: basic_types.cpp:77
std::string toStr< NodeStatus >(NodeStatus status)
Definition: basic_types.cpp:10
long convertFromString< long >(StringView str)
std::ostream & operator<<(std::ostream &os, const BT::NodeStatus &status)
const std::type_info * type() const
Any parseString(const char *str) const
std::vector< StringView > splitString(const StringView &strToSplit, char delimeter)
const std::string & defaultValue() const
PortDirection _type
Definition: basic_types.h:252
nonstd::string_view StringView
Definition: basic_types.h:50
void setDefaultValue(StringView default_value_as_string)
unsigned convertFromString< unsigned >(StringView str)
NodeType convertFromString< NodeType >(StringView str)
bool convertFromString< bool >(StringView str)
int convertFromString< int >(StringView str)
NodeStatus
Definition: basic_types.h:35
void setDescription(StringView description)
std::string toStr< PortDirection >(PortDirection direction)
Definition: basic_types.cpp:65
NodeType
Enumerates the possible types of nodes.
Definition: basic_types.h:22
PortDirection
Definition: basic_types.h:43
double convertFromString< double >(StringView str)
unsigned long convertFromString< unsigned long >(StringView str)


behaviotree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Tue May 4 2021 02:56:24