Template Class ThreadSafeMap
Defined in File ThreadSafeMap.hpp
Class Documentation
-
template<typename Key, typename Value>
class ThreadSafeMap Implements a conditionally thread-safe map.
The ThreadSafeMap class implements a map that can be used in a thread-safe or non-thread-safe way. It uses a std::map as the underlying data structure and a mutex that is conditionally locked.
- Template Parameters:
Key – The key type of the map
Value – The value type of the map
Public Functions
-
inline explicit ThreadSafeMap(const bool safe)
Construct a new ThreadSafeMap object.
- Parameters:
safe – Whether the map should be thread-safe
-
template<typename ...Args>
inline bool try_emplace(const Key &key, Args&&... args) Wrapper around std::map::try_emplace.
- Parameters:
key – The key to insert
args – Arguments to forward to the constructor of the value
-
template<typename ...Args>
inline bool try_emplace(Key &&key, Args&&... args) Wrapper around std::map::try_emplace.
- Parameters:
key – The key to insert
args – Arguments to forward to the constructor of the value
-
inline std::size_t erase(const Key &key)
Wrapper around std::map::erase.
- Parameters:
key – The key to erase
-
inline void clear()
Wrapper around std::map::clear.
-
inline std::size_t size() const
Wrapper around std::map::size.
- Returns:
The size of the map
-
inline bool empty() const
Wrapper around std::map::empty.
- Returns:
True if the map is empty, false otherwise
-
inline bool contains(const Key &key) const
Wrapper around std::map::contains.
- Parameters:
key – The key to check for
- Returns:
True if the map contains the key, false otherwise
-
inline auto apply(const Key &key, const std::invocable<const Value&> auto &func) const -> decltype(func(std::declval<const Value&>()))
Use a value in the map in a (potentially) thread-safe way.
Locks the mutex for the duration of the function call if the map is thread-safe.
- Parameters:
key – The key to use
func – The function to apply to the value
- Returns:
The result of the function
-
inline auto apply(const Key &key, const std::invocable<Value&> auto &func) -> decltype(func(std::declval<Value&>()))
Use a value in the map in a (potentially) thread-safe way.
Locks the mutex for the duration of the function call if the map is thread-safe.
- Parameters:
key – The key to use
func – The function to apply to the value
- Returns:
The result of the function
-
inline void apply_all(const std::invocable<const Value&> auto &func) const
Apply function to all elements in a (potentially) thread-safe way.
Locks the mutex for the duration of the function call if the map is thread-safe.
- Parameters:
func – The function to apply to the value
- Returns:
The result of the function
-
inline void apply_all(const std::invocable<Value&> auto &func)
Apply function to all elements in a (potentially) thread-safe way.
Locks the mutex for the duration of the function call if the map is thread-safe.
- Parameters:
func – The function to apply to the value
- Returns:
The result of the function