|
Container System 0.1.0
High-performance C++20 type-safe container framework with SIMD-accelerated serialization
|
Thread-safe container with lock optimization. More...
#include <thread_safe_container.h>


Classes | |
| struct | Statistics |
| Get statistics. More... | |
Public Types | |
| using | value_map = std::unordered_map<std::string, value> |
| using | const_iterator = value_map::const_iterator |
| using | iterator = value_map::iterator |
Public Member Functions | |
| thread_safe_container ()=default | |
| Default constructor. | |
| thread_safe_container (std::initializer_list< std::pair< std::string, value > > init) | |
| Construct with initial values. | |
| thread_safe_container (const thread_safe_container &other) | |
| Copy constructor (thread-safe) | |
| thread_safe_container (thread_safe_container &&other) noexcept | |
| Move constructor. | |
| thread_safe_container & | operator= (const thread_safe_container &other) |
| Copy assignment (thread-safe) | |
| thread_safe_container & | operator= (thread_safe_container &&other) noexcept |
| Move assignment. | |
| std::optional< value > | get (std::string_view key) const |
| Get value by key (thread-safe read) | |
| template<concepts::ValueVariantType T> | |
| std::optional< T > | get_typed (std::string_view key) const |
| Get typed value by key. | |
| void | set (std::string_view key, value value) |
| Set value for key (thread-safe write) | |
| template<concepts::ValueVariantType T> | |
| void | set_typed (std::string_view key, T &&val) |
| Set typed value for key. | |
| bool | remove (std::string_view key) |
| Remove value by key. | |
| void | clear () |
| Clear all values. | |
| size_t | size () const |
| Get the number of values. | |
| bool | empty () const |
| Check if container is empty. | |
| bool | contains (std::string_view key) const |
| Check if key exists. | |
| std::vector< std::string > | keys () const |
| Get all keys. | |
| void | set_variant (const value &val) |
| Set a value directly using variant value. | |
| std::optional< value > | get_variant (const std::string &key) const |
| Get a value as variant value. | |
| void | set_container (const std::string &key, std::shared_ptr< thread_safe_container > container) |
| Set a nested container. | |
| std::shared_ptr< thread_safe_container > | get_container (const std::string &key) const |
| Get a nested container. | |
| template<concepts::KeyValueCallback Func> | |
| void | for_each (Func &&func) const |
| Apply a function to all values (read-only) | |
| template<concepts::MutableKeyValueCallback Func> | |
| void | for_each_mut (Func &&func) |
| Apply a function to all values (mutable) | |
| template<concepts::ValueMapCallback< value_map > Func> | |
| void | bulk_update (Func &&updater) |
| Bulk update operation with minimal lock contention. | |
| template<concepts::ConstValueMapCallback< value_map > Func> | |
| auto | bulk_read (Func &&reader) const |
| Bulk read operation. | |
| bool | compare_exchange (std::string_view key, const value &expected, const value &desired) |
| Atomic compare and swap. | |
| Statistics | get_statistics () const |
| std::string | to_json () const |
| Serialize container to JSON. | |
| std::vector< uint8_t > | serialize () const |
| Serialize container to binary. | |
| value & | operator[] (const std::string &key) |
| Array-style access (creates if not exists) | |
| std::shared_ptr< lockfree_container_reader > | create_lockfree_reader () |
| Create a lock-free reader for this container. | |
| std::shared_ptr< auto_refresh_reader > | create_auto_refresh_reader (std::chrono::milliseconds refresh_interval) |
| Create an auto-refreshing lock-free reader. | |
Static Public Member Functions | |
| static std::shared_ptr< thread_safe_container > | deserialize (const std::vector< uint8_t > &data) |
| Deserialize from binary. | |
Private Attributes | |
| std::shared_mutex | mutex_ |
| value_map | values_ |
| std::atomic< size_t > | read_count_ {0} |
| std::atomic< size_t > | write_count_ {0} |
| std::atomic< size_t > | bulk_read_count_ {0} |
| std::atomic< size_t > | bulk_write_count_ {0} |
Thread-safe container with lock optimization.
This container provides thread-safe access to variant values with optimized locking strategies for different access patterns.
Definition at line 31 of file thread_safe_container.h.
| using kcenon::container::thread_safe_container::const_iterator = value_map::const_iterator |
Definition at line 35 of file thread_safe_container.h.
| using kcenon::container::thread_safe_container::iterator = value_map::iterator |
Definition at line 36 of file thread_safe_container.h.
| using kcenon::container::thread_safe_container::value_map = std::unordered_map<std::string, value> |
Definition at line 34 of file thread_safe_container.h.
|
default |
Default constructor.
| kcenon::container::thread_safe_container::thread_safe_container | ( | std::initializer_list< std::pair< std::string, value > > | init | ) |
Construct with initial values.
Definition at line 14 of file thread_safe_container.cpp.
References values_.
| kcenon::container::thread_safe_container::thread_safe_container | ( | const thread_safe_container & | other | ) |
Copy constructor (thread-safe)
Definition at line 22 of file thread_safe_container.cpp.
References bulk_read_count_, bulk_write_count_, mutex_, read_count_, values_, and write_count_.
|
noexcept |
Move constructor.
Definition at line 32 of file thread_safe_container.cpp.
|
inline |
Bulk read operation.
| Func | Function type satisfying ConstValueMapCallback concept |
| reader | Function to perform bulk reads |
Definition at line 214 of file thread_safe_container.h.
References bulk_read_count_, mutex_, and values_.
|
inline |
Bulk update operation with minimal lock contention.
| Func | Function type satisfying ValueMapCallback concept |
| updater | Function to perform bulk updates |
Definition at line 201 of file thread_safe_container.h.
References bulk_write_count_, mutex_, and values_.
| void kcenon::container::thread_safe_container::clear | ( | ) |
Clear all values.
Definition at line 104 of file thread_safe_container.cpp.
References mutex_, values_, and write_count_.
| bool kcenon::container::thread_safe_container::compare_exchange | ( | std::string_view | key, |
| const value & | expected, | ||
| const value & | desired ) |
Atomic compare and swap.
| key | The key to update |
| expected | The expected current value |
| desired | The desired new value |
Definition at line 185 of file thread_safe_container.cpp.
References mutex_, values_, and write_count_.
| bool kcenon::container::thread_safe_container::contains | ( | std::string_view | key | ) | const |
Check if key exists.
Definition at line 123 of file thread_safe_container.cpp.
References mutex_, read_count_, and values_.
|
inline |
Create an auto-refreshing lock-free reader.
The returned reader automatically refreshes its snapshot at the specified interval using a background thread. This is useful for scenarios where you want lock-free reads with automatic updates without managing the refresh manually.
| refresh_interval | The interval between automatic refreshes |
Definition at line 802 of file thread_safe_container.h.
|
inline |
Create a lock-free reader for this container.
The returned reader provides truly lock-free reads using the RCU pattern. Call refresh() on the reader periodically to update its snapshot.
Definition at line 797 of file thread_safe_container.h.
|
static |
Deserialize from binary.
Definition at line 282 of file thread_safe_container.cpp.
References kcenon::container::value::deserialize().
Referenced by kcenon::container::value::deserialize_data().


