xref: /aosp_15_r20/frameworks/native/include/ftl/details/optional.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright 2022 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #pragma once
18*38e8c45fSAndroid Build Coastguard Worker 
19*38e8c45fSAndroid Build Coastguard Worker #include <functional>
20*38e8c45fSAndroid Build Coastguard Worker #include <optional>
21*38e8c45fSAndroid Build Coastguard Worker 
22*38e8c45fSAndroid Build Coastguard Worker #include <ftl/details/type_traits.h>
23*38e8c45fSAndroid Build Coastguard Worker 
24*38e8c45fSAndroid Build Coastguard Worker namespace android::ftl {
25*38e8c45fSAndroid Build Coastguard Worker 
26*38e8c45fSAndroid Build Coastguard Worker template <typename>
27*38e8c45fSAndroid Build Coastguard Worker struct Optional;
28*38e8c45fSAndroid Build Coastguard Worker 
29*38e8c45fSAndroid Build Coastguard Worker namespace details {
30*38e8c45fSAndroid Build Coastguard Worker 
31*38e8c45fSAndroid Build Coastguard Worker template <typename>
32*38e8c45fSAndroid Build Coastguard Worker struct is_optional : std::false_type {};
33*38e8c45fSAndroid Build Coastguard Worker 
34*38e8c45fSAndroid Build Coastguard Worker template <typename T>
35*38e8c45fSAndroid Build Coastguard Worker struct is_optional<std::optional<T>> : std::true_type {};
36*38e8c45fSAndroid Build Coastguard Worker 
37*38e8c45fSAndroid Build Coastguard Worker template <typename T>
38*38e8c45fSAndroid Build Coastguard Worker struct is_optional<Optional<T>> : std::true_type {};
39*38e8c45fSAndroid Build Coastguard Worker 
40*38e8c45fSAndroid Build Coastguard Worker template <typename F, typename T>
41*38e8c45fSAndroid Build Coastguard Worker struct transform_result {
42*38e8c45fSAndroid Build Coastguard Worker   using type = Optional<std::remove_cv_t<std::invoke_result_t<F, T>>>;
43*38e8c45fSAndroid Build Coastguard Worker };
44*38e8c45fSAndroid Build Coastguard Worker 
45*38e8c45fSAndroid Build Coastguard Worker template <typename F, typename T>
46*38e8c45fSAndroid Build Coastguard Worker using transform_result_t = typename transform_result<F, T>::type;
47*38e8c45fSAndroid Build Coastguard Worker 
48*38e8c45fSAndroid Build Coastguard Worker template <typename F, typename T>
49*38e8c45fSAndroid Build Coastguard Worker struct and_then_result {
50*38e8c45fSAndroid Build Coastguard Worker   using type = remove_cvref_t<std::invoke_result_t<F, T>>;
51*38e8c45fSAndroid Build Coastguard Worker   static_assert(is_optional<type>{}, "and_then function must return an optional");
52*38e8c45fSAndroid Build Coastguard Worker };
53*38e8c45fSAndroid Build Coastguard Worker 
54*38e8c45fSAndroid Build Coastguard Worker template <typename F, typename T>
55*38e8c45fSAndroid Build Coastguard Worker using and_then_result_t = typename and_then_result<F, T>::type;
56*38e8c45fSAndroid Build Coastguard Worker 
57*38e8c45fSAndroid Build Coastguard Worker template <typename F, typename T>
58*38e8c45fSAndroid Build Coastguard Worker struct or_else_result {
59*38e8c45fSAndroid Build Coastguard Worker   using type = remove_cvref_t<std::invoke_result_t<F>>;
60*38e8c45fSAndroid Build Coastguard Worker   static_assert(std::is_same_v<type, std::optional<T>> || std::is_same_v<type, Optional<T>>,
61*38e8c45fSAndroid Build Coastguard Worker                 "or_else function must return an optional T");
62*38e8c45fSAndroid Build Coastguard Worker };
63*38e8c45fSAndroid Build Coastguard Worker 
64*38e8c45fSAndroid Build Coastguard Worker template <typename F, typename T>
65*38e8c45fSAndroid Build Coastguard Worker using or_else_result_t = typename or_else_result<F, T>::type;
66*38e8c45fSAndroid Build Coastguard Worker 
67*38e8c45fSAndroid Build Coastguard Worker }  // namespace details
68*38e8c45fSAndroid Build Coastguard Worker }  // namespace android::ftl
69