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


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Jun 8 2019 18:04:04