Go to the documentation of this file.00001 #include "behaviortree_cpp/basic_types.h"
00002 #include <cstdlib>
00003 #include <cstring>
00004
00005 namespace BT
00006 {
00007
00008 template <>
00009 std::string toStr<NodeStatus>(NodeStatus status)
00010 {
00011 switch (status)
00012 {
00013 case NodeStatus::SUCCESS:
00014 return "SUCCESS";
00015 case NodeStatus::FAILURE:
00016 return "FAILURE";
00017 case NodeStatus::RUNNING:
00018 return "RUNNING";
00019 case NodeStatus::IDLE:
00020 return "IDLE";
00021 }
00022 return "";
00023 }
00024
00025 std::string toStr(std::string value)
00026 {
00027 return value;
00028 }
00029
00030 std::string toStr(NodeStatus status, bool colored)
00031 {
00032 if (!colored)
00033 {
00034 return toStr(status);
00035 }
00036 else
00037 {
00038 switch (status)
00039 {
00040 case NodeStatus::SUCCESS:
00041 return "\x1b[32m"
00042 "SUCCESS"
00043 "\x1b[0m";
00044 case NodeStatus::FAILURE:
00045 return "\x1b[31m"
00046 "FAILURE"
00047 "\x1b[0m";
00048 case NodeStatus::RUNNING:
00049 return "\x1b[33m"
00050 "RUNNING"
00051 "\x1b[0m";
00052 case NodeStatus::IDLE:
00053 return "\x1b[36m"
00054 "IDLE"
00055 "\x1b[0m";
00056 }
00057 }
00058 return "Undefined";
00059 }
00060
00061
00062
00063 template <>
00064 std::string toStr<PortDirection>(PortDirection direction)
00065 {
00066 switch(direction)
00067 {
00068 case PortDirection::INPUT: return "Input";
00069 case PortDirection::OUTPUT: return "Output";
00070 case PortDirection::INOUT: return "InOut";
00071 }
00072 return "InOut";
00073 }
00074
00075
00076 template<> std::string toStr<NodeType>(NodeType type)
00077 {
00078 switch (type)
00079 {
00080 case NodeType::ACTION:
00081 return "Action";
00082 case NodeType::CONDITION:
00083 return "Condition";
00084 case NodeType::DECORATOR:
00085 return "Decorator";
00086 case NodeType::CONTROL:
00087 return "Control";
00088 case NodeType::SUBTREE:
00089 return "SubTree";
00090 default:
00091 return "Undefined";
00092 }
00093 }
00094
00095
00096 template <>
00097 std::string convertFromString<std::string>(StringView str)
00098 {
00099 return std::string( str.data(), str.size() );
00100 }
00101
00102
00103 template <>
00104 const char* convertFromString<const char*>(StringView str)
00105 {
00106 return str.to_string().c_str();
00107 }
00108
00109 template <>
00110 int convertFromString<int>(StringView str)
00111 {
00112 return std::stoi(str.data());
00113 }
00114
00115 template <>
00116 unsigned convertFromString<unsigned>(StringView str)
00117 {
00118 return unsigned(std::stoul(str.data()));
00119 }
00120
00121 template <>
00122 double convertFromString<double>(StringView str)
00123 {
00124 return std::stod(str.data());
00125 }
00126
00127 template <>
00128 std::vector<int> convertFromString<std::vector<int>>(StringView str)
00129 {
00130 auto parts = splitString(str, ';');
00131 std::vector<int> output;
00132 output.reserve( parts.size() );
00133 for(const StringView& part: parts)
00134 {
00135 char* end;
00136 output.push_back( std::strtol( part.data(), &end, 10 ) );
00137 }
00138 return output;
00139 }
00140
00141 template <>
00142 std::vector<double> convertFromString<std::vector<double>>(StringView str)
00143 {
00144 auto parts = splitString(str, ';');
00145 std::vector<double> output;
00146 output.reserve( parts.size() );
00147 for(const StringView& part: parts)
00148 {
00149 char* end;
00150 output.push_back( std::strtod( part.data(), &end ) );
00151 }
00152 return output;
00153 }
00154
00155 template <>
00156 bool convertFromString<bool>(StringView str)
00157 {
00158 if (str.size() == 1)
00159 {
00160 if (str[0] == '0')
00161 {
00162 return false;
00163 }
00164 if (str[0] == '1')
00165 {
00166 return true;
00167 }
00168 }
00169 else if (str.size() == 4)
00170 {
00171 if (str == "true" || str == "TRUE" || str == "True")
00172 {
00173 return true;
00174 }
00175 }
00176 else if (str.size() == 5)
00177 {
00178 if (str == "false" || str == "FALSE" || str == "False")
00179 {
00180 return false;
00181 }
00182 }
00183 throw RuntimeError("convertFromString(): invalid bool conversion");
00184 }
00185
00186 template <>
00187 NodeStatus convertFromString<NodeStatus>(StringView str)
00188 {
00189 if( str == "IDLE" ) return NodeStatus::IDLE;
00190 if( str == "RUNNING" ) return NodeStatus::RUNNING;
00191 if( str == "SUCCESS" ) return NodeStatus::SUCCESS;
00192 if( str == "FAILURE" ) return NodeStatus::FAILURE;
00193 throw RuntimeError(std::string("Cannot convert this to NodeStatus: ") + str.to_string() );
00194 }
00195
00196 template <>
00197 NodeType convertFromString<NodeType>(StringView str)
00198 {
00199 if( str == "Action" ) return NodeType::ACTION;
00200 if( str == "Condition" ) return NodeType::CONDITION;
00201 if( str == "Control" ) return NodeType::CONTROL;
00202 if( str == "Decorator" ) return NodeType::DECORATOR;
00203 if( str == "SubTree" || str == "Subtree" ) return NodeType::SUBTREE;
00204 return NodeType::UNDEFINED;
00205 }
00206
00207 template <>
00208 PortDirection convertFromString<PortDirection>(StringView str)
00209 {
00210 if( str == "Input" || str == "INPUT" ) return PortDirection::INPUT;
00211 if( str == "Output" || str == "OUTPUT") return PortDirection::OUTPUT;
00212 return PortDirection::INOUT;
00213 }
00214
00215
00216 std::ostream& operator<<(std::ostream& os, const NodeType& type)
00217 {
00218 os << toStr(type);
00219 return os;
00220 }
00221
00222 std::ostream& operator<<(std::ostream& os, const NodeStatus& status)
00223 {
00224 os << toStr(status);
00225 return os;
00226 }
00227
00228 std::ostream& operator<<(std::ostream& os, const PortDirection& type)
00229 {
00230 os << toStr(type);
00231 return os;
00232 }
00233
00234 std::vector<StringView> splitString(const StringView &strToSplit, char delimeter)
00235 {
00236 std::vector<StringView> splitted_strings;
00237 splitted_strings.reserve(4);
00238
00239 size_t pos = 0;
00240 while( pos < strToSplit.size())
00241 {
00242 size_t new_pos = strToSplit.find_first_of(delimeter, pos);
00243 if( new_pos == std::string::npos)
00244 {
00245 new_pos = strToSplit.size();
00246 }
00247 StringView sv = { &strToSplit.data()[pos], new_pos - pos };
00248 splitted_strings.push_back( sv );
00249 pos = new_pos + 1;
00250 }
00251 return splitted_strings;
00252 }
00253
00254 PortDirection PortInfo::direction() const
00255 {
00256 return _type;
00257 }
00258
00259 const std::type_info* PortInfo::type() const
00260 {
00261 return _info;
00262 }
00263
00264 Any PortInfo::parseString(const char *str) const
00265 {
00266 if( _converter)
00267 {
00268 return _converter(str);
00269 }
00270 return {};
00271 }
00272
00273 Any PortInfo::parseString(const std::string &str) const
00274 {
00275 if( _converter)
00276 {
00277 return _converter(str);
00278 }
00279 return {};
00280 }
00281
00282 void PortInfo::setDescription(StringView description)
00283 {
00284 description_ = description.to_string();
00285 }
00286
00287 void PortInfo::setDefaultValue(StringView default_value_as_string)
00288 {
00289 default_value_ = default_value_as_string.to_string();
00290 }
00291
00292 const std::string &PortInfo::description() const
00293 {
00294 return description_;
00295 }
00296
00297 const std::string &PortInfo::defaultValue() const
00298 {
00299 return default_value_;
00300 }
00301
00302
00303
00304 }