basic_types.cpp
Go to the documentation of this file.
2 #include <cstdlib>
3 #include <cstring>
4 
5 namespace BT
6 {
7 const char* toStr(const NodeStatus& status, bool colored)
8 {
9  if (!colored)
10  {
11  switch (status)
12  {
14  return "SUCCESS";
16  return "FAILURE";
18  return "RUNNING";
19  case NodeStatus::IDLE:
20  return "IDLE";
21  }
22  }
23  else
24  {
25  switch (status)
26  {
28  return ("\x1b[32m"
29  "SUCCESS"
30  "\x1b[0m"); // RED
32  return ("\x1b[31m"
33  "FAILURE"
34  "\x1b[0m"); // GREEN
36  return ("\x1b[33m"
37  "RUNNING"
38  "\x1b[0m"); // YELLOW
39  case NodeStatus::IDLE:
40  return ("\x1b[36m"
41  "IDLE"
42  "\x1b[0m"); // CYAN
43  }
44  }
45  return "Undefined";
46 }
47 
48 const char* toStr(const NodeType& type)
49 {
50  switch (type)
51  {
52  case NodeType::ACTION:
53  return "Action";
55  return "Condition";
57  return "Decorator";
58  case NodeType::CONTROL:
59  return "Control";
60  case NodeType::SUBTREE:
61  return "SubTree";
62  default:
63  return "Undefined";
64  }
65 }
66 
67 template <>
68 std::string convertFromString<std::string>(const StringView& str)
69 {
70  return std::string( str.data(), str.size() );
71 }
72 
73 template <>
75 {
76  return str.to_string().c_str();
77 }
78 
79 template <>
81 {
82  return std::stoi(str.data());
83 }
84 
85 template <>
87 {
88  return std::stoul(str.data());
89 }
90 
91 template <>
93 {
94  return std::stod(str.data());
95 }
96 
97 template <>
98 std::vector<int> convertFromString<std::vector<int>>(const StringView& str)
99 {
100  auto parts = splitString(str, ';');
101  std::vector<int> output;
102  output.reserve( parts.size() );
103  for(const StringView& part: parts)
104  {
105  char* end;
106  output.push_back( std::strtol( part.data(), &end, 10 ) );
107  }
108  return output;
109 }
110 
111 template <>
112 std::vector<double> convertFromString<std::vector<double>>(const StringView& str)
113 {
114  auto parts = splitString(str, ';');
115  std::vector<double> output;
116  output.reserve( parts.size() );
117  for(const StringView& part: parts)
118  {
119  char* end;
120  output.push_back( std::strtod( part.data(), &end ) );
121  }
122  return output;
123 }
124 
125 template <>
127 {
128  if (str.size() == 1)
129  {
130  if (str[0] == '0')
131  {
132  return false;
133  }
134  else if (str[0] == '1')
135  {
136  return true;
137  }
138  else
139  {
140  std::runtime_error("invalid bool conversion");
141  }
142  }
143  else if (str.size() == 4)
144  {
145  if (str == "true" || str == "TRUE" || str == "True")
146  {
147  return true;
148  }
149  else
150  {
151  std::runtime_error("invalid bool conversion");
152  }
153  }
154  else if (str.size() == 5)
155  {
156  if (str == "false" || str == "FALSE" || str == "False")
157  {
158  return false;
159  }
160  else
161  {
162  std::runtime_error("invalid bool conversion");
163  }
164  }
165 
166  std::runtime_error("invalid bool conversion");
167  return false;
168 }
169 
170 template <>
172 {
173  for (auto status :
175  {
176  if ( StringView(toStr(status)) == str )
177  {
178  return status;
179  }
180  }
181  throw std::invalid_argument(std::string("Cannot convert this to NodeStatus: ") + str.to_string() );
182 }
183 
184 template <>
186 {
189  {
190  if (StringView(toStr(status)) == str)
191  {
192  return status;
193  }
194  }
195  throw std::invalid_argument(std::string("Cannot convert this to NodeType: ") + str.to_string());
196 }
197 
198 std::ostream& operator<<(std::ostream& os, const NodeType& type)
199 {
200  os << toStr(type);
201  return os;
202 }
203 
204 std::ostream& operator<<(std::ostream& os, const NodeStatus& status)
205 {
206  os << toStr(status);
207  return os;
208 }
209 
210 std::vector<StringView> splitString(const StringView &strToSplit, char delimeter)
211 {
212  std::vector<StringView> splitted_strings;
213  splitted_strings.reserve(4);
214 
215  size_t pos = 0;
216  while( pos < strToSplit.size())
217  {
218  size_t new_pos = strToSplit.find_first_of(delimeter, pos);
219  if( new_pos == std::string::npos)
220  {
221  new_pos = strToSplit.size();
222  }
223  StringView sv = { &strToSplit.data()[pos], new_pos - pos };
224  splitted_strings.push_back( sv );
225  pos = new_pos + 1;
226  }
227  return splitted_strings;
228 }
229 
230 } // end namespace
double convertFromString< double >(const StringView &str)
Definition: basic_types.cpp:92
NodeType convertFromString< NodeType >(const StringView &str)
std::ostream & operator<<(std::ostream &os, const BT::NodeStatus &status)
unsigned convertFromString< unsigned >(const StringView &str)
Definition: basic_types.cpp:86
const char * toStr(const BT::NodeStatus &status, bool colored=false)
toStr converts NodeStatus to string. Optionally colored.
Definition: basic_types.cpp:7
std::vector< StringView > splitString(const StringView &strToSplit, char delimeter)
NodeStatus convertFromString< NodeStatus >(const StringView &str)
nonstd::string_view StringView
Definition: basic_types.h:58
const char * convertFromString< const char * >(const StringView &str)
Definition: basic_types.cpp:74
bool convertFromString< bool >(const StringView &str)
int convertFromString< int >(const StringView &str)
Definition: basic_types.cpp:80
NodeStatus
Definition: basic_types.h:28
NodeType
Definition: basic_types.h:16


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 04:01:53