From 4b2b47d6edc8c8dd75f260fed3286f5dc4fe9374 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Wed, 4 Mar 2026 08:55:10 +0100 Subject: feat: core: add ASIO example and new IOC helper 'run_loop'. Signed-off-by: Amlal El Mahrouss --- example/asio_example/CMakeLists.txt | 15 +++++++++++++++ example/asio_example/example.cpp | 33 +++++++++++++++++++++++++++++++++ include/ocl/asio.hpp | 21 +++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 example/asio_example/CMakeLists.txt create mode 100644 example/asio_example/example.cpp diff --git a/example/asio_example/CMakeLists.txt b/example/asio_example/CMakeLists.txt new file mode 100644 index 0000000..2efd470 --- /dev/null +++ b/example/asio_example/CMakeLists.txt @@ -0,0 +1,15 @@ + +cmake_minimum_required(VERSION 3.15...3.31) + +project( + AsioExample + VERSION 1.0 + LANGUAGES CXX) + +find_package(Boost REQUIRED) + +add_executable(AsioExample example.cpp) + +set_property(TARGET AsioExample PROPERTY CXX_STANDARD 20) +target_include_directories(AsioExample PUBLIC ../../include/) +# target_link_libraries(AsioExample PRIVATE Boost Core Asio) diff --git a/example/asio_example/example.cpp b/example/asio_example/example.cpp new file mode 100644 index 0000000..0aeb285 --- /dev/null +++ b/example/asio_example/example.cpp @@ -0,0 +1,33 @@ +// Copyright 2026, Amlal El Mahrouss (amlal@nekernel.org) +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// Official repository: https://git.ocl.nekernel.org/core + +#include +#include +#include +#include + +constexpr long int operator ""_USD(long double n) +{ + return n * 1000.0; +} + +/// @brief Wrap OCL in ASIO calls. +int main() +{ + boost::asio::io_context ioc{1}; + auto spawn_strand = boost::asio::make_strand(ioc); + + boost::asio::co_spawn(spawn_strand, [&]() -> boost::asio::awaitable { + ocl::allocator int_alloc; + auto balance = int_alloc.construct_array<1>(); + + *balance = operator ""_USD(150); + ocl::io::println("USD: ", *balance); + co_return; + }, boost::asio::detached); + + ocl::asio::run_loop<[]() { std::terminate(); return; }>(ioc); + return EXIT_SUCCESS; +} diff --git a/include/ocl/asio.hpp b/include/ocl/asio.hpp index c1e304d..1e4abf5 100644 --- a/include/ocl/asio.hpp +++ b/include/ocl/asio.hpp @@ -12,6 +12,7 @@ #else +#include #include #include #include @@ -20,4 +21,24 @@ #endif +namespace ocl::asio +{ + using io_context = boost::asio::io_context; + using run_pred_type = void(*)(); + + template + inline void run_loop(io_context& ioc) + { + try + { + ioc.run(); + } + catch (...) + { + IOCPred(); + } + } + +} // namespace ocl + #endif -- cgit v1.2.3