summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2026-03-18 06:51:01 +0100
committerAmlal El Mahrouss <amlal@nekernel.org>2026-03-18 06:51:01 +0100
commit0745b058c1fd373a10210c8d9397008b145d82bd (patch)
tree506b6d858b49e5dcc1e75e8e4f7a705553722668
parente3c44bac404812920f6e19285bd126b087f3bc39 (diff)
[CHORE] Update examples for Asio and AllocOp.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
-rw-r--r--example/allocator_op_example/example.cpp14
-rw-r--r--example/asio_example/example.cpp17
-rw-r--r--example/asio_example_2/example.cpp14
-rw-r--r--include/ocl/alloc_op.hpp4
-rw-r--r--include/ocl/asio.hpp22
5 files changed, 43 insertions, 28 deletions
diff --git a/example/allocator_op_example/example.cpp b/example/allocator_op_example/example.cpp
index 7c521fd..bd34955 100644
--- a/example/allocator_op_example/example.cpp
+++ b/example/allocator_op_example/example.cpp
@@ -23,11 +23,11 @@ int task()
int main()
{
- auto ret = std::async(std::launch::deferred, task);
- auto ret2 = std::async(std::launch::deferred, task);
- auto ret3 = std::async(std::launch::deferred, task);
-
- ret.get();
- ret2.get();
- ret3.get();
+ auto ret = std::async(std::launch::deferred, task);
+ auto ret2 = std::async(std::launch::deferred, task);
+ auto ret3 = std::async(std::launch::deferred, task);
+
+ ret.get();
+ ret2.get();
+ ret3.get();
}
diff --git a/example/asio_example/example.cpp b/example/asio_example/example.cpp
index d5abe0a..7b7fc16 100644
--- a/example/asio_example/example.cpp
+++ b/example/asio_example/example.cpp
@@ -8,9 +8,9 @@
#include <ocl/allocator_op.hpp>
#include <future>
-constexpr long int operator ""_USD(long double n)
+constexpr long int operator""_USD(long double n)
{
- return n * 1000.0;
+ return n * 1000.0;
}
/// @brief Wrap OCL in ASIO calls.
@@ -19,19 +19,18 @@ int main()
boost::asio::io_context ioc{1};
auto spawn_strand = boost::asio::make_strand(ioc);
- ocl::allocator<int> int_alloc;
+ ocl::allocator<int> int_alloc;
- boost::asio::co_spawn(spawn_strand, [&int_alloc]() -> boost::asio::awaitable<void> {
+ boost::asio::co_spawn(spawn_strand, [&int_alloc]() -> boost::asio::awaitable<void> {
auto balance = int_alloc.construct_array<1>();
*balance = operator ""_USD(150);
ocl::io::println("Balance USD: ", *balance);
- co_return;
- }, boost::asio::detached);
+ co_return; }, boost::asio::detached);
- auto balances = int_alloc.construct_array<5>();
+ auto balances = int_alloc.construct_array<5>();
- ocl::asio::run<[]() { (void)0; }>(ioc);
+ ocl::asio::run<[]() { (void)0; }>(ioc);
- return EXIT_SUCCESS;
+ return EXIT_SUCCESS;
}
diff --git a/example/asio_example_2/example.cpp b/example/asio_example_2/example.cpp
index c3bfeef..42410d7 100644
--- a/example/asio_example_2/example.cpp
+++ b/example/asio_example_2/example.cpp
@@ -9,18 +9,18 @@
#include <future>
#include <boost/process.hpp>
-namespace asio = ::boost::asio;
+namespace asio = ::boost::asio;
namespace process = ::boost::process;
/// @brief Wrap OCL in ASIO calls.
int main()
{
- asio::io_context ioc;
- process::process proc(ioc, "/usr/bin/g++", {"--version"}, process::v2::process_stdio{{ /* in to default */}, {}, nullptr});
-
- proc.wait();
+ asio::io_context ioc;
+ process::process proc(ioc, "/usr/bin/g++", {"--version"}, process::v2::process_stdio{{/* in to default */}, {}, nullptr});
- ocl::asio::run<[]() { (void)0; }>(ioc);
+ proc.wait();
- return EXIT_SUCCESS;
+ ocl::asio::run<[]() { (void)0; }>(ioc);
+
+ return EXIT_SUCCESS;
}
diff --git a/include/ocl/alloc_op.hpp b/include/ocl/alloc_op.hpp
index d7f81fe..05d9ed0 100644
--- a/include/ocl/alloc_op.hpp
+++ b/include/ocl/alloc_op.hpp
@@ -76,7 +76,7 @@ namespace ocl
template <typename... VarType>
auto construct_var(VarType&&... args)
{
- static AllocNew alloc;
+ static AllocNew alloc;
typename AllocNew::lock_type lt{alloc.m_};
return std::shared_ptr<RetType>(alloc.template var_alloc<VarType...>(std::forward<VarType...>(args)...), AllocDelete{});
}
@@ -84,7 +84,7 @@ namespace ocl
template <std::size_t N>
auto construct_array()
{
- static AllocNew alloc;
+ static AllocNew alloc;
typename AllocNew::lock_type lt{alloc.m_};
return std::shared_ptr<RetType>(alloc.template array_alloc<N>(), AllocDelete{});
}
diff --git a/include/ocl/asio.hpp b/include/ocl/asio.hpp
index a4c504b..ab7ee4d 100644
--- a/include/ocl/asio.hpp
+++ b/include/ocl/asio.hpp
@@ -19,6 +19,7 @@
#include <boost/asio/io_context.hpp>
#include <boost/asio/write.hpp>
#include <boost/asio/read.hpp>
+#include <ocl/print.hpp>
#endif
@@ -28,16 +29,31 @@ namespace ocl::asio
using io_context_type = boost::asio::io_context;
using run_pred_type = void (*)();
- template <run_pred_type IOCPred>
+ template <class IOCPred>
inline void run(io_context_type& ioc)
{
try
{
ioc.run();
}
- catch (...)
+ catch (const std::exception& e)
{
- IOCPred();
+ IOCPred{}();
+ ocl::io::println(e.what());
+ }
+ }
+
+ template <run_pred_type IOCFn>
+ inline void run(io_context_type& ioc)
+ {
+ try
+ {
+ ioc.run();
+ }
+ catch (const std::exception& e)
+ {
+ IOCFn();
+ ocl::io::println(e.what());
}
}