| bool kcenon::container::thread_safe_container::empty | ( | ) | const |
|
inline |
Apply a function to all values (read-only)
| Func | Function type satisfying KeyValueCallback concept |
| func | Function to apply to each key-value pair |
Definition at line 175 of file thread_safe_container.h.
|
inline |
Apply a function to all values (mutable)
| Func | Function type satisfying MutableKeyValueCallback concept |
| func | Function to apply to each key-value pair |
Definition at line 188 of file thread_safe_container.h.
| std::optional< value > kcenon::container::thread_safe_container::get | ( | std::string_view | key | ) | const |
Get value by key (thread-safe read)
| key | The key to look up |
Definition at line 71 of file thread_safe_container.cpp.
References mutex_, read_count_, and values_.
| std::shared_ptr< thread_safe_container > kcenon::container::thread_safe_container::get_container | ( | const std::string & | key | ) | const |
Get a nested container.
| key | The key to look up |
Definition at line 172 of file thread_safe_container.cpp.
References mutex_, read_count_, and values_.
| thread_safe_container::Statistics kcenon::container::thread_safe_container::get_statistics | ( | ) | const |
Definition at line 202 of file thread_safe_container.cpp.
References bulk_read_count_, bulk_write_count_, mutex_, read_count_, values_, and write_count_.
|
inline |
Get typed value by key.
| T | The expected type (must be a valid variant type) |
| key | The key to look up |
Definition at line 82 of file thread_safe_container.h.
| std::optional< value > kcenon::container::thread_safe_container::get_variant | ( | const std::string & | key | ) | const |
Get a value as variant value.
| key | The key to look up |
Definition at line 152 of file thread_safe_container.cpp.
References mutex_, read_count_, and values_.
| std::vector< std::string > kcenon::container::thread_safe_container::keys | ( | ) | const |
Get all keys.
Definition at line 130 of file thread_safe_container.cpp.
References mutex_, read_count_, and values_.
| thread_safe_container & kcenon::container::thread_safe_container::operator= | ( | const thread_safe_container & | other | ) |
Copy assignment (thread-safe)
Definition at line 47 of file thread_safe_container.cpp.
References mutex_, values_, and write_count_.
|
noexcept |
Move assignment.
Definition at line 59 of file thread_safe_container.cpp.
| value & kcenon::container::thread_safe_container::operator[] | ( | const std::string & | key | ) |
Array-style access (creates if not exists)
Definition at line 338 of file thread_safe_container.cpp.
References mutex_, values_, and write_count_.
| bool kcenon::container::thread_safe_container::remove | ( | std::string_view | key | ) |
Remove value by key.
| key | The key to remove |
Definition at line 91 of file thread_safe_container.cpp.
References mutex_, values_, and write_count_.
| std::vector< uint8_t > kcenon::container::thread_safe_container::serialize | ( | ) | const |
Serialize container to binary.
Definition at line 248 of file thread_safe_container.cpp.
References mutex_, read_count_, kcenon::container::value::serialize(), and values_.

