Container System 0.1.0
High-performance C++20 type-safe container framework with SIMD-accelerated serialization
Loading...
Searching...
No Matches
kcenon::container::async::async_container Class Reference

Async wrapper for value_container operations. More...

#include <async_container.h>

Collaboration diagram for kcenon::container::async::async_container:
Collaboration graph

Public Member Functions

 async_container (std::shared_ptr< value_container > container)
 Construct async_container with existing container.
 
 async_container ()
 Construct async_container with new empty container.
 
 async_container (async_container &&other) noexcept=default
 Move constructor.
 
async_containeroperator= (async_container &&other) noexcept=default
 Move assignment operator.
 
 ~async_container ()=default
 Destructor.
 
 async_container (const async_container &)=delete
 
async_containeroperator= (const async_container &)=delete
 
std::shared_ptr< value_container > get_container () const noexcept
 Get the underlying container.
 
void set_container (std::shared_ptr< value_container > container) noexcept
 Set the underlying container.
 
task< std::vector< uint8_t > > serialize_async () const
 Serialize container to byte array asynchronously.
 
task< std::string > serialize_string_async () const
 Serialize container to string asynchronously.
 
template<typename T >
async_containerset (std::string_view key, T &&value)
 Set a value in the container.
 
template<typename T >
std::optional< T > get (std::string_view key) const
 Get a value from the container.
 
bool contains (std::string_view key) const noexcept
 Check if container contains a key.
 
task< bool > load_async (std::string_view path, progress_callback callback=nullptr)
 Load container from file asynchronously.
 
task< bool > save_async (std::string_view path, progress_callback callback=nullptr)
 Save container to file asynchronously.
 
generator< std::vector< uint8_t > > serialize_chunked (size_t chunk_size=64 *1024) const
 Serialize container in chunks using generator.
 

Static Public Member Functions

static task< std::shared_ptr< value_container > > deserialize_async (std::span< const uint8_t > data)
 Deserialize from byte array asynchronously.
 
static task< std::shared_ptr< value_container > > deserialize_string_async (std::string_view data)
 Deserialize from string asynchronously.
 
static task< std::shared_ptr< value_container > > deserialize_streaming (std::span< const uint8_t > data, bool is_final=true)
 Deserialize container progressively from chunks.
 

Private Attributes

std::shared_ptr< value_container > container_
 

Detailed Description

Async wrapper for value_container operations.

This class wraps a value_container and provides async versions of serialization and deserialization operations using C++20 coroutines.

The async operations offload CPU-bound work to a separate thread, allowing the calling coroutine to yield and resume when the operation completes.

Properties:

  • Thread-safe wrapper for value_container
  • Supports both Result-based and exception-based APIs
  • Movable but not copyable
Examples
asio_integration_example.cpp, and async_coroutine_example.cpp.

Definition at line 164 of file async_container.h.

Constructor & Destructor Documentation

◆ async_container() [1/4]

kcenon::container::async::async_container::async_container ( std::shared_ptr< value_container > container)
inlineexplicit

Construct async_container with existing container.

Parameters
containerShared pointer to value_container

Definition at line 171 of file async_container.h.

172 : container_(std::move(container))
173 {
174 }
std::shared_ptr< value_container > container_

◆ async_container() [2/4]

kcenon::container::async::async_container::async_container ( )
inline

Construct async_container with new empty container.

Definition at line 179 of file async_container.h.

180 : container_(std::make_shared<value_container>())
181 {
182 }

◆ async_container() [3/4]

kcenon::container::async::async_container::async_container ( async_container && other)
defaultnoexcept

Move constructor.

◆ ~async_container()

kcenon::container::async::async_container::~async_container ( )
default

Destructor.

◆ async_container() [4/4]

kcenon::container::async::async_container::async_container ( const async_container & )
delete

Member Function Documentation

◆ contains()

bool kcenon::container::async::async_container::contains ( std::string_view key) const
inlinenodiscardnoexcept

Check if container contains a key.

Parameters
keyKey name to check
Returns
true if key exists

Definition at line 346 of file async_container.h.

347 {
348 return container_->contains(key);
349 }

References container_.

◆ deserialize_async()

task< std::shared_ptr< value_container > > kcenon::container::async::async_container::deserialize_async ( std::span< const uint8_t > data)
inlinestaticnodiscard

Deserialize from byte array asynchronously.

Creates a new container from serialized data.

Parameters
dataSpan of bytes containing serialized container data
Returns
task containing Result with deserialized container or error
std::vector<uint8_t> data = receive_data();
auto result = co_await async_container::deserialize_async(data);
if (result.is_ok()) {
auto container = result.value();
// Use deserialized container
}
static task< std::shared_ptr< value_container > > deserialize_async(std::span< const uint8_t > data)
Deserialize from byte array asynchronously.

Definition at line 891 of file async_container.h.

892 {
893 std::vector<uint8_t> data_copy(data.begin(), data.end());
894 auto result = co_await detail::make_async_awaitable(
895 [data_copy = std::move(data_copy)]() {
896 auto container = std::make_shared<value_container>(data_copy, false);
897 return container;
898 });
899 co_return result;
900 }
auto make_async_awaitable(F &&func) -> async_awaitable< std::invoke_result_t< F > >

References kcenon::container::async::detail::make_async_awaitable().

Here is the call graph for this function:

◆ deserialize_streaming()

task< std::shared_ptr< value_container > > kcenon::container::async::async_container::deserialize_streaming ( std::span< const uint8_t > data,
bool is_final = true )
inlinestaticnodiscard

Deserialize container progressively from chunks.

Creates a streaming deserializer that can accept data chunks progressively and build the container incrementally.

Parameters
dataSpan of bytes to deserialize (can be partial)
is_finalTrue if this is the final chunk
Returns
task containing Result with deserialized container or error
streaming_deserializer deserializer;
while (has_more_data()) {
auto chunk = receive_chunk();
bool is_last = !has_more_data();
auto result = co_await deserializer.feed(chunk, is_last);
if (is_last && result.is_ok()) {
// Container is complete
}
}
Async wrapper for value_container operations.

Definition at line 1104 of file async_container.h.

1107 {
1108 // For streaming deserialization, we accumulate data until we have a complete
1109 // container. In this simplified implementation, we require is_final to be true
1110 // to actually deserialize.
1111 if (!is_final) {
1112 co_return nullptr;
1113 }
1114
1115 std::vector<uint8_t> data_copy(data.begin(), data.end());
1116 auto result = co_await detail::make_async_awaitable(
1117 [data_copy = std::move(data_copy)]() {
1118 auto container = std::make_shared<value_container>(data_copy, false);
1119 return container;
1120 });
1121 co_return result;
1122 }

References kcenon::container::async::detail::make_async_awaitable().

Here is the call graph for this function:

◆ deserialize_string_async()

task< std::shared_ptr< value_container > > kcenon::container::async::async_container::deserialize_string_async ( std::string_view data)
inlinestaticnodiscard

Deserialize from string asynchronously.

Parameters
dataString containing serialized container data
Returns
task containing Result with deserialized container or error

Definition at line 903 of file async_container.h.

904 {
905 std::string data_copy(data);
906 auto result = co_await detail::make_async_awaitable(
907 [data_copy = std::move(data_copy)]() {
908 auto container = std::make_shared<value_container>(data_copy, false);
909 return container;
910 });
911 co_return result;
912 }

References kcenon::container::async::detail::make_async_awaitable().

Here is the call graph for this function:

◆ get()

template<typename T >
std::optional< T > kcenon::container::async::async_container::get ( std::string_view key) const
inlinenodiscard

Get a value from the container.

Template Parameters
TExpected value type
Parameters
keyKey name
Returns
Optional containing the value if found and type matches

Definition at line 329 of file async_container.h.

330 {
331 auto val = container_->get(key);
332 if (!val) {
333 return std::nullopt;
334 }
335 if (auto* ptr = std::get_if<T>(&val->data)) {
336 return *ptr;
337 }
338 return std::nullopt;
339 }

References container_.

◆ get_container()

std::shared_ptr< value_container > kcenon::container::async::async_container::get_container ( ) const
inlinenodiscardnoexcept

Get the underlying container.

Returns
Shared pointer to the wrapped value_container
Examples
async_coroutine_example.cpp.

Definition at line 207 of file async_container.h.

208 {
209 return container_;
210 }

References container_.

◆ load_async()

task< bool > kcenon::container::async::async_container::load_async ( std::string_view path,
progress_callback callback = nullptr )
inlinenodiscard

Load container from file asynchronously.

Reads the file in a worker thread and deserializes the content into the container.

Parameters
pathFile path to load from
callbackOptional progress callback
Returns
task containing VoidResult indicating success or error
auto result = co_await cont.load_async("data.bin");
if (result.is_ok()) {
// Container now contains loaded data
}
task< bool > load_async(std::string_view path, progress_callback callback=nullptr)
Load container from file asynchronously.
Examples
async_coroutine_example.cpp.

Definition at line 915 of file async_container.h.

916 {
917 auto container = container_;
918 std::string path_str(path);
919 auto result = co_await detail::make_async_awaitable(
920 [container, path_str, callback]() {
921 std::ifstream file(path_str, std::ios::binary | std::ios::ate);
922 if (!file) {
923 return false;
924 }
925
926 auto size = file.tellg();
927 if (size < 0) {
928 return false;
929 }
930 file.seekg(0, std::ios::beg);
931
932 std::vector<uint8_t> buffer(static_cast<size_t>(size));
933 const size_t chunk_size = 64 * 1024; // 64KB chunks
934 size_t bytes_read = 0;
935
936 while (bytes_read < static_cast<size_t>(size)) {
937 size_t to_read = std::min(chunk_size,
938 static_cast<size_t>(size) - bytes_read);
939 file.read(reinterpret_cast<char*>(buffer.data() + bytes_read),
940 static_cast<std::streamsize>(to_read));
941 if (!file) {
942 return false;
943 }
944 bytes_read += to_read;
945 if (callback) {
946 callback(bytes_read, static_cast<size_t>(size));
947 }
948 }
949
950 container->deserialize(buffer, false);
951 return true;
952 });
953 co_return result;
954 }

References container_, and kcenon::container::async::detail::make_async_awaitable().

Here is the call graph for this function:

◆ operator=() [1/2]

async_container & kcenon::container::async::async_container::operator= ( async_container && other)
defaultnoexcept

Move assignment operator.

◆ operator=() [2/2]

async_container & kcenon::container::async::async_container::operator= ( const async_container & )
delete

◆ save_async()

task< bool > kcenon::container::async::async_container::save_async ( std::string_view path,
progress_callback callback = nullptr )
inlinenodiscard

Save container to file asynchronously.

Serializes the container and writes to file in a worker thread.

Parameters
pathFile path to save to
callbackOptional progress callback
Returns
task containing VoidResult indicating success or error
cont.set("key", "value");
auto result = co_await cont.save_async("output.bin");
if (result.is_ok()) {
// Data saved successfully
}
async_container & set(std::string_view key, T &&value)
Set a value in the container.
task< bool > save_async(std::string_view path, progress_callback callback=nullptr)
Save container to file asynchronously.

Definition at line 957 of file async_container.h.

958 {
959 auto container = container_;
960 std::string path_str(path);
961 auto result = co_await detail::make_async_awaitable(
962 [container, path_str, callback]() {
963 auto serialize_result = container->serialize(serialization_format::binary);
964 if (!serialize_result.is_ok()) {
965 return false;
966 }
967 auto data = serialize_result.value();
968 if (data.empty()) {
969 return false;
970 }
971
972 std::ofstream file(path_str, std::ios::binary);
973 if (!file) {
974 return false;
975 }
976
977 const size_t chunk_size = 64 * 1024; // 64KB chunks
978 size_t bytes_written = 0;
979 const size_t total_size = data.size();
980
981 while (bytes_written < total_size) {
982 size_t to_write = std::min(chunk_size, total_size - bytes_written);
983 file.write(reinterpret_cast<const char*>(data.data() + bytes_written),
984 static_cast<std::streamsize>(to_write));
985 if (!file) {
986 return false;
987 }
988 bytes_written += to_write;
989 if (callback) {
990 callback(bytes_written, total_size);
991 }
992 }
993
994 return true;
995 });
996 co_return result;
997 }

References container_, and kcenon::container::async::detail::make_async_awaitable().

Here is the call graph for this function:

◆ serialize_async()

task< std::vector< uint8_t > > kcenon::container::async::async_container::serialize_async ( ) const
inlinenodiscard

Serialize container to byte array asynchronously.

Offloads serialization to a worker thread and returns a task that completes when serialization is done.

Returns
task containing Result with serialized bytes or error
async_container cont(my_container);
auto result = co_await cont.serialize_async();
if (result.is_ok()) {
auto& bytes = result.value();
// Use serialized bytes
} else {
// Handle error
}

Definition at line 869 of file async_container.h.

870 {
871 auto container = container_;
872 auto result = co_await detail::make_async_awaitable([container]() {
873 auto serialize_result = container->serialize(serialization_format::binary);
874 return serialize_result.is_ok() ? serialize_result.value() : std::vector<uint8_t>{};
875 });
876 co_return result;
877 }

References container_, and kcenon::container::async::detail::make_async_awaitable().

Here is the call graph for this function:

◆ serialize_chunked()

generator< std::vector< uint8_t > > kcenon::container::async::async_container::serialize_chunked ( size_t chunk_size = 64 * 1024) const
inlinenodiscard

Serialize container in chunks using generator.

Returns a generator that yields chunks of serialized data. Useful for streaming large containers without loading everything into memory at once.

Parameters
chunk_sizeSize of each chunk in bytes (default: 64KB)
Returns
Generator yielding chunks of serialized data
async_container cont(my_container);
for (auto chunk : cont.serialize_chunked(32 * 1024)) {
network_send(chunk);
}

Definition at line 1079 of file async_container.h.

1080 {
1081 auto serialize_result = container_->serialize(serialization_format::binary);
1082 if (!serialize_result.is_ok()) {
1083 co_return;
1084 }
1085 auto full_data = serialize_result.value();
1086 if (full_data.empty()) {
1087 co_return;
1088 }
1089
1090 const size_t total_size = full_data.size();
1091 size_t offset = 0;
1092
1093 while (offset < total_size) {
1094 size_t current_chunk_size = std::min(chunk_size, total_size - offset);
1095 std::vector<uint8_t> chunk(
1096 full_data.begin() + static_cast<std::ptrdiff_t>(offset),
1097 full_data.begin() + static_cast<std::ptrdiff_t>(offset + current_chunk_size));
1098 offset += current_chunk_size;
1099 co_yield chunk;
1100 }
1101 }

References container_.

◆ serialize_string_async()

task< std::string > kcenon::container::async::async_container::serialize_string_async ( ) const
inlinenodiscard

Serialize container to string asynchronously.

Returns
task containing Result with serialized string or error

Definition at line 880 of file async_container.h.

881 {
882 auto container = container_;
883 auto result = co_await detail::make_async_awaitable([container]() {
884 auto serialize_result = container->serialize_string(serialization_format::binary);
885 return serialize_result.is_ok() ? serialize_result.value() : std::string{};
886 });
887 co_return result;
888 }

References container_, and kcenon::container::async::detail::make_async_awaitable().

Here is the call graph for this function:

◆ set()

template<typename T >
async_container & kcenon::container::async::async_container::set ( std::string_view key,
T && value )
inline

Set a value in the container.

Template Parameters
TValue type
Parameters
keyKey name
valueValue to set
Returns
Reference to this async_container for chaining

Definition at line 316 of file async_container.h.

317 {
318 container_->set(key, std::forward<T>(value));
319 return *this;
320 }

References container_.

◆ set_container()

void kcenon::container::async::async_container::set_container ( std::shared_ptr< value_container > container)
inlinenoexcept

Set the underlying container.

Parameters
containerNew container to wrap

Definition at line 216 of file async_container.h.

217 {
218 container_ = std::move(container);
219 }

References container_.

Member Data Documentation

◆ container_

std::shared_ptr<value_container> kcenon::container::async::async_container::container_
private

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