From a632dca2ff053fee51e6ebcea88000d38ebbca34 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sat, 20 Dec 2025 22:37:50 +0100 Subject: chore: API and design improvements. Signed-off-by: Amlal El Mahrouss --- example/smart_ptr_example/example.cc | 8 ++++++-- include/ocl/io.hpp | 4 ++-- include/ocl/is_same.hpp | 2 +- include/ocl/option.hpp | 37 +++++++++++++++++++++++------------- include/ocl/tracked_ptr.hpp | 13 ++++--------- 5 files changed, 37 insertions(+), 27 deletions(-) diff --git a/example/smart_ptr_example/example.cc b/example/smart_ptr_example/example.cc index ef1a865..a8293f2 100644 --- a/example/smart_ptr_example/example.cc +++ b/example/smart_ptr_example/example.cc @@ -2,9 +2,13 @@ #include #include -int main(int argc, char** argv) +auto main(int argc, char** argv) -> int { ocl::shared_ptr smart = ocl::delete_ptr(&std::cout); + *smart << "hello, world\n"; - return 0; + ocl::tracked_ptr tracked = ocl::make_tracked(10); + *smart << *tracked << "\n"; + + return EXIT_SUCCESS; } diff --git a/include/ocl/io.hpp b/include/ocl/io.hpp index 188f321..155153e 100644 --- a/include/ocl/io.hpp +++ b/include/ocl/io.hpp @@ -19,7 +19,7 @@ #define console_io_out ::ocl::io::void_cout #define console_io_in ::ocl::io::void_cin -#warning The OCL doesn't define IO streams in a freestanding host. +#warning The OCL doesnt define IO streams in a freestanding host. namespace ocl::io { @@ -37,4 +37,4 @@ namespace ocl::io } // namespace ocl::io #endif -#endif \ No newline at end of file +#endif diff --git a/include/ocl/is_same.hpp b/include/ocl/is_same.hpp index 4051afe..655ee4c 100644 --- a/include/ocl/is_same.hpp +++ b/include/ocl/is_same.hpp @@ -4,6 +4,6 @@ #error "This header has been deprecated, it now redirects to equiv.hpp" #else #warning "This header has been deprecated, it now redirects to equiv.hpp" -#endif +#endif #include diff --git a/include/ocl/option.hpp b/include/ocl/option.hpp index c1c8476..07cf194 100644 --- a/include/ocl/option.hpp +++ b/include/ocl/option.hpp @@ -20,6 +20,16 @@ namespace ocl count = err - okay + 1, }; + namespace detail + { + using option_error = std::runtime_error; + + void throw_option_invalid_type_error() + { + throw option_error("option has invalid type."); + } + } // namespace detail + class option final { public: @@ -27,7 +37,8 @@ namespace ocl explicit option(const return_type& return_type) : ret_(return_type) { - assert(ret_ != return_type::invalid); + if (ret_ == return_type::invalid) + detail::throw_option_invalid_type_error(); } ~option() = default; @@ -41,13 +52,13 @@ namespace ocl if (ret_ == return_type::err) { - throw std::runtime_error(input ? input : "option::error"); + throw detail::option_error(input ? input : "option::error"); } return *this; } - template + template option& expect_or_handle(const char* input) { assert(ret_ != return_type::invalid); @@ -55,7 +66,7 @@ namespace ocl if (ret_ == return_type::err) { // AMLALE: Shall it be a functor or container here? - ErrorHandler{}(input ? input : "option::error"); + Handleable(input ? input : "option::error"); } return *this; @@ -82,7 +93,7 @@ namespace ocl } }; - struct int_eq_teller final : teller + struct eq_teller final : teller { template bool operator()(ObjFirst a, ObjLast b) noexcept @@ -91,7 +102,7 @@ namespace ocl } }; - struct int_greater_than_teller final : teller + struct greater_than_teller final : teller { template bool operator()(ObjFirst a, ObjLast b) noexcept @@ -100,7 +111,7 @@ namespace ocl } }; - struct int_less_than_teller final : teller + struct less_than_teller final : teller { template bool operator()(ObjFirst a, ObjLast b) noexcept @@ -118,21 +129,21 @@ namespace ocl } template - inline return_type eval_less_than(Lst&&... arg) noexcept + inline return_type eval_less_than(Lst&&... arg) { - return detail::int_less_than_teller{}(std::forward(arg)...) ? return_type::okay : return_type::err; + return detail::less_than_teller{}(std::forward(arg)...) ? return_type::okay : return_type::err; } template - inline return_type eval_eq(Lst&&... arg) noexcept + inline return_type eval_eq(Lst&&... arg) { - return detail::int_eq_teller{}(std::forward(arg)...) ? return_type::okay : return_type::err; + return detail::eq_teller{}(std::forward(arg)...) ? return_type::okay : return_type::err; } template - inline return_type eval_greater_than(Lst&&... arg) noexcept + inline return_type eval_greater_than(Lst&&... arg) { - return detail::int_greater_than_teller{}(std::forward(arg)...) ? return_type::okay : return_type::err; + return detail::greater_than_teller{}(std::forward(arg)...) ? return_type::okay : return_type::err; } inline return_type eval_true() noexcept diff --git a/include/ocl/tracked_ptr.hpp b/include/ocl/tracked_ptr.hpp index cda37a8..8675a9d 100644 --- a/include/ocl/tracked_ptr.hpp +++ b/include/ocl/tracked_ptr.hpp @@ -115,19 +115,14 @@ namespace ocl template > class tracked_ptr { - public: - static Mgr& manager() noexcept - { - static Mgr mgr; - return mgr; - } + Mgr m_mgr_; public: template tracked_ptr(U&&... args) : ptr_(nullptr) { - ptr_ = tracked_ptr::manager().retain(std::forward(args)...); + ptr_ = m_mgr_.retain(std::forward(args)...); } virtual ~tracked_ptr() noexcept @@ -146,7 +141,7 @@ namespace ocl { if (ptr_) { - tracked_ptr::manager().dispose(ptr_); + m_mgr_.dispose(ptr_); } } @@ -237,4 +232,4 @@ namespace ocl } } // namespace ocl -#endif // ifndef __OCL_TRACKED_PTR \ No newline at end of file +#endif // ifndef __OCL_TRACKED_PTR -- cgit v1.2.3