rad 6.2.0
|
#include <doubleMap.hpp>
Public Member Functions | |
DoubleMap () | |
virtual | ~DoubleMap () |
void | Reset () |
void | Set (const std::string &key, const T &value) |
void | Set (const std::map< std::string, T > &kv_map) |
void | Pop (std::map< std::string, T > &kv_map) |
DoubleMap (const DoubleMap &)=delete | |
DoubleMap & | operator= (const DoubleMap &)=delete |
This class allows to share a map of attributes and associated values between several producers and one consumer.
It is assumed that the producers are running at higher freq. than the consumer and that they should be interrupted as little as possible. The consumer instead runs in a best-effort mode.
The type of the values is a template parameter. In case of multiple types, a std::variant can be used.
The attributes/values are written by the producer(s) in a map via the Set() method. The attribute name is used as key. If multiple values for the same attribute are written, the old ones are replaced.
The attributes/values can be read by the consumer via the Pop() method. After reading, the attributes/values are removed from the map so that the consumer does not get old attributes.
This class uses double buffering to minimize the blocking time for the producers when the consumer try to get the values.
The writing/reading (Set/Pop methods) are protected by a mutex. For the reading (Pop method) only the swap of the index is protected, while for the writer the whole writing section is protected.
Note that another possibility is to use an std::atomic to protect the index only instead of using the std::mutex to protect the attributes writing and the index swap. In this case however the Set() of multiple attributes might end up with some attributes in one buffer and other attributes in the other buffer. This also means that some attributes related to the same Set() invocation may get published one cycle later.
|
inline |
Default constructor.
|
inlinevirtual |
Destructor.
|
delete |
Disabled copy constructor.
|
delete |
Disabled assignment operator.
|
inline |
Method to get the attributes/values currently stored. The returned attributes/values are remove them from this object.
This method is thread-safe.
kv_map | Map filled in with the attributes and values currently stored in this object. |
|
inline |
Method to clear the content of the map.
This method is NOT thread-safe.
Note: to make it thread safe we need to protect the copying in Pop() method.
|
inline |
Method to merge the given attributes/values with the ones currently stored in this object.
If an attribute is already in this object, its value is overwritten.
This method is thread-safe.
kv_map | Map of attributes and values to be merged. |
|
inline |
Method to write an attribute/value pair.
If the attribute is already in the map, its value is overwritten.
This method is thread-safe.
key | Name of the attribute to be written. |
value | Value of the attribute to be written. |