| void kcenon::container::thread_safe_container::set | ( | std::string_view | key, |
| value | value ) |
Set value for key (thread-safe write)
| key | The key to set |
| value | The value to store |
Definition at line 83 of file thread_safe_container.cpp.
References mutex_, values_, and write_count_.
Referenced by set_typed().

| void kcenon::container::thread_safe_container::set_container | ( | const std::string & | key, |
| std::shared_ptr< thread_safe_container > | container ) |
Set a nested container.
| key | The key for the nested container |
| container | The container to store |
Definition at line 164 of file thread_safe_container.cpp.
References mutex_, values_, and write_count_.
|
inline |
Set typed value for key.
| T | The value type (must be a valid variant type) |
| key | The key to set |
| value | The value to store |
Definition at line 105 of file thread_safe_container.h.
References set().

| void kcenon::container::thread_safe_container::set_variant | ( | const value & | val | ) |
Set a value directly using variant value.
| val | The value to store (key is extracted from value's name) |
Definition at line 145 of file thread_safe_container.cpp.
References mutex_, kcenon::container::value::name(), values_, and write_count_.

| size_t kcenon::container::thread_safe_container::size | ( | ) | const |
| std::string kcenon::container::thread_safe_container::to_json | ( | ) | const |
Serialize container to JSON.
Definition at line 214 of file thread_safe_container.cpp.
References mutex_, read_count_, kcenon::container::value::to_json(), and values_.

|
mutableprivate |
Definition at line 303 of file thread_safe_container.h.
Referenced by bulk_read(), get_statistics(), and thread_safe_container().
|
mutableprivate |
Definition at line 304 of file thread_safe_container.h.
Referenced by bulk_update(), get_statistics(), and thread_safe_container().
|
mutableprivate |
Definition at line 297 of file thread_safe_container.h.
Referenced by bulk_read(), bulk_update(), clear(), compare_exchange(), contains(), empty(), for_each(), for_each_mut(), get(), get_container(), get_statistics(), get_typed(), get_variant(), keys(), operator=(), operator[](), remove(), serialize(), set(), set_container(), set_variant(), size(), thread_safe_container(), and to_json().
|
mutableprivate |
Definition at line 301 of file thread_safe_container.h.
Referenced by contains(), get(), get_container(), get_statistics(), get_variant(), keys(), serialize(), thread_safe_container(), and to_json().
|
private |
Definition at line 298 of file thread_safe_container.h.
Referenced by bulk_read(), bulk_update(), clear(), compare_exchange(), contains(), empty(), for_each(), for_each_mut(), get(), get_container(), get_statistics(), get_typed(), get_variant(), keys(), operator=(), operator[](), remove(), serialize(), set(), set_container(), set_variant(), size(), thread_safe_container(), thread_safe_container(), and to_json().
|
mutableprivate |
Definition at line 302 of file thread_safe_container.h.
Referenced by clear(), compare_exchange(), get_statistics(), operator=(), operator[](), remove(), set(), set_container(), set_variant(), and thread_safe_container().