@@ -158,13 +158,16 @@ inline StringConverter GetAnyFromStringFunctor<void>()
158
158
template <typename T> [[nodiscard]]
159
159
std::string toStr (const T& value)
160
160
{
161
- if constexpr (!std::is_arithmetic_v<T>)
161
+ if constexpr (std::is_convertible_v<T, std::string> ||
162
+ std::is_convertible_v<T, std::string_view>)
163
+ {
164
+ return value;
165
+ }
166
+ else if constexpr (!std::is_arithmetic_v<T>)
162
167
{
163
168
throw LogicError (
164
169
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." )
170
+ BT::demangle (typeid (T)), " ]" )
168
171
);
169
172
} else {
170
173
return std::to_string (value);
@@ -255,25 +258,27 @@ using Result = Expected<std::monostate>;
255
258
[[nodiscard]]
256
259
bool IsAllowedPortName (StringView str);
257
260
258
- class PortInfo
261
+ struct AnyTypeAllowed
262
+ {};
263
+
264
+ class TypeInfo
259
265
{
260
266
public:
261
- struct AnyTypeAllowed
262
- {
263
- };
264
267
265
- PortInfo (PortDirection direction = PortDirection::INOUT) :
266
- type_ (direction), type_info_(typeid (AnyTypeAllowed)),
268
+ template <typename T>
269
+ static TypeInfo Create () {
270
+ return TypeInfo{typeid (T), GetAnyFromStringFunctor<T>()};
271
+ }
272
+
273
+ TypeInfo (): type_info_(typeid (AnyTypeAllowed)),
267
274
type_str_ (" AnyTypeAllowed" )
268
275
{}
269
276
270
- PortInfo (PortDirection direction, std::type_index type_info, StringConverter conv) :
271
- type_ (direction), type_info_(type_info), converter_(conv),
277
+ TypeInfo ( std::type_index type_info, StringConverter conv) :
278
+ type_info_(type_info), converter_(conv),
272
279
type_str_(BT::demangle(type_info))
273
280
{}
274
281
275
- [[nodiscard]] PortDirection direction () const ;
276
-
277
282
[[nodiscard]] const std::type_index& type () const ;
278
283
279
284
[[nodiscard]] const std::string& typeName () const ;
@@ -289,6 +294,38 @@ class PortInfo
289
294
return {};
290
295
}
291
296
297
+ [[nodiscard]] bool isStronglyTyped () const
298
+ {
299
+ return type_info_ != typeid (AnyTypeAllowed);
300
+ }
301
+
302
+ [[nodiscard]] const StringConverter& converter () const
303
+ {
304
+ return converter_;
305
+ }
306
+
307
+ private:
308
+
309
+ std::type_index type_info_;
310
+ StringConverter converter_;
311
+ std::string type_str_;
312
+ };
313
+
314
+
315
+ class PortInfo : public TypeInfo
316
+ {
317
+ public:
318
+
319
+ PortInfo (PortDirection direction = PortDirection::INOUT) :
320
+ TypeInfo (), direction_(direction)
321
+ {}
322
+
323
+ PortInfo (PortDirection direction, std::type_index type_info, StringConverter conv) :
324
+ TypeInfo (type_info, conv), direction_(direction)
325
+ {}
326
+
327
+ [[nodiscard]] PortDirection direction () const ;
328
+
292
329
void setDescription (StringView description);
293
330
294
331
template <typename T>
@@ -306,27 +343,14 @@ class PortInfo
306
343
307
344
[[nodiscard]] const std::string& defaultValueString () const ;
308
345
309
- [[nodiscard]] bool isStronglyTyped () const
310
- {
311
- return type_info_ != typeid (AnyTypeAllowed);
312
- }
313
-
314
- [[nodiscard]] const StringConverter& converter () const
315
- {
316
- return converter_;
317
- }
318
-
319
346
private:
320
- PortDirection type_;
321
- std::type_index type_info_;
322
- StringConverter converter_;
347
+ PortDirection direction_;
323
348
std::string description_;
324
349
Any default_value_;
325
350
std::string default_value_str_;
326
- std::string type_str_;
327
351
};
328
352
329
- template <typename T = PortInfo:: AnyTypeAllowed> [[nodiscard]]
353
+ template <typename T = AnyTypeAllowed> [[nodiscard]]
330
354
std::pair<std::string, PortInfo> CreatePort (PortDirection direction,
331
355
StringView name,
332
356
StringView description = {})
@@ -357,28 +381,28 @@ std::pair<std::string, PortInfo> CreatePort(PortDirection direction,
357
381
}
358
382
359
383
// ----------
360
- template <typename T = PortInfo:: AnyTypeAllowed> [[nodiscard]]
384
+ template <typename T = AnyTypeAllowed> [[nodiscard]]
361
385
inline std::pair<std::string, PortInfo> InputPort (StringView name,
362
386
StringView description = {})
363
387
{
364
388
return CreatePort<T>(PortDirection::INPUT, name, description);
365
389
}
366
390
367
- template <typename T = PortInfo:: AnyTypeAllowed> [[nodiscard]]
391
+ template <typename T = AnyTypeAllowed> [[nodiscard]]
368
392
inline std::pair<std::string, PortInfo> OutputPort (StringView name,
369
393
StringView description = {})
370
394
{
371
395
return CreatePort<T>(PortDirection::OUTPUT, name, description);
372
396
}
373
397
374
- template <typename T = PortInfo:: AnyTypeAllowed> [[nodiscard]]
398
+ template <typename T = AnyTypeAllowed> [[nodiscard]]
375
399
inline std::pair<std::string, PortInfo> BidirectionalPort (StringView name,
376
400
StringView description = {})
377
401
{
378
402
return CreatePort<T>(PortDirection::INOUT, name, description);
379
403
}
380
404
// ----------
381
- template <typename T = PortInfo:: AnyTypeAllowed> [[nodiscard]]
405
+ template <typename T = AnyTypeAllowed> [[nodiscard]]
382
406
inline std::pair<std::string, PortInfo> InputPort (StringView name, const T& default_value,
383
407
StringView description)
384
408
{
@@ -387,7 +411,7 @@ inline std::pair<std::string, PortInfo> InputPort(StringView name, const T& defa
387
411
return out;
388
412
}
389
413
390
- template <typename T = PortInfo:: AnyTypeAllowed> [[nodiscard]]
414
+ template <typename T = AnyTypeAllowed> [[nodiscard]]
391
415
inline std::pair<std::string, PortInfo> BidirectionalPort (StringView name,
392
416
const T& default_value,
393
417
StringView description)
0 commit comments