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 unknown,
24 count = unknown - okay + 1,
25 };
26
27 namespace detail
28 {
29 using option_error = std::runtime_error;
30
31 inline void throw_option_invalid_type_error(const boost::source_location& loc = BOOST_CURRENT_LOCATION)
32 {
33 ::boost::throw_exception(option_error(""), loc);
34 }
35 } // namespace detail
36
37 class basic_option final
38 {
39 public:
40 basic_option() = delete;
41
42 explicit basic_option(const return_type& return_type)
43 : ret_(return_type)
44 {
45 if (ret_ == return_type::invalid)
46 detail::throw_option_invalid_type_error();
47 }
48
49 ~basic_option() = default;
50
51 basic_option& operator=(const basic_option&) = default;
52 basic_option(const basic_option&) = default;
53
54 basic_option& expect(const char* input)
55 {
56 assert(ret_ != return_type::invalid);
57
58 if (ret_ == return_type::err)
59 {
60 io::println(input ? input : "basic_option::error");
61 detail::throw_option_invalid_type_error();
62 }
63
64 return *this;
65 }
66
67 template <typename Handleable>
68 basic_option& expect(const char* input)
69 {
70 assert(ret_ != return_type::invalid);
71
72 if (ret_ == return_type::err)
73 {
74 // AMLALE: Shall it be a functor or container here?
75 Handleable{}(input ? input : "basic_option::error");
76 }
77
78 return *this;
79 }
80
81 private:
82 return_type ret_{return_type::invalid};
83 };
84
85 namespace detail
86 {
87 // AMLALE: The operator() are marked as `noexcept` as failing conditions within an evaluation (say a overloads operator==) proves that the
88 // predictate is wrong. Thus program state is undefined.
89
90 struct eq_teller final
91 {
92 template <class ObjFirst, class ObjLast>
93 bool operator()(ObjFirst a, ObjLast b) noexcept
94 {
95 return (a == b);
96 }
97 };
98
99 struct greater_than_teller final
100 {
101 template <class ObjFirst, class ObjLast>
102 bool operator()(ObjFirst a, ObjLast b) noexcept
103 {
104 return (a > b);
105 }
106 };
107
108 struct less_than_teller final
109 {
110 template <class ObjFirst, class ObjLast>
111 bool operator()(ObjFirst a, ObjLast b) noexcept
112 {
113 return (a < b);
114 }
115 };
116 } // namespace detail
117
118 template <typename Teller, typename... Lst>
119 inline return_type eval(const Teller& tell, Lst&&... arg)
120 {
121 return tell(std::forward<Lst>(arg)...) ? return_type::okay : return_type::err;
122 }
123
124 template <typename... Lst>
125 inline return_type eval_less_than(Lst&&... arg)
126 {
127 return detail::less_than_teller{}(std::forward<Lst>(arg)...) ? return_type::okay : return_type::err;
128 }
129
130 template <typename... Lst>
131 inline return_type eval_eq(Lst&&... arg)
132 {
133 return detail::eq_teller{}(std::forward<Lst>(arg)...) ? return_type::okay : return_type::err;
134 }
135
136 template <typename... Lst>
137 inline return_type eval_greater_than(Lst&&... arg)
138 {
139 return detail::greater_than_teller{}(std::forward<Lst>(arg)...) ? return_type::okay : return_type::err;
140 }
141
142 inline return_type eval_true() noexcept
143 {
144 return return_type::okay;
145 }
146
147 inline return_type eval_false() noexcept
148 {
149 return return_type::err;
150 }
151
152 inline return_type eval_invalid() noexcept
153 {
154 return return_type::invalid;
155 }
156
157 using option = basic_option;
158
159} // namespace ocl
160
161#endif /* ifndef OCL_CORE_OPTION */
Definition option_fwd.hpp:38
Memory allocator for OCL support.
Definition allocator_fwd.hpp:16