summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-12-20 22:37:50 +0100
committerAmlal El Mahrouss <amlal@nekernel.org>2025-12-20 22:37:50 +0100
commita632dca2ff053fee51e6ebcea88000d38ebbca34 (patch)
treeb70e9e10fdb8dbd9fef284136981b54888f4a1df
parent6471853426716df7f5b804649d0cf74df340dfe5 (diff)
chore: API and design improvements.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
-rw-r--r--example/smart_ptr_example/example.cc8
-rw-r--r--include/ocl/io.hpp4
-rw-r--r--include/ocl/is_same.hpp2
-rw-r--r--include/ocl/option.hpp37
-rw-r--r--include/ocl/tracked_ptr.hpp13
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 <ocl/print.hpp>
#include <ocl/smart_ptr.hpp>
-int main(int argc, char** argv)
+auto main(int argc, char** argv) -> int
{
ocl::shared_ptr<std::ostream> smart = ocl::delete_ptr(&std::cout);
+ *smart << "hello, world\n";
- return 0;
+ ocl::tracked_ptr<int> tracked = ocl::make_tracked<int>(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 <ocl/equiv.hpp>
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 <typename ErrorHandler>
+ template <typename Handleable>
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 <class ObjFirst, class ObjLast>
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 <class ObjFirst, class ObjLast>
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 <class ObjFirst, class ObjLast>
bool operator()(ObjFirst a, ObjLast b) noexcept
@@ -118,21 +129,21 @@ namespace ocl
}
template <typename... Lst>
- 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<Lst>(arg)...) ? return_type::okay : return_type::err;
+ return detail::less_than_teller{}(std::forward<Lst>(arg)...) ? return_type::okay : return_type::err;
}
template <typename... Lst>
- inline return_type eval_eq(Lst&&... arg) noexcept
+ inline return_type eval_eq(Lst&&... arg)
{
- return detail::int_eq_teller{}(std::forward<Lst>(arg)...) ? return_type::okay : return_type::err;
+ return detail::eq_teller{}(std::forward<Lst>(arg)...) ? return_type::okay : return_type::err;
}
template <typename... Lst>
- 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<Lst>(arg)...) ? return_type::okay : return_type::err;
+ return detail::greater_than_teller{}(std::forward<Lst>(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 <typename Type, typename Mgr = tracked_mgr<Type>>
class tracked_ptr
{
- public:
- static Mgr& manager() noexcept
- {
- static Mgr mgr;
- return mgr;
- }
+ Mgr m_mgr_;
public:
template <typename... U>
tracked_ptr(U&&... args)
: ptr_(nullptr)
{
- ptr_ = tracked_ptr::manager().retain(std::forward<U>(args)...);
+ ptr_ = m_mgr_.retain(std::forward<U>(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