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 const char* toStr(const NodeStatus& status, bool colored)
00008 {
00009 if (!colored)
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 }
00023 else
00024 {
00025 switch (status)
00026 {
00027 case NodeStatus::SUCCESS:
00028 return ("\x1b[32m"
00029 "SUCCESS"
00030 "\x1b[0m");
00031 case NodeStatus::FAILURE:
00032 return ("\x1b[31m"
00033 "FAILURE"
00034 "\x1b[0m");
00035 case NodeStatus::RUNNING:
00036 return ("\x1b[33m"
00037 "RUNNING"
00038 "\x1b[0m");
00039 case NodeStatus::IDLE:
00040 return ("\x1b[36m"
00041 "IDLE"
00042 "\x1b[0m");
00043 }
00044 }
00045 return "Undefined";
00046 }
00047
00048 const char* toStr(const NodeType& type)
00049 {
00050 switch (type)
00051 {
00052 case NodeType::ACTION:
00053 return "Action";
00054 case NodeType::CONDITION:
00055 return "Condition";
00056 case NodeType::DECORATOR:
00057 return "Decorator";
00058 case NodeType::CONTROL:
00059 return "Control";
00060 case NodeType::SUBTREE:
00061 return "SubTree";
00062 default:
00063 return "Undefined";
00064 }
00065 }
00066
00067 template <>
00068 std::string convertFromString<std::string>(const StringView& str)
00069 {
00070 return std::string( str.data(), str.size() );
00071 }
00072
00073 template <>
00074 const char* convertFromString<const char*>(const StringView& str)
00075 {
00076 return str.to_string().c_str();
00077 }
00078
00079 template <>
00080 int convertFromString<int>(const StringView& str)
00081 {
00082 return std::stoi(str.data());
00083 }
00084
00085 template <>
00086 unsigned convertFromString<unsigned>(const StringView& str)
00087 {
00088 return std::stoul(str.data());
00089 }
00090
00091 template <>
00092 double convertFromString<double>(const StringView& str)
00093 {
00094 return std::stod(str.data());
00095 }
00096
00097 template <>
00098 std::vector<int> convertFromString<std::vector<int>>(const StringView& str)
00099 {
00100 auto parts = splitString(str, ';');
00101 std::vector<int> output;
00102 output.reserve( parts.size() );
00103 for(const StringView& part: parts)
00104 {
00105 char* end;
00106 output.push_back( std::strtol( part.data(), &end, 10 ) );
00107 }
00108 return output;
00109 }
00110
00111 template <>
00112 std::vector<double> convertFromString<std::vector<double>>(const StringView& str)
00113 {
00114 auto parts = splitString(str, ';');
00115 std::vector<double> output;
00116 output.reserve( parts.size() );
00117 for(const StringView& part: parts)
00118 {
00119 char* end;
00120 output.push_back( std::strtod( part.data(), &end ) );
00121 }
00122 return output;
00123 }
00124
00125 template <>
00126 bool convertFromString<bool>(const StringView& str)
00127 {
00128 if (str.size() == 1)
00129 {
00130 if (str[0] == '0')
00131 {
00132 return false;
00133 }
00134 else if (str[0] == '1')
00135 {
00136 return true;
00137 }
00138 else
00139 {
00140 std::runtime_error("invalid bool conversion");
00141 }
00142 }
00143 else if (str.size() == 4)
00144 {
00145 if (str == "true" || str == "TRUE" || str == "True")
00146 {
00147 return true;
00148 }
00149 else
00150 {
00151 std::runtime_error("invalid bool conversion");
00152 }
00153 }
00154 else if (str.size() == 5)
00155 {
00156 if (str == "false" || str == "FALSE" || str == "False")
00157 {
00158 return false;
00159 }
00160 else
00161 {
00162 std::runtime_error("invalid bool conversion");
00163 }
00164 }
00165
00166 std::runtime_error("invalid bool conversion");
00167 return false;
00168 }
00169
00170 template <>
00171 NodeStatus convertFromString<NodeStatus>(const StringView& str)
00172 {
00173 for (auto status :
00174 {NodeStatus::IDLE, NodeStatus::RUNNING, NodeStatus::SUCCESS, NodeStatus::FAILURE})
00175 {
00176 if ( StringView(toStr(status)) == str )
00177 {
00178 return status;
00179 }
00180 }
00181 throw std::invalid_argument(std::string("Cannot convert this to NodeStatus: ") + str.to_string() );
00182 }
00183
00184 template <>
00185 NodeType convertFromString<NodeType>(const StringView& str)
00186 {
00187 for (auto status : {NodeType::ACTION, NodeType::CONDITION, NodeType::CONTROL,
00188 NodeType::DECORATOR, NodeType::SUBTREE, NodeType::UNDEFINED})
00189 {
00190 if (StringView(toStr(status)) == str)
00191 {
00192 return status;
00193 }
00194 }
00195 throw std::invalid_argument(std::string("Cannot convert this to NodeType: ") + str.to_string());
00196 }
00197
00198 std::ostream& operator<<(std::ostream& os, const NodeType& type)
00199 {
00200 os << toStr(type);
00201 return os;
00202 }
00203
00204 std::ostream& operator<<(std::ostream& os, const NodeStatus& status)
00205 {
00206 os << toStr(status);
00207 return os;
00208 }
00209
00210 std::vector<StringView> splitString(const StringView &strToSplit, char delimeter)
00211 {
00212 std::vector<StringView> splitted_strings;
00213 splitted_strings.reserve(4);
00214
00215 size_t pos = 0;
00216 while( pos < strToSplit.size())
00217 {
00218 size_t new_pos = strToSplit.find_first_of(delimeter, pos);
00219 if( new_pos == std::string::npos)
00220 {
00221 new_pos = strToSplit.size();
00222 }
00223 StringView sv = { &strToSplit.data()[pos], new_pos - pos };
00224 splitted_strings.push_back( sv );
00225 pos = new_pos + 1;
00226 }
00227 return splitted_strings;
00228 }
00229
00230 }