rad 6.2.0
Loading...
Searching...
No Matches
Public Member Functions | List of all members
rad::DoubleMap< T > Class Template Reference

#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
 
DoubleMapoperator= (const DoubleMap &)=delete
 

Detailed Description

template<typename T>
class rad::DoubleMap< T >

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.

Constructor & Destructor Documentation

◆ DoubleMap() [1/2]

template<typename T >
rad::DoubleMap< T >::DoubleMap ( )
inline

Default constructor.

◆ ~DoubleMap()

template<typename T >
virtual rad::DoubleMap< T >::~DoubleMap ( )
inlinevirtual

Destructor.

◆ DoubleMap() [2/2]

template<typename T >
rad::DoubleMap< T >::DoubleMap ( const DoubleMap< T > & )
delete

Disabled copy constructor.

Member Function Documentation

◆ operator=()

template<typename T >
DoubleMap & rad::DoubleMap< T >::operator= ( const DoubleMap< T > & )
delete

Disabled assignment operator.

◆ Pop()

template<typename T >
void rad::DoubleMap< T >::Pop ( std::map< std::string, T > & kv_map)
inline

Method to get the attributes/values currently stored. The returned attributes/values are remove them from this object.

This method is thread-safe.

Parameters
kv_mapMap filled in with the attributes and values currently stored in this object.

◆ Reset()

template<typename T >
void rad::DoubleMap< T >::Reset ( )
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.

◆ Set() [1/2]

template<typename T >
void rad::DoubleMap< T >::Set ( const std::map< std::string, T > & kv_map)
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.

Parameters
kv_mapMap of attributes and values to be merged.

◆ Set() [2/2]

template<typename T >
void rad::DoubleMap< T >::Set ( const std::string & key,
const T & value )
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.

Parameters
keyName of the attribute to be written.
valueValue of the attribute to be written.

The documentation for this class was generated from the following file: