diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-12-15 16:57:12 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-12-15 16:57:12 +0100 |
| commit | 7096a2b477782db54d9e0829b6ad7c6a3314dc2c (patch) | |
| tree | 1d9b126d1f2aa3c749242217b37962391695327d | |
| parent | 0424f52c142e64cfadaa37288feee775fafc2bf0 (diff) | |
chore: new elements and breaking API changes in is_same, option.hpp.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
| -rw-r--r-- | examples/option_example/example.cc | 3 | ||||
| -rw-r--r-- | include/ocl/is_same.hpp | 29 | ||||
| -rw-r--r-- | include/ocl/option.hpp | 52 |
3 files changed, 47 insertions, 37 deletions
diff --git a/examples/option_example/example.cc b/examples/option_example/example.cc index 7649438..0da9d49 100644 --- a/examples/option_example/example.cc +++ b/examples/option_example/example.cc @@ -7,5 +7,8 @@ int main(int argc, char** argv) ocl::option opt{ocl::eval_eq(nullptr, nullptr)}; opt.expect("is incorrect"); + ocl::option opt2{ocl::eval_eq(argv, nullptr)}; + opt2.expect("is incorrect"); + return 0; } diff --git a/include/ocl/is_same.hpp b/include/ocl/is_same.hpp index 57d372b..519a87b 100644 --- a/include/ocl/is_same.hpp +++ b/include/ocl/is_same.hpp @@ -19,8 +19,8 @@ namespace ocl using result_type = typename T::result_type; using type = T; - /// @brief hash from T's result_type. - static result_type hash() + // AMLALE: If it throws, we can't compute the hash correctly. + static constexpr result_type hash() noexcept { return T{}.hash(); } @@ -61,10 +61,7 @@ namespace ocl { using result_type = bool; - constexpr result_type hash() - { - return sizeof(T) == 1; - } + static constexpr auto value = sizeof(T) == 1; }; template <typename T> @@ -72,10 +69,7 @@ namespace ocl { using result_type = bool; - constexpr result_type hash() - { - return sizeof(T) > 1; - } + static constexpr auto value = sizeof(T) > 1; }; template <typename T> @@ -83,10 +77,7 @@ namespace ocl { using result_type = bool; - constexpr result_type hash() - { - return sizeof(T) >= 4; - } + static constexpr auto value = sizeof(T) >= 4; }; template <typename L, typename R> @@ -96,10 +87,7 @@ namespace ocl using left_type = L; using right_type = R; - constexpr result_type hash() - { - return false; - } + static constexpr auto value = false; }; template <typename L> @@ -109,10 +97,7 @@ namespace ocl using left_type = L; using right_type = L; - constexpr result_type hash() - { - return true; - } + static constexpr auto value = true; }; } // namespace ocl diff --git a/include/ocl/option.hpp b/include/ocl/option.hpp index d84e935..73af0ae 100644 --- a/include/ocl/option.hpp +++ b/include/ocl/option.hpp @@ -27,6 +27,7 @@ namespace ocl explicit option(const return_type& return_type) : ret_(return_type) { + assert(ret_ != return_type::invalid); } ~option() = default; @@ -36,6 +37,8 @@ namespace ocl option& expect(const char* input) { + assert(ret_ != return_type::invalid); + if (ret_ == return_type::err) { throw std::runtime_error(input ? input : "option::error"); @@ -47,8 +50,11 @@ namespace ocl template <typename ErrorHandler> option& expect_or_handle(const char* input) { + assert(ret_ != return_type::invalid); + if (ret_ == return_type::err) { + // AMLALE: Shall it be a functor or container here? ErrorHandler{}(input ? input : "option::error"); } @@ -59,56 +65,72 @@ namespace ocl return_type ret_{return_type::invalid}; }; - template <typename Teller, typename... Lst> - inline return_type eval(Teller tell, Lst&&... arg) - { - return tell(std::forward<Lst>(arg)...) ? return_type::okay : return_type::err; - } - namespace detail { - struct int_eq_teller final + // AMLALE: They (operator()) were marked `noexcept` as failing conditions within an evaluation (say a overloads operator==) proves that the + // predictate is wrong. + + struct teller + { + explicit teller() = default; + virtual ~teller() = default; + + template <class ObjFirst, class ObjLast> + bool operator()(ObjFirst a, ObjLast b) noexcept + { + return false; + } + }; + + struct int_eq_teller final : teller { template <class ObjFirst, class ObjLast> - bool operator()(ObjFirst a, ObjLast b) + bool operator()(ObjFirst a, ObjLast b) noexcept { return (a == b); } }; - struct int_greater_than_teller final + struct int_greater_than_teller final : teller { template <class ObjFirst, class ObjLast> - bool operator()(ObjFirst a, ObjLast b) + bool operator()(ObjFirst a, ObjLast b) noexcept { return (a > b); } }; - struct int_less_than_teller final + struct int_less_than_teller final : teller { template <class ObjFirst, class ObjLast> - bool operator()(ObjFirst a, ObjLast b) + bool operator()(ObjFirst a, ObjLast b) noexcept { return (a < b); } }; } // namespace detail + template <typename Teller, typename... Lst> + inline return_type eval(const Teller& tell, Lst&&... arg) + { + static_assert(std::is_base_of_v<detail::teller, Teller>, "Teller is not evalueable."); + return tell(std::forward<Lst>(arg)...) ? return_type::okay : return_type::err; + } + template <typename... Lst> - inline return_type eval_less_than(Lst&&... arg) + inline return_type eval_less_than(Lst&&... arg) noexcept { return detail::int_less_than_teller{}(std::forward<Lst>(arg)...) ? return_type::okay : return_type::err; } template <typename... Lst> - inline return_type eval_eq(Lst&&... arg) + inline return_type eval_eq(Lst&&... arg) noexcept { return detail::int_eq_teller{}(std::forward<Lst>(arg)...) ? return_type::okay : return_type::err; } template <typename... Lst> - inline return_type eval_greater_than(Lst&&... arg) + inline return_type eval_greater_than(Lst&&... arg) noexcept { return detail::int_greater_than_teller{}(std::forward<Lst>(arg)...) ? return_type::okay : return_type::err; } |
