iceoryx_hoofs 2.0.3
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
24namespace iox
25{
26namespace cxx
27{
28namespace internal
29{
30template <typename Derived, class = void>
31struct HasValueMethod : std::false_type
32{
33};
34
35template <typename Derived>
36struct HasValueMethod<Derived, cxx::void_t<decltype(std::declval<Derived>().value())>> : std::true_type
37{
38};
39
40template <typename Derived, class = void>
41struct HasGetErrorMethod : std::false_type
42{
43};
44
45template <typename Derived>
46struct HasGetErrorMethod<Derived, cxx::void_t<decltype(std::declval<Derived>().get_error())>> : std::true_type
47{
48};
49
50template <typename Derived>
51struct Expect
52{
56 void expect(const char* const msg) const noexcept;
57};
58
59template <typename Derived, typename ValueType>
60struct 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
87template <typename Derived, typename ValueType>
88struct 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
109template <typename Derived, typename ValueType>
110struct 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
144template <typename Derived>
145struct 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
174template <typename Derived, typename ErrorType>
175struct 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
209template <typename Derived>
210struct 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
239template <typename Derived, typename ValueType, typename ErrorType>
240struct FunctionalInterfaceImpl : public ExpectWithValue<Derived, ValueType>,
241 public ValueOr<Derived, ValueType>,
242 public AndThenWithValue<Derived, ValueType>,
243 public OrElseWithValue<Derived, ErrorType>
244{
245};
246
247template <typename Derived>
248struct FunctionalInterfaceImpl<Derived, void, void>
249 : public Expect<Derived>, public AndThen<Derived>, public OrElse<Derived>
250{
251};
252
253template <typename Derived, typename ValueType>
254struct 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
261template <typename Derived, typename ErrorType>
262struct FunctionalInterfaceImpl<Derived, void, ErrorType>
263 : public Expect<Derived>, public AndThen<Derived>, public OrElseWithValue<Derived, ErrorType>
264{
265};
266} // namespace internal
267
283template <typename Derived, typename ValueType, typename ErrorType>
284using 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