ADTF
Loading...
Searching...
No Matches
mapper_interfaces.h
Go to the documentation of this file.
1
19
20#ifndef _MAPPING_INTERFACES_HEADER
21#define _MAPPING_INTERFACES_HEADER
22
23#include <chrono>
24#include <functional>
25#include <memory>
26#include <stdexcept>
27#include <unordered_map>
28#include <variant>
29#include <vector>
30#include <optional>
31#include <string>
32
33namespace ddl {
34namespace mapping {
35
39using ValueVariant = std::variant<uint8_t,
40 int8_t,
41 uint16_t,
42 int16_t,
43 uint32_t,
44 int32_t,
45 uint64_t,
46 int64_t,
47 float,
48 double,
49 bool>;
50
57template <typename T>
58void getValueRef(const ValueVariant& value, T& value_reference)
59{
60 static_assert(std::is_arithmetic_v<T>,
61 "Only arithmetic types are supported to retrieve the value with getValue<T>");
62 value_reference = std::visit([](auto&& variant_content) { return static_cast<T>(variant_content); }, value);
63}
64
70template <typename T>
71T getValue(const ValueVariant& value)
72{
73 static_assert(std::is_arithmetic_v<T>,
74 "Only arithmetic types are supported to retrieve the value with getValue<T>");
75 return std::visit([](auto&& variant_content) { return static_cast<T>(variant_content); }, value);
76}
77
82{
83public:
87 virtual ~SourceValue() {};
88};
89
94public:
99 virtual std::optional<ValueVariant> getValue() const = 0;
100
106 template <typename T>
107 void getValueRef(T& value_reference) const
108 {
109 if (const auto untyped_value = getValue())
110 {
111 ddl::mapping::getValueRef(*untyped_value, value_reference);
112 }
113 }
114
121 template <typename T>
122 void getValueOrDefaultRef(T& value_reference, T default_value) const
123 {
124 if (const auto untyped_value = getValue())
125 {
126 ddl::mapping::getValueRef(*untyped_value, value_reference);
127 }
128 else
129 {
130 value_reference = default_value;
131 }
132 }
133
140 template <typename T>
141 T getValueOrDefault(T default_value) const
142 {
143 T value;
144 this->getValueOrDefaultRef<T>(value, default_value);
145 return value;
146 }
147
153 template <typename T>
154 T getValue() const
155 {
156 return this->getValueOrDefault(T{});
157 }
158};
159
164public:
170 virtual std::optional<ValueVariant> getValue(size_t array_pos) const = 0;
171
176 virtual size_t getArraySize() const = 0;
177};
178
183public:
187 virtual ~TargetValue(){};
188
196 virtual void initialize(std::shared_ptr<const SourceValue> value_source) = 0;
197
202 virtual void update() = 0;
203};
204
208class Property {
209public:
213 Property() = default;
217 Property(const Property&) = default;
221 Property(Property&&) = default;
226 Property& operator=(const Property&) = default;
232
240 Property(std::string value, std::string type) : _value(std::move(value)), _type(std::move(type))
241 {
242 }
243
248 template <
249 typename T,
250 std::enable_if_t<std::is_unsigned<T>::value && !std::is_same<T, bool>::value, bool> = true>
251 Property(T value) : _value(std::to_string(value)), _type("unsigned")
252 {
253 }
254
259 template <typename T,
260 std::enable_if_t<std::is_integral<T>::value && std::is_signed<T>::value, bool> = true>
261 Property(T value) : _value(std::to_string(value)), _type("signed")
262 {
263 }
264
269 template <typename T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true>
270 Property(T value) : _value(std::to_string(value)), _type("float")
271 {
272 }
273
278 template <typename T, std::enable_if_t<std::is_same<T, bool>::value, bool> = true>
279 Property(T value) : _value((value ? "true" : "false")), _type("bool")
280 {
281 }
282
287 Property(std::string_view string_value) : _value(std::string(string_value)), _type("string")
288 {
289 }
290
295 const std::string& getValue() const
296 {
297 return _value;
298 }
299
304 const std::string& getType() const
305 {
306 return _type;
307 }
308
309private:
310 std::string _value;
311 std::string _type;
312};
313
322inline bool operator==(const Property& left, const Property& right)
323{
324 return left.getType() == right.getType() && left.getValue() == right.getValue();
325}
326
331using Properties = std::unordered_map<std::string, Property>;
332
341inline Properties operator+(const Properties& properties_left, const Properties& properties_right)
342{
343 Properties result_properties(properties_left);
344 for (const auto& current_right: properties_right) {
345 result_properties.insert_or_assign(current_right.first, current_right.second);
346 }
347 return result_properties;
348}
349
354public:
358 virtual ~MapperElement() = default;
363 virtual const std::string& getName() const = 0;
368 virtual const Properties& getProperties() const = 0;
369};
370
375public:
379 virtual void updateBegin(std::chrono::nanoseconds /* time */)
380 {
381 }
382
386 virtual void updateEnd()
387 {
388 }
389
393 virtual void reset()
394 {
395 }
396};
397
401class Source : public MapperElement {
402public:
407 virtual std::vector<std::string> getAllValues() const = 0;
413 virtual std::shared_ptr<const SourceValue> getValue(
414 const std::string& value_name) const = 0;
415};
416
420class Memory {
421public:
425 virtual ~Memory() = default;
430 virtual const void* getPtr() const = 0;
435 virtual void* getPtr() = 0;
441 virtual size_t getSize() const = 0;
442};
443
447class MemorySource : public Source {
448public:
449 ~MemorySource() override = default;
450
455 virtual size_t getExpectedDataSize() const = 0;
460 virtual void updateMemory(std::shared_ptr<const Memory> memory) = 0;
461};
462
467public:
471 virtual ~MapperFactory() = default;
476 virtual std::string getType() const = 0;
481 virtual Properties getDefaultProperties() const = 0;
482};
483
488template<typename ElementType>
490{
491public:
498 virtual std::shared_ptr<ElementType> make(std::string name, Properties properties) const = 0;
499};
500
505
509class Target : public MapperElement, public Updateable {
510public:
515 virtual std::vector<std::string> getAllValues() const = 0;
521 virtual std::shared_ptr<TargetValue> getValue(const std::string& value_name) const = 0;
522};
523
527class MemoryTarget : public Target {
528public:
532 ~MemoryTarget() override = default;
533
538 virtual size_t getExpectedDataSize() const = 0;
543 virtual void updateMemory(std::shared_ptr<Memory> memory) = 0;
547 void reset() override
548 {
549 updateMemory(nullptr);
550 }
551};
552
557
561class Function : public MapperElement {
562public:
570 std::optional<std::string> default_value;
571 };
572
576 class Callable {
577 public:
581 virtual ~Callable() = default;
587 virtual std::shared_ptr<const SourceValue> initialize(const std::unordered_map<std::string, std::shared_ptr<const SourceValue>>& parameters) = 0;
588 };
589
593 using Description = std::unordered_map<std::string, ParameterDescription>;
598 virtual Description getDescription() const = 0;
604 virtual std::shared_ptr<Callable> getCallable() const = 0;
605};
606
611
616public:
628 virtual bool updateTime(std::chrono::nanoseconds source_value_time,
629 uint64_t source_value_update_counter,
630 const std::shared_ptr<const SourceValue>& source_value) = 0;
634 virtual std::optional<std::chrono::nanoseconds> getTime() const = 0;
635};
636
641public:
646 virtual void update([[maybe_unused]]std::chrono::nanoseconds trigger_time) = 0;
647};
648
654public:
660 virtual void update([[maybe_unused]] std::chrono::nanoseconds trigger_time,
661 [[maybe_unused]] TimeStrategy* time_strategy) = 0;
662};
663
664} // namespace mapping
665} // namespace ddl
666#endif // _MAPPING_INTERFACES_HEADER
Source Value base class that represents an array.
Definition mapper_interfaces.h:163
virtual size_t getArraySize() const =0
Returns the array size of the SourceValue.
virtual std::optional< ValueVariant > getValue(size_t array_pos) const =0
Get the value as optional variant.
Trigger interface to trigger the assignments of a specific target together with a update time strateg...
Definition mapper_interfaces.h:653
virtual void update(std::chrono::nanoseconds trigger_time, TimeStrategy *time_strategy)=0
indicates an update of target or a source
Trigger interface to trigger the assignments of a specific target.
Definition mapper_interfaces.h:640
virtual void update(std::chrono::nanoseconds trigger_time)=0
indicates an update of target or a source
Function caller.
Definition mapper_interfaces.h:576
virtual std::shared_ptr< const SourceValue > initialize(const std::unordered_map< std::string, std::shared_ptr< const SourceValue > > &parameters)=0
Initializes the callable with named parameter source value references.
virtual ~Callable()=default
DTOR.
Mapping Function base class.
Definition mapper_interfaces.h:561
virtual Description getDescription() const =0
Get the parameter descriptions.
virtual std::shared_ptr< Callable > getCallable() const =0
Get a Function Callable.
std::unordered_map< std::string, ParameterDescription > Description
Definition mapper_interfaces.h:593
Mapper Element.
Definition mapper_interfaces.h:353
virtual const Properties & getProperties() const =0
Get the Properties.
virtual const std::string & getName() const =0
Get the Name.
virtual ~MapperElement()=default
DTOR.
A mapper factory to create Source, Target or Function.
Definition mapper_interfaces.h:466
virtual Properties getDefaultProperties() const =0
Get the default properties to initialize the Source, Target or Function with.
virtual std::string getType() const =0
Get the Type as string.
virtual ~MapperFactory()=default
DTOR.
Source memory base class for sources representing a continuous memory buffer.
Definition mapper_interfaces.h:447
virtual size_t getExpectedDataSize() const =0
Get the expected data size in bytes.
virtual void updateMemory(std::shared_ptr< const Memory > memory)=0
Update the sources memory buffer.
Memory Target base class that represents a continuous memory buffer.
Definition mapper_interfaces.h:527
virtual void updateMemory(std::shared_ptr< Memory > memory)=0
Sets the target's memory buffer where to set the values to.
virtual size_t getExpectedDataSize() const =0
Get the expected data size of the buffer.
~MemoryTarget() override=default
DTOR.
void reset() override
resets the current memory buffer
Definition mapper_interfaces.h:547
Memory base class for sources and targets representing a continous memory buffer.
Definition mapper_interfaces.h:420
virtual const void * getPtr() const =0
Get the readable pointer of the buffer.
virtual void * getPtr()=0
Get the writable pointer of the buffer.
virtual ~Memory()=default
DTOR.
virtual size_t getSize() const =0
Get the Size in bytes of the buffer.
Value getter to retrieve a value with plain type content.
Definition mapper_interfaces.h:93
void getValueRef(T &value_reference) const
Get the value.
Definition mapper_interfaces.h:107
T getValueOrDefault(T default_value) const
Get the value or the default value if not set.
Definition mapper_interfaces.h:141
T getValue() const
Get the value.
Definition mapper_interfaces.h:154
virtual std::optional< ValueVariant > getValue() const =0
Get the value as arithmetic type.
void getValueOrDefaultRef(T &value_reference, T default_value) const
Get the value or the default value if not set.
Definition mapper_interfaces.h:122
Property class representing a pair of value and its type.
Definition mapper_interfaces.h:208
Property(T value)
CTOR for unsigned integer value (uint8-uint64) and "unsigned" type.
Definition mapper_interfaces.h:251
const std::string & getValue() const
Get the value as string.
Definition mapper_interfaces.h:295
const std::string & getType() const
Get the value type as string.
Definition mapper_interfaces.h:304
Property & operator=(const Property &)=default
copy assignment
Property()=default
CTOR.
Property(const Property &)=default
copy CTOR
Property(std::string_view string_value)
CTOR for string value and "string" type.
Definition mapper_interfaces.h:287
Property & operator=(Property &&)=default
move assignment
Property(Property &&)=default
move CTOR
Property(std::string value, std::string type)
CTOR.
Definition mapper_interfaces.h:240
Source Value base class.
Definition mapper_interfaces.h:82
virtual ~SourceValue()
DTOR.
Definition mapper_interfaces.h:87
Mapping Source base class.
Definition mapper_interfaces.h:401
virtual std::vector< std::string > getAllValues() const =0
Get all value names.
virtual std::shared_ptr< const SourceValue > getValue(const std::string &value_name) const =0
Get the value source with a certain name.
A specific mapper factory to create Source, Target or Function.
Definition mapper_interfaces.h:490
virtual std::shared_ptr< ElementType > make(std::string name, Properties properties) const =0
Factory create function which creates a Source, Target or Function instance with a name and intialize...
Value setter to set a value.
Definition mapper_interfaces.h:182
virtual ~TargetValue()
DTOR.
Definition mapper_interfaces.h:187
virtual void update()=0
virtual void initialize(std::shared_ptr< const SourceValue > value_source)=0
Sets the value source at initializing time to bind the value_source as source of the target.
Mapping Target base class.
Definition mapper_interfaces.h:509
virtual std::shared_ptr< TargetValue > getValue(const std::string &value_name) const =0
Get the value Target with a certain name.
virtual std::vector< std::string > getAllValues() const =0
Get all value names.
Definition mapper_interfaces.h:615
virtual std::optional< std::chrono::nanoseconds > getTime() const =0
virtual bool updateTime(std::chrono::nanoseconds source_value_time, uint64_t source_value_update_counter, const std::shared_ptr< const SourceValue > &source_value)=0
Updateable interface class.
Definition mapper_interfaces.h:374
virtual void updateBegin(std::chrono::nanoseconds)
Function callback interface that is called before a value will be updated.
Definition mapper_interfaces.h:379
virtual void updateEnd()
Function callback interface that is called after a value was updated.
Definition mapper_interfaces.h:386
virtual void reset()
Function callback interface that is called if a value is about to be reset.
Definition mapper_interfaces.h:393
definition of the mapping namespace
Definition ddl.h:50
SpecificMapperFactory< Source > SourceFactory
A Source factory to create a Source.
Definition mapper_interfaces.h:504
std::unordered_map< std::string, Property > Properties
Properties class as set of key - property value pairs This proerties are used to adjust the readers a...
Definition mapper_interfaces.h:331
bool operator==(const MapAssignment &a, const MapAssignment &b)
compares the Assignments
Definition map_assignment.h:184
SpecificMapperFactory< Target > TargetFactory
A Target factory to create a Target.
Definition mapper_interfaces.h:556
SpecificMapperFactory< Function > FunctionFactory
A Function factory to create a Function.
Definition mapper_interfaces.h:610
Properties operator+(const Properties &properties_left, const Properties &properties_right)
Add operator for properties.
Definition mapper_interfaces.h:341
void getValueRef(const ValueVariant &value, T &value_reference)
Get the value from a variant as plain type T.
Definition mapper_interfaces.h:58
std::variant< uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t, int64_t, float, double, bool > ValueVariant
Variant Value to retrieve a plain type value of a source.
Definition mapper_interfaces.h:39
T getValue(const ValueVariant &value)
Get the value.
Definition mapper_interfaces.h:71
definition of the ddl namespace
Definition codec.h:21
Parameter Description.
Definition mapper_interfaces.h:566
std::optional< std::string > default_value
if optional, the default value of the parameter
Definition mapper_interfaces.h:570