Cbeam
Loading...
Searching...
No Matches
cbeam::container::thread_safe_map< Key, Value > Class Template Reference

Thread-safe wrapper for std::map. More...

#include <thread_safe_map.hpp>

Inheritance diagram for cbeam::container::thread_safe_map< Key, Value >:
Collaboration diagram for cbeam::container::thread_safe_map< Key, Value >:

Public Member Functions

Value & at (const Key &key, const std::string &what_arg="Key not found")
 Access specified element with bounds checking.
 
auto find (const Key &key, const std::string &what_arg={})
 Finds an element with a specific key.
 
Value & operator[] (const Key &key)
 Access or insert specified element.
 
std::size_t erase (const Key &key)
 Removes the element with the specified key.
 
std::map< Key, Value >::iterator erase (typename std::map< Key, Value >::iterator position)
 Removes the element at the specified position.
 
std::size_t count (const Key &key) const
 Returns the number of elements matching specific key.
 
std::pair< typename std::map< Key, Value >::iterator, bool > insert (const Key &key, const Value &value)
 
std::pair< typename std::map< Key, Value >::iterator, bool > insert (const std::pair< Key, Value > &pair)
 Inserts a new element or assigns to the current element if the key already exists.
 
- Public Member Functions inherited from cbeam::container::thread_safe_container< std::map< Key, Value > >
lock_guard get_lock_guard () const
 Acquires a lock guard for the set, ensuring thread safety.
 
 thread_safe_container ()=default
 Default constructor.
 
virtual ~thread_safe_container () noexcept
 
bool empty () const
 Checks if the container is empty.
 
void clear ()
 Clears the contents.
 
std::size_t size () const
 Returns the number of elements.
 
auto begin ()
 Returns an iterator to the beginning.
 
auto end ()
 Returns an iterator to the end.
 
bool is_being_modified () const
 

Additional Inherited Members

- Protected Attributes inherited from cbeam::container::thread_safe_container< std::map< Key, Value > >
std::recursive_mutex _mutex
 Mutex to protect access to _container.
 
std::map< Key, Value > _container
 Internal non-thread safe container.
 
std::atomic< bool > _is_being_modified
 

Detailed Description

template<typename Key, typename Value>
class cbeam::container::thread_safe_map< Key, Value >

Thread-safe wrapper for std::map.

Template Parameters
KeyType of the keys
ValueType of the values

Member Function Documentation

◆ at()

template<typename Key, typename Value>
Value & cbeam::container::thread_safe_map< Key, Value >::at ( const Key & key,
const std::string & what_arg = "Key not found" )
inline

Access specified element with bounds checking.

Parameters
keyKey of the element to find
what_argOptional argument for the exception message
Returns
Reference to the mapped value of the element with key equivalent to key
Exceptions
cbeam::error::out_of_rangeIf the container does not have an element with the specified key

◆ count()

template<typename Key, typename Value>
std::size_t cbeam::container::thread_safe_map< Key, Value >::count ( const Key & key) const
inline

Returns the number of elements matching specific key.

Parameters
keyKey of the elements to count
Returns
Number of elements with specified key

◆ erase() [1/2]

template<typename Key, typename Value>
std::size_t cbeam::container::thread_safe_map< Key, Value >::erase ( const Key & key)
inline

Removes the element with the specified key.

Parameters
keyKey of the element to remove
Returns
The number of elements removed (0 or 1)

◆ erase() [2/2]

template<typename Key, typename Value>
std::map< Key, Value >::iterator cbeam::container::thread_safe_map< Key, Value >::erase ( typename std::map< Key, Value >::iterator position)
inline

Removes the element at the specified position.

Parameters
positionThe iterator pointing to the element to remove.
Returns
An iterator following the last removed element. If the iterator refers to the last element, the end() iterator is returned.
Note
This method is thread-safe and will block if another thread is currently writing to the map.

◆ find()

template<typename Key, typename Value>
auto cbeam::container::thread_safe_map< Key, Value >::find ( const Key & key,
const std::string & what_arg = {} )
inline

Finds an element with a specific key.

Parameters
keyKey of the element to find
what_argOptional argument for the exception message
Returns
Iterator to an element with key equivalent to key. If no such element is found, past-the-end (see end()) iterator is returned.
Exceptions
cbeam::error::out_of_rangeIf what_arg is not empty and the key is not found

◆ insert() [1/2]

template<typename Key, typename Value>
std::pair< typename std::map< Key, Value >::iterator, bool > cbeam::container::thread_safe_map< Key, Value >::insert ( const Key & key,
const Value & value )
inline

◆ insert() [2/2]

template<typename Key, typename Value>
std::pair< typename std::map< Key, Value >::iterator, bool > cbeam::container::thread_safe_map< Key, Value >::insert ( const std::pair< Key, Value > & pair)
inline

Inserts a new element or assigns to the current element if the key already exists.

Parameters
pairA pair consisting of the key and value to insert into the map.
Returns
A pair consisting of an iterator to the inserted or assigned element and a bool denoting whether the insertion took place.
Note
This method is thread-safe and will block if another thread is currently writing to the map.

◆ operator[]()

template<typename Key, typename Value>
Value & cbeam::container::thread_safe_map< Key, Value >::operator[] ( const Key & key)
inline

Access or insert specified element.

Parameters
keyKey of the element to find or insert
Returns
Reference to the mapped value of the element with key equivalent to key

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