Open C++ Libraries.org develop
Loading...
Searching...
No Matches
option_fwd.hpp
1// SPDX-License-Identifier: BSL-1.0
2// Copyright 2025-2026, Amlal El Mahrouss (amlal@nekernel.org)
3// Distributed under the Boost Software License, Version 1.0. (See accompanying
4// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5// Official repository: https://github.com/open-cpp-libraries/core
6
7#ifndef OCL_CORE_OPTION
8#define OCL_CORE_OPTION
9
10#include <ocl/detail/config.hpp>
11#include <ocl/print_fwd.hpp>
12
14
15namespace ocl
16{
17
18 enum struct return_type : int
19 {
20 invalid = 0,
21 okay = 100,
22 err,
23 count = err - okay + 1,
24 };
25
26 namespace detail
27 {
28 using option_error = std::runtime_error;
29
30 inline void throw_option_invalid_type_error(const boost::source_location& loc = BOOST_CURRENT_LOCATION)
31 {
32 ::boost::throw_exception(option_error(""), loc);
33 }
34 } // namespace detail
35
36 class basic_option final
37 {
38 public:
39 basic_option() = delete;
40
41 explicit basic_option(const return_type& return_type)
42 : ret_(return_type)
43 {
44 if (ret_ == return_type::invalid)
45 detail::throw_option_invalid_type_error();
46 }
47
48 ~basic_option() = default;
49
50 basic_option& operator=(const basic_option&) = default;
51 basic_option(const basic_option&) = default;
52
53 basic_option& expect(const char* input)
54 {
55 assert(ret_ != return_type::invalid);
56
57 if (ret_ == return_type::err)
58 {
59 io::println(input ? input : "basic_option::error");
60 detail::throw_option_invalid_type_error();
61 }
62
63 return *this;
64 }
65
66 template <typename Handleable>
67 basic_option& expect(const char* input)
68 {
69 assert(ret_ != return_type::invalid);
70
71 if (ret_ == return_type::err)
72 {
73 // AMLALE: Shall it be a functor or container here?
74 Handleable{}(input ? input : "basic_option::error");
75 }
76
77 return *this;
78 }
79
80 private:
81 return_type ret_{return_type::invalid};
82 };
83
84 namespace detail
85 {
86 // AMLALE: The operator() are marked as `noexcept` as failing conditions within an evaluation (say a overloads operator==) proves that the
87 // predictate is wrong. Thus program state is undefined.
88
89 struct eq_teller final
90 {
91 template <class ObjFirst, class ObjLast>
92 bool operator()(ObjFirst a, ObjLast b) noexcept
93 {
94 return (a == b);
95 }
96 };
97
98 struct greater_than_teller final
99 {
100 template <class ObjFirst, class ObjLast>
101 bool operator()(ObjFirst a, ObjLast b) noexcept
102 {
103 return (a > b);
104 }
105 };
106
107 struct less_than_teller final
108 {
109 template <class ObjFirst, class ObjLast>
110 bool operator()(ObjFirst a, ObjLast b) noexcept
111 {
112 return (a < b);
113 }
114 };
115 } // namespace detail
116
117 template <typename Teller, typename... Lst>
118 inline return_type eval(const Teller& tell, Lst&&... arg)
119 {
120 return tell(std::forward<Lst>(arg)...) ? return_type::okay : return_type::err;
121 }
122
123 template <typename... Lst>
124 inline return_type eval_less_than(Lst&&... arg)
125 {
126 return detail::less_than_teller{}(std::forward<Lst>(arg)...) ? return_type::okay : return_type::err;
127 }
128
129 template <typename... Lst>
130 inline return_type eval_eq(Lst&&... arg)
131 {
132 return detail::eq_teller{}(std::forward<Lst>(arg)...) ? return_type::okay : return_type::err;
133 }
134
135 template <typename... Lst>
136 inline return_type eval_greater_than(Lst&&... arg)
137 {
138 return detail::greater_than_teller{}(std::forward<Lst>(arg)...) ? return_type::okay : return_type::err;
139 }
140
141 inline return_type eval_true() noexcept
142 {
143 return return_type::okay;
144 }
145
146 inline return_type eval_false() noexcept
147 {
148 return return_type::err;
149 }
150
151 inline return_type eval_invalid() noexcept
152 {
153 return return_type::invalid;
154 }
155
156 using option = basic_option;
157
158} // namespace ocl
159
160#endif /* ifndef OCL_CORE_OPTION */
Definition option_fwd.hpp:37
Memory allocator for OCL support.
Definition allocator_fwd.hpp:16