@@ -130,15 +130,21 @@ 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]]
139139inline StringConverter GetAnyFromStringFunctor ()
140140{
141- return [](StringView str) { return Any (convertFromString<T>(str)); };
141+ if constexpr (std::is_constructible_v<StringView, T>)
142+ {
143+ return [](StringView str) { return Any (str); };
144+ }
145+ else {
146+ return [](StringView str) { return Any (convertFromString<T>(str)); };
147+ }
142148}
143149
144150template <> [[nodiscard]]
@@ -152,10 +158,17 @@ inline StringConverter GetAnyFromStringFunctor<void>()
152158template <typename T> [[nodiscard]]
153159std::string toStr (const T& value)
154160{
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);
161+ if constexpr (!std::is_arithmetic_v<T>)
162+ {
163+ throw LogicError (
164+ StrCat (" Function BT::toStr<T>() not specialized for type [" ,
165+ BT::demangle (typeid (T)), " ]," ,
166+ " Implement it consistently with BT::convertFromString<T>(), "
167+ " or provide at dummy version that returns an empty string." )
168+ );
169+ } else {
170+ return std::to_string (value);
171+ }
159172}
160173
161174template <> [[nodiscard]]
@@ -250,11 +263,11 @@ class PortInfo
250263 };
251264
252265 PortInfo (PortDirection direction = PortDirection::INOUT) :
253- _type (direction), _type_info (typeid (AnyTypeAllowed))
266+ type_ (direction), type_info_ (typeid (AnyTypeAllowed))
254267 {}
255268
256269 PortInfo (PortDirection direction, std::type_index type_info, StringConverter conv) :
257- _type (direction), _type_info (type_info), _converter (conv)
270+ type_ (direction), type_info_ (type_info), converter_ (conv)
258271 {}
259272
260273 [[nodiscard]] PortDirection direction () const ;
@@ -274,28 +287,38 @@ class PortInfo
274287
275288 void setDescription (StringView description);
276289
277- void setDefaultValue (StringView default_value_as_string);
290+ template <typename T>
291+ void setDefaultValue (const T& default_value) {
292+ default_value_ = Any (default_value);
293+ try {
294+ default_value_str_ = BT::toStr (default_value);
295+ }
296+ catch (LogicError&) {}
297+ }
278298
279299 [[nodiscard]] const std::string& description () const ;
280300
281- [[nodiscard]] std::optional<std::string> defaultValue () const ;
301+ [[nodiscard]] const Any& defaultValue () const ;
302+
303+ [[nodiscard]] const std::string& defaultValueString () const ;
282304
283305 [[nodiscard]] bool isStronglyTyped () const
284306 {
285- return _type_info != typeid (AnyTypeAllowed);
307+ return type_info_ != typeid (AnyTypeAllowed);
286308 }
287309
288310 [[nodiscard]] const StringConverter& converter () const
289311 {
290- return _converter ;
312+ return converter_ ;
291313 }
292314
293315private:
294- PortDirection _type ;
295- std::type_index _type_info ;
296- StringConverter _converter ;
316+ PortDirection type_ ;
317+ std::type_index type_info_ ;
318+ StringConverter converter_ ;
297319 std::string description_;
298- std::optional<std::string> default_value_;
320+ Any default_value_;
321+ std::string default_value_str_;
299322};
300323
301324template <typename T = PortInfo::AnyTypeAllowed> [[nodiscard]]
@@ -355,7 +378,7 @@ inline std::pair<std::string, PortInfo> InputPort(StringView name, const T& defa
355378 StringView description)
356379{
357380 auto out = CreatePort<T>(PortDirection::INPUT, name, description);
358- out.second .setDefaultValue (BT::toStr ( default_value) );
381+ out.second .setDefaultValue (default_value);
359382 return out;
360383}
361384
@@ -365,12 +388,12 @@ inline std::pair<std::string, PortInfo> BidirectionalPort(StringView name,
365388 StringView description)
366389{
367390 auto out = CreatePort<T>(PortDirection::INOUT, name, description);
368- out.second .setDefaultValue (BT::toStr ( default_value) );
391+ out.second .setDefaultValue (default_value);
369392 return out;
370393}
371394// ----------
372395
373- typedef std::unordered_map<std::string, PortInfo> PortsList ;
396+ using PortsList = std::unordered_map<std::string, PortInfo>;
374397
375398template <typename T, typename = void >
376399struct has_static_method_providedPorts : std::false_type
@@ -398,8 +421,8 @@ inline PortsList
398421 return {};
399422}
400423
401- typedef std::chrono::high_resolution_clock::time_point TimePoint ;
402- typedef std::chrono::high_resolution_clock::duration Duration ;
424+ using TimePoint = std::chrono::high_resolution_clock::time_point;
425+ using Duration = std::chrono::high_resolution_clock::duration;
403426
404427} // namespace BT
405428
0 commit comments