@@ -130,9 +130,9 @@ NodeType convertFromString<NodeType>(StringView str);
130130template <> [[nodiscard]]
131131PortDirection convertFromString<PortDirection>(StringView str);
132132
133- typedef std::function<Any(StringView)> StringConverter ;
133+ using StringConverter = std::function<Any(StringView)>;
134134
135- typedef std::unordered_map<const std::type_info*, StringConverter> StringConvertersMap ;
135+ using StringConvertersMap = std::unordered_map<const std::type_info*, StringConverter>;
136136
137137// helper function
138138template <typename T> [[nodiscard]]
@@ -152,10 +152,17 @@ inline StringConverter GetAnyFromStringFunctor<void>()
152152template <typename T> [[nodiscard]]
153153std::string toStr (const T& value)
154154{
155- static_assert (std::is_arithmetic_v<T>,
156- " You need a template specialization of BT::toStr() and "
157- " it must be consistent with the implementation of BT::convertFromString" );
158- return std::to_string (value);
155+ if constexpr (!std::is_arithmetic_v<T>)
156+ {
157+ throw LogicError (
158+ StrCat (" Function BT::toStr<T>() not specialized for type [" ,
159+ BT::demangle (typeid (T)), " ]," ,
160+ " Implement it consistently with BT::convertFromString<T>(), "
161+ " or provide at dummy version that returns an empty string." )
162+ );
163+ } else {
164+ return std::to_string (value);
165+ }
159166}
160167
161168template <> [[nodiscard]]
@@ -250,11 +257,11 @@ class PortInfo
250257 };
251258
252259 PortInfo (PortDirection direction = PortDirection::INOUT) :
253- _type (direction), _type_info (typeid (AnyTypeAllowed))
260+ type_ (direction), type_info_ (typeid (AnyTypeAllowed))
254261 {}
255262
256263 PortInfo (PortDirection direction, std::type_index type_info, StringConverter conv) :
257- _type (direction), _type_info (type_info), _converter (conv)
264+ type_ (direction), type_info_ (type_info), converter_ (conv)
258265 {}
259266
260267 [[nodiscard]] PortDirection direction () const ;
@@ -274,28 +281,38 @@ class PortInfo
274281
275282 void setDescription (StringView description);
276283
277- void setDefaultValue (StringView default_value_as_string);
284+ template <typename T>
285+ void setDefaultValue (const T& default_value) {
286+ default_value_ = Any (default_value);
287+ try {
288+ default_value_str_ = BT::toStr (default_value);
289+ }
290+ catch (LogicError&) {}
291+ }
278292
279293 [[nodiscard]] const std::string& description () const ;
280294
281- [[nodiscard]] std::optional<std::string> defaultValue () const ;
295+ [[nodiscard]] const Any& defaultValue () const ;
296+
297+ [[nodiscard]] const std::string& defaultValueString () const ;
282298
283299 [[nodiscard]] bool isStronglyTyped () const
284300 {
285- return _type_info != typeid (AnyTypeAllowed);
301+ return type_info_ != typeid (AnyTypeAllowed);
286302 }
287303
288304 [[nodiscard]] const StringConverter& converter () const
289305 {
290- return _converter ;
306+ return converter_ ;
291307 }
292308
293309private:
294- PortDirection _type ;
295- std::type_index _type_info ;
296- StringConverter _converter ;
310+ PortDirection type_ ;
311+ std::type_index type_info_ ;
312+ StringConverter converter_ ;
297313 std::string description_;
298- std::optional<std::string> default_value_;
314+ Any default_value_;
315+ std::string default_value_str_;
299316};
300317
301318template <typename T = PortInfo::AnyTypeAllowed> [[nodiscard]]
@@ -355,7 +372,7 @@ inline std::pair<std::string, PortInfo> InputPort(StringView name, const T& defa
355372 StringView description)
356373{
357374 auto out = CreatePort<T>(PortDirection::INPUT, name, description);
358- out.second .setDefaultValue (BT::toStr ( default_value) );
375+ out.second .setDefaultValue (default_value);
359376 return out;
360377}
361378
@@ -365,12 +382,12 @@ inline std::pair<std::string, PortInfo> BidirectionalPort(StringView name,
365382 StringView description)
366383{
367384 auto out = CreatePort<T>(PortDirection::INOUT, name, description);
368- out.second .setDefaultValue (BT::toStr ( default_value) );
385+ out.second .setDefaultValue (default_value);
369386 return out;
370387}
371388// ----------
372389
373- typedef std::unordered_map<std::string, PortInfo> PortsList ;
390+ using PortsList = std::unordered_map<std::string, PortInfo>;
374391
375392template <typename T, typename = void >
376393struct has_static_method_providedPorts : std::false_type
@@ -398,8 +415,8 @@ inline PortsList
398415 return {};
399416}
400417
401- typedef std::chrono::high_resolution_clock::time_point TimePoint ;
402- typedef std::chrono::high_resolution_clock::duration Duration ;
418+ using TimePoint = std::chrono::high_resolution_clock::time_point;
419+ using Duration = std::chrono::high_resolution_clock::duration;
403420
404421} // namespace BT
405422
0 commit comments