iceoryx_hoofs  2.0.2
functional_interface.hpp
1 // Copyright (c) 2022 by Apex.AI Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // SPDX-License-Identifier: Apache-2.0
16 #ifndef IOX_HOOFS_CXX_FUNCTIONAL_POLICY_HPP
17 #define IOX_HOOFS_CXX_FUNCTIONAL_POLICY_HPP
18 
19 #include "iceoryx_hoofs/cxx/function_ref.hpp"
20 #include "iceoryx_hoofs/cxx/helplets.hpp"
21 #include <iostream>
22 #include <utility>
23 
24 namespace iox
25 {
26 namespace cxx
27 {
28 namespace internal
29 {
30 template <typename Derived, class = void>
31 struct HasValueMethod : std::false_type
32 {
33 };
34 
35 template <typename Derived>
36 struct HasValueMethod<Derived, cxx::void_t<decltype(std::declval<Derived>().value())>> : std::true_type
37 {
38 };
39 
40 template <typename Derived, class = void>
41 struct HasGetErrorMethod : std::false_type
42 {
43 };
44 
45 template <typename Derived>
46 struct HasGetErrorMethod<Derived, cxx::void_t<decltype(std::declval<Derived>().get_error())>> : std::true_type
47 {
48 };
49 
50 template <typename Derived>
51 struct Expect
52 {
56  void expect(const char* const msg) const noexcept;
57 };
58 
59 template <typename Derived, typename ValueType>
60 struct ExpectWithValue
61 {
63  // the method prints the provided message and induces a fatal error
66  ValueType& expect(const char* const msg) & noexcept;
67 
69  // the method prints the provided message and induces a fatal error
72  const ValueType& expect(const char* const msg) const& noexcept;
73 
75  // the method prints the provided message and induces a fatal error
78  ValueType&& expect(const char* const msg) && noexcept;
79 
81  // the method prints the provided message and induces a fatal error
84  const ValueType&& expect(const char* const msg) const&& noexcept;
85 };
86 
87 template <typename Derived, typename ValueType>
88 struct ValueOr
89 {
96  template <typename U>
97  ValueType value_or(U&& alternative) const& noexcept;
98 
105  template <typename U>
106  ValueType value_or(U&& alternative) && noexcept;
107 };
108 
109 template <typename Derived, typename ValueType>
110 struct AndThenWithValue
111 {
112  using and_then_callback_t = cxx::function_ref<void(ValueType&)>;
113  using const_and_then_callback_t = cxx::function_ref<void(const ValueType&)>;
114 
120  Derived& and_then(const and_then_callback_t& callable) & noexcept;
121 
127  const Derived& and_then(const const_and_then_callback_t& callable) const& noexcept;
128 
134  Derived&& and_then(const and_then_callback_t& callable) && noexcept;
135 
141  const Derived&& and_then(const const_and_then_callback_t& callable) const&& noexcept;
142 };
143 
144 template <typename Derived>
145 struct AndThen
146 {
147  using and_then_callback_t = cxx::function_ref<void()>;
148 
153  Derived& and_then(const and_then_callback_t& callable) & noexcept;
154 
159  const Derived& and_then(const and_then_callback_t& callable) const& noexcept;
160 
165  Derived&& and_then(const and_then_callback_t& callable) && noexcept;
166 
171  const Derived&& and_then(const and_then_callback_t& callable) const&& noexcept;
172 };
173 
174 template <typename Derived, typename ErrorType>
175 struct OrElseWithValue
176 {
177  using or_else_callback_t = cxx::function_ref<void(ErrorType&)>;
178  using const_or_else_callback_t = cxx::function_ref<void(const ErrorType&)>;
179 
185  Derived& or_else(const or_else_callback_t& callable) & noexcept;
186 
192  const Derived& or_else(const const_or_else_callback_t& callable) const& noexcept;
193 
199  Derived&& or_else(const or_else_callback_t& callable) && noexcept;
200 
206  const Derived&& or_else(const const_or_else_callback_t& callable) const&& noexcept;
207 };
208 
209 template <typename Derived>
210 struct OrElse
211 {
212  using or_else_callback_t = cxx::function_ref<void()>;
213 
218  Derived& or_else(const or_else_callback_t& callable) & noexcept;
219 
224  const Derived& or_else(const or_else_callback_t& callable) const& noexcept;
225 
230  Derived&& or_else(const or_else_callback_t& callable) && noexcept;
231 
236  const Derived&& or_else(const or_else_callback_t& callable) const&& noexcept;
237 };
238 
239 template <typename Derived, typename ValueType, typename ErrorType>
240 struct FunctionalInterfaceImpl : public ExpectWithValue<Derived, ValueType>,
241  public ValueOr<Derived, ValueType>,
242  public AndThenWithValue<Derived, ValueType>,
243  public OrElseWithValue<Derived, ErrorType>
244 {
245 };
246 
247 template <typename Derived>
248 struct FunctionalInterfaceImpl<Derived, void, void>
249  : public Expect<Derived>, public AndThen<Derived>, public OrElse<Derived>
250 {
251 };
252 
253 template <typename Derived, typename ValueType>
254 struct FunctionalInterfaceImpl<Derived, ValueType, void> : public ExpectWithValue<Derived, ValueType>,
255  public ValueOr<Derived, ValueType>,
256  public AndThenWithValue<Derived, ValueType>,
257  public OrElse<Derived>
258 {
259 };
260 
261 template <typename Derived, typename ErrorType>
262 struct FunctionalInterfaceImpl<Derived, void, ErrorType>
263  : public Expect<Derived>, public AndThen<Derived>, public OrElseWithValue<Derived, ErrorType>
264 {
265 };
266 } // namespace internal
267 
283 template <typename Derived, typename ValueType, typename ErrorType>
284 using FunctionalInterface = internal::FunctionalInterfaceImpl<Derived, ValueType, ErrorType>;
285 
286 } // namespace cxx
287 } // namespace iox
288 
289 #include "iceoryx_hoofs/internal/cxx/functional_interface.inl"
290 
291 #endif
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:29