Go to the documentation of this file.
8 #include <unordered_map>
85 using Expected = nonstd::expected<T, std::string>;
103 template <
typename T>
121 template <
typename T>
127 str.remove_prefix(5);
128 return convertFromJSON<T>(str);
133 std::cerr <<
"You (maybe indirectly) called BT::convertFromString() for type ["
134 <<
type_name <<
"], but I can't find the template specialization.\n"
137 throw LogicError(std::string(
"You didn't implement the template specialization of "
138 "convertFromString for this type: ") +
143 [[nodiscard]] std::string convertFromString<std::string>(
StringView str);
180 [[nodiscard]] std::vector<int> convertFromString<std::vector<int>>(
StringView str);
184 [[nodiscard]] std::vector<double> convertFromString<std::vector<double>>(
StringView str);
188 [[nodiscard]] std::vector<std::string>
189 convertFromString<std::vector<std::string>>(
StringView str);
211 template <
typename T>
214 if constexpr(std::is_constructible_v<StringView, T>)
218 else if constexpr(std::is_same_v<BT::AnyTypeAllowed, T> || std::is_enum_v<T>)
224 return [](
StringView str) {
return Any(convertFromString<T>(str)); };
236 template <
typename T>
239 return std::is_convertible_v<T, std::string> ||
240 std::is_convertible_v<T, std::string_view>;
251 template <
typename T>
252 [[nodiscard]] std::string
toStr(
const T& value)
254 if constexpr(IsConvertibleToString<T>())
256 return static_cast<std::string
>(value);
258 else if constexpr(!std::is_arithmetic_v<T>)
265 throw LogicError(
StrCat(
"Function BT::toStr<T>() not specialized for type [",
275 [[nodiscard]] std::string
toStr<bool>(
const bool& value);
278 [[nodiscard]] std::string toStr<std::string>(
const std::string& value);
281 [[nodiscard]] std::string toStr<BT::NodeStatus>(
const BT::NodeStatus& status);
291 [[nodiscard]] std::string toStr<BT::NodeType>(
const BT::NodeType& type);
296 [[nodiscard]] std::string toStr<BT::PortDirection>(
const BT::PortDirection& direction);
304 template <
typename Predicate>
305 using enable_if =
typename std::enable_if<Predicate::value>::type*;
307 template <
typename Predicate>
310 #ifdef USE_BTCPP3_OLD_NAMES
313 template <
typename T>
314 using Optional = nonstd::expected<T, std::string>;
340 std::chrono::nanoseconds
time = std::chrono::nanoseconds(0);
348 template <
typename T>
351 return TypeInfo{
typeid(T), GetAnyFromStringFunctor<T>() };
361 [[nodiscard]]
const std::type_index&
type()
const;
363 [[nodiscard]]
const std::string&
typeName()
const;
369 template <
typename T>
407 template <
typename T>
419 [[nodiscard]]
const std::string&
description()
const;
432 template <
typename T = AnyTypeAllowed>
437 auto sname =
static_cast<std::string
>(name);
440 throw RuntimeError(
"The name of a port must not be `name` or `ID` "
441 "and must start with an alphabetic character. "
442 "Underscore is reserved.");
445 std::pair<std::string, PortInfo> out;
449 out = { sname, PortInfo(direction) };
453 out = { sname, PortInfo(direction,
typeid(T), GetAnyFromStringFunctor<T>()) };
455 if(!description.empty())
457 out.second.setDescription(description);
468 template <
typename T = AnyTypeAllowed>
469 [[nodiscard]]
inline std::pair<std::string, PortInfo>
480 template <
typename T = AnyTypeAllowed>
481 [[nodiscard]]
inline std::pair<std::string, PortInfo>
492 template <
typename T = AnyTypeAllowed>
493 [[nodiscard]]
inline std::pair<std::string, PortInfo>
503 template <
typename T = AnyTypeAllowed,
typename DefaultT = T>
504 [[nodiscard]]
inline std::pair<std::string, PortInfo>
508 static_assert(IsConvertibleToString<DefaultT>() || std::is_convertible_v<T, DefaultT> ||
509 std::is_constructible_v<T, DefaultT>,
510 "The default value must be either the same of the port or string");
512 auto out = CreatePort<T>(direction, name, description);
514 if constexpr(std::is_constructible_v<T, DefaultT>)
516 out.second.setDefaultValue(T(default_value));
518 else if constexpr(IsConvertibleToString<DefaultT>())
520 out.second.setDefaultValue(std::string(default_value));
524 out.second.setDefaultValue(default_value);
538 template <
typename T = AnyTypeAllowed,
typename DefaultT = T>
539 [[nodiscard]]
inline std::pair<std::string, PortInfo>
553 template <
typename T = AnyTypeAllowed,
typename DefaultT = T>
554 [[nodiscard]]
inline std::pair<std::string, PortInfo>
568 template <
typename T = AnyTypeAllowed>
573 if(default_value.empty() || default_value.front() !=
'{' || default_value.back() !=
'}')
575 throw LogicError(
"Output port can only refer to blackboard entries, i.e. use the "
576 "syntax '{port_name}'");
579 out.second.setDefaultValue(default_value);
585 using PortsList = std::unordered_map<std::string, PortInfo>;
587 template <
typename T,
typename =
void>
592 template <
typename T>
595 std::is_same<decltype(T::providedPorts()), PortsList>::value>::type>
600 template <
typename T,
typename =
void>
605 template <
typename T>
608 std::is_same<decltype(T::metadata()), KeyValueVector>::value>::type>
613 template <
typename T>
617 return T::providedPorts();
620 template <
typename T>
627 using TimePoint = std::chrono::high_resolution_clock::time_point;
628 using Duration = std::chrono::high_resolution_clock::duration;
void setDescription(StringView description)
const std::string & typeName() const
typename std::enable_if< Predicate::value >::type * enable_if
int32_t convertFromString< int32_t >(StringView str)
std::string demangle(char const *name)
uint64_t convertFromString< uint64_t >(StringView str)
constexpr bool IsConvertibleToString()
NodeType
Enumerates the possible types of nodes.
Expected< std::string > toJsonString(const Any &value)
std::string toStr< bool >(const bool &value)
const std::string & defaultValueString() const
const Any & defaultValue() const
nonstd::expected< T, std::string > Expected
std::string_view StringView
const char * convertFromString< const char * >(StringView str)
std::pair< std::string, PortInfo > InputPort(StringView name, StringView description={})
PortInfo(PortDirection direction, std::type_index type_info, StringConverter conv)
Any parseString(const T &) const
void setDefaultValue(const T &default_value)
StringConverter GetAnyFromStringFunctor< void >()
typename std::enable_if<!Predicate::value >::type * enable_if_not
std::pair< std::string, PortInfo > BidirectionalPort(StringView name, StringView description={})
std::pair< std::string, PortInfo > PortWithDefault(PortDirection direction, StringView name, const DefaultT &default_value, StringView description)
const StringConverter & converter() const
std::chrono::high_resolution_clock::time_point TimePoint
constexpr E value(std::size_t i) noexcept
std::chrono::high_resolution_clock::duration Duration
StringConverter converter_
uint16_t convertFromString< uint16_t >(StringView str)
std::type_index type_info_
PortInfo(PortDirection direction=PortDirection::INOUT)
int64_t convertFromString< int64_t >(StringView str)
float convertFromString< float >(StringView str)
std::unordered_map< std::string, PortInfo > PortsList
PortsList getProvidedPorts(enable_if< has_static_method_providedPorts< T >>=nullptr)
StringConverter GetAnyFromStringFunctor()
std::unordered_map< const std::type_info *, StringConverter > StringConvertersMap
static const char * json_text
std::function< Any(StringView)> StringConverter
int8_t convertFromString< int8_t >(StringView str)
Any convertFromJSON(StringView json_text, std::type_index type)
convertFromJSON will parse a json string and use JsonExporter to convert its content to a given type....
Point3D convertFromString(StringView key)
bool convertFromString< bool >(StringView str)
std::ostream & operator<<(std::ostream &os, const BT::NodeStatus &status)
std::pair< std::string, PortInfo > CreatePort(PortDirection direction, StringView name, StringView description={})
std::vector< StringView > splitString(const StringView &strToSplit, char delimeter)
NodeType convertFromString< NodeType >(StringView str)
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values
uint32_t convertFromString< uint32_t >(StringView str)
const std::type_index & type() const
std::vector< std::pair< std::string, std::string > > KeyValueVector
bool IsAllowedPortName(StringView str)
PortDirection direction() const
bool isStatusCompleted(const NodeStatus &status)
bool StartWith(StringView str, StringView prefix)
std::pair< std::string, PortInfo > OutputPort(StringView name, StringView description={})
TypeInfo(std::type_index type_info, StringConverter conv)
double convertFromString< double >(StringView str)
Expected< std::monostate > Result
std::string default_value_str_
PortDirection convertFromString< PortDirection >(StringView str)
bool isStatusActive(const NodeStatus &status)
std::chrono::nanoseconds time
NodeStatus convertFromString< NodeStatus >(StringView str)
Any parseString(const char *str) const
int16_t convertFromString< int16_t >(StringView str)
const std::string & description() const
std::string toStr(const T &value)
toStr is the reverse operation of convertFromString.
basic_string_view< char > string_view
uint8_t convertFromString< uint8_t >(StringView str)
bool isStronglyTyped() const
constexpr const char * type_name()