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

Namespaces

namespace  detail
 

Classes

class  async_container
 Async wrapper for value_container operations. More...
 
class  async_executor_context
 Global executor for async operations. More...
 
class  executor_context_guard
 RAII guard for setting executor context. More...
 
class  generator
 Forward declaration of generator. More...
 
class  task
 Forward declaration of task. More...
 

Typedefs

using progress_callback = std::function<void(size_t bytes_processed, size_t total_bytes)>
 Progress callback type for async file operations.
 
using executor_ptr = std::nullptr_t
 Executor type for async operations.
 

Functions

task< std::vector< uint8_t > > read_file_async (std::string_view path, progress_callback callback=nullptr)
 Read file contents asynchronously.
 
task< bool > write_file_async (std::string_view path, std::span< const uint8_t > data, progress_callback callback=nullptr)
 Write data to file asynchronously.
 
template<typename Range >
generator< typename std::ranges::range_value_t< Range > > from_range (Range &&range)
 Create a generator from a range.
 
template<typename T >
generator< T > take (generator< T > gen, size_t count)
 Create a generator that yields a fixed number of elements.
 
template<typename T >
task< std::decay_t< T > > make_ready_task (T &&value)
 Create a task that returns a value immediately.
 
task< void > make_ready_task ()
 Create a task that completes immediately with no value.
 
template<typename T >
task< T > make_exceptional_task (std::exception_ptr ex)
 Create a task that throws an exception.
 
template<>
task< void > make_exceptional_task< void > (std::exception_ptr ex)
 Specialization for void.
 

Variables

constexpr bool has_coroutine_support = false
 Check if coroutines are available at compile time.
 

Typedef Documentation

◆ executor_ptr

Executor type for async operations.

When common_system is available, uses IExecutor interface. Otherwise, falls back to simple std::thread-based execution.

Definition at line 55 of file thread_pool_executor.h.

◆ progress_callback

using kcenon::container::async::progress_callback = std::function<void(size_t bytes_processed, size_t total_bytes)>

Progress callback type for async file operations.

Parameters
bytes_processedNumber of bytes processed so far
total_bytesTotal number of bytes (0 if unknown)

Definition at line 54 of file async_container.h.

Function Documentation

◆ from_range()

template<typename Range >
generator< typename std::ranges::range_value_t< Range > > kcenon::container::async::from_range ( Range && range)

Create a generator from a range.

Template Parameters
RangeThe range type
Parameters
rangeThe range to iterate
Returns
A generator that yields each element of the range

Definition at line 347 of file generator.h.

348 {
349 for (auto&& element : range)
350 {
351 co_yield std::forward<decltype(element)>(element);
352 }
353 }

◆ make_exceptional_task()

template<typename T >
task< T > kcenon::container::async::make_exceptional_task ( std::exception_ptr ex)

Create a task that throws an exception.

Template Parameters
TThe return type of the task
Parameters
exThe exception pointer to throw
Returns
A task that throws the exception when awaited

Definition at line 407 of file task.h.

408 {
409 std::rethrow_exception(ex);
410 // Unreachable, but needed for coroutine
411 co_return T{};
412 }

◆ make_exceptional_task< void >()

template<>
task< void > kcenon::container::async::make_exceptional_task< void > ( std::exception_ptr ex)
inline

Specialization for void.

Definition at line 407 of file task.h.

419 {
420 std::rethrow_exception(ex);
421 co_return;
422 }

◆ make_ready_task() [1/2]

task< void > kcenon::container::async::make_ready_task ( )
inline

Create a task that completes immediately with no value.

Returns
A task that immediately completes

Definition at line 394 of file task.h.

395 {
396 co_return;
397 }

◆ make_ready_task() [2/2]

template<typename T >
task< std::decay_t< T > > kcenon::container::async::make_ready_task ( T && value)

Create a task that returns a value immediately.

Template Parameters
TThe value type
Parameters
valueThe value to return
Returns
A task that immediately returns the value

Definition at line 384 of file task.h.

385 {
386 co_return std::forward<T>(value);
387 }
Enhanced type-safe value with perfect legacy compatibility.
Definition value.h:129

◆ read_file_async()

task< std::vector< uint8_t > > kcenon::container::async::read_file_async ( std::string_view path,
progress_callback callback = nullptr )
inlinenodiscard

Read file contents asynchronously.

Reads entire file content in a worker thread.

Parameters
pathFile path to read from
callbackOptional progress callback
Returns
task containing Result with file bytes or error
auto result = co_await read_file_async("data.bin");
if (result.is_ok()) {
auto& bytes = result.value();
// Process bytes
}
task< std::vector< uint8_t > > read_file_async(std::string_view path, progress_callback callback=nullptr)
Read file contents asynchronously.

Definition at line 1000 of file async_container.h.

1001 {
1002 std::string path_str(path);
1003 auto result = co_await detail::make_async_awaitable(
1004 [path_str, callback]() {
1005 std::ifstream file(path_str, std::ios::binary | std::ios::ate);
1006 if (!file) {
1007 return std::vector<uint8_t>{};
1008 }
1009
1010 auto size = file.tellg();
1011 if (size < 0) {
1012 return std::vector<uint8_t>{};
1013 }
1014 file.seekg(0, std::ios::beg);
1015
1016 std::vector<uint8_t> buffer(static_cast<size_t>(size));
1017 const size_t chunk_size = 64 * 1024; // 64KB chunks
1018 size_t bytes_read = 0;
1019
1020 while (bytes_read < static_cast<size_t>(size)) {
1021 size_t to_read = std::min(chunk_size,
1022 static_cast<size_t>(size) - bytes_read);
1023 file.read(reinterpret_cast<char*>(buffer.data() + bytes_read),
1024 static_cast<std::streamsize>(to_read));
1025 if (!file) {
1026 return std::vector<uint8_t>{};
1027 }
1028 bytes_read += to_read;
1029 if (callback) {
1030 callback(bytes_read, static_cast<size_t>(size));
1031 }
1032 }
1033
1034 return buffer;
1035 });
1036 co_return result;
1037 }

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

Here is the call graph for this function:

◆ take()

template<typename T >
generator< T > kcenon::container::async::take ( generator< T > gen,
size_t count )

Create a generator that yields a fixed number of elements.

Template Parameters
TThe element type
Parameters
genThe source generator
countMaximum number of elements to yield
Returns
A generator limited to count elements

Definition at line 364 of file generator.h.

365 {
366 size_t yielded = 0;
367 for (auto&& element : gen)
368 {
369 if (yielded++ >= count)
370 {
371 break;
372 }
373 co_yield std::forward<decltype(element)>(element);
374 }
375 }

◆ write_file_async()

task< bool > kcenon::container::async::write_file_async ( std::string_view path,
std::span< const uint8_t > data,
progress_callback callback = nullptr )
inlinenodiscard

Write data to file asynchronously.

Writes data to file in a worker thread.

Parameters
pathFile path to write to
dataData to write
callbackOptional progress callback
Returns
task containing VoidResult indicating success or error
std::vector<uint8_t> data = {1, 2, 3, 4, 5};
auto result = co_await write_file_async("output.bin", data);
if (result.is_ok()) {
// File written successfully
}
task< bool > write_file_async(std::string_view path, std::span< const uint8_t > data, progress_callback callback=nullptr)
Write data to file asynchronously.

Definition at line 1040 of file async_container.h.

1042 {
1043 std::string path_str(path);
1044 std::vector<uint8_t> data_copy(data.begin(), data.end());
1045 auto result = co_await detail::make_async_awaitable(
1046 [path_str, data_copy = std::move(data_copy), callback]() {
1047 std::ofstream file(path_str, std::ios::binary);
1048 if (!file) {
1049 return false;
1050 }
1051
1052 const size_t chunk_size = 64 * 1024; // 64KB chunks
1053 size_t bytes_written = 0;
1054 const size_t total_size = data_copy.size();
1055
1056 while (bytes_written < total_size) {
1057 size_t to_write = std::min(chunk_size, total_size - bytes_written);
1058 file.write(reinterpret_cast<const char*>(data_copy.data() + bytes_written),
1059 static_cast<std::streamsize>(to_write));
1060 if (!file) {
1061 return false;
1062 }
1063 bytes_written += to_write;
1064 if (callback) {
1065 callback(bytes_written, total_size);
1066 }
1067 }
1068
1069 return true;
1070 });
1071 co_return result;
1072 }

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

Here is the call graph for this function:

Variable Documentation

◆ has_coroutine_support

bool kcenon::container::async::has_coroutine_support = false
inlineconstexpr

Check if coroutines are available at compile time.

Definition at line 60 of file async.h.