| Dominik Laskowski | a7e2255 | 2022-08-01 08:23:34 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright 2022 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | #pragma once | 
|  | 18 |  | 
|  | 19 | #include <functional> | 
|  | 20 | #include <optional> | 
| Dominik Laskowski | a7e2255 | 2022-08-01 08:23:34 -0700 | [diff] [blame] | 21 | #include <utility> | 
|  | 22 |  | 
| Dominik Laskowski | a957c1c | 2022-08-03 12:51:43 -0700 | [diff] [blame] | 23 | #include <ftl/details/optional.h> | 
|  | 24 |  | 
| Dominik Laskowski | a7e2255 | 2022-08-01 08:23:34 -0700 | [diff] [blame] | 25 | namespace android::ftl { | 
|  | 26 |  | 
|  | 27 | // Superset of std::optional<T> with monadic operations, as proposed in https://wg21.link/P0798R8. | 
|  | 28 | // | 
|  | 29 | // TODO: Remove in C++23. | 
|  | 30 | // | 
|  | 31 | template <typename T> | 
|  | 32 | struct Optional final : std::optional<T> { | 
|  | 33 | using std::optional<T>::optional; | 
|  | 34 |  | 
| Dominik Laskowski | d48d801 | 2022-08-23 08:36:32 -0700 | [diff] [blame] | 35 | // Implicit downcast. | 
|  | 36 | Optional(std::optional<T> other) : std::optional<T>(std::move(other)) {} | 
|  | 37 |  | 
| Dominik Laskowski | a7e2255 | 2022-08-01 08:23:34 -0700 | [diff] [blame] | 38 | using std::optional<T>::has_value; | 
|  | 39 | using std::optional<T>::value; | 
|  | 40 |  | 
|  | 41 | // Returns Optional<U> where F is a function that maps T to U. | 
|  | 42 | template <typename F> | 
|  | 43 | constexpr auto transform(F&& f) const& { | 
| Dominik Laskowski | a957c1c | 2022-08-03 12:51:43 -0700 | [diff] [blame] | 44 | using R = details::transform_result_t<F, decltype(value())>; | 
|  | 45 | if (has_value()) return R(std::invoke(std::forward<F>(f), value())); | 
|  | 46 | return R(); | 
| Dominik Laskowski | a7e2255 | 2022-08-01 08:23:34 -0700 | [diff] [blame] | 47 | } | 
|  | 48 |  | 
|  | 49 | template <typename F> | 
|  | 50 | constexpr auto transform(F&& f) & { | 
| Dominik Laskowski | a957c1c | 2022-08-03 12:51:43 -0700 | [diff] [blame] | 51 | using R = details::transform_result_t<F, decltype(value())>; | 
|  | 52 | if (has_value()) return R(std::invoke(std::forward<F>(f), value())); | 
|  | 53 | return R(); | 
| Dominik Laskowski | a7e2255 | 2022-08-01 08:23:34 -0700 | [diff] [blame] | 54 | } | 
|  | 55 |  | 
|  | 56 | template <typename F> | 
|  | 57 | constexpr auto transform(F&& f) const&& { | 
| Dominik Laskowski | a957c1c | 2022-08-03 12:51:43 -0700 | [diff] [blame] | 58 | using R = details::transform_result_t<F, decltype(std::move(value()))>; | 
|  | 59 | if (has_value()) return R(std::invoke(std::forward<F>(f), std::move(value()))); | 
|  | 60 | return R(); | 
| Dominik Laskowski | a7e2255 | 2022-08-01 08:23:34 -0700 | [diff] [blame] | 61 | } | 
|  | 62 |  | 
|  | 63 | template <typename F> | 
|  | 64 | constexpr auto transform(F&& f) && { | 
| Dominik Laskowski | a957c1c | 2022-08-03 12:51:43 -0700 | [diff] [blame] | 65 | using R = details::transform_result_t<F, decltype(std::move(value()))>; | 
|  | 66 | if (has_value()) return R(std::invoke(std::forward<F>(f), std::move(value()))); | 
|  | 67 | return R(); | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | // Returns Optional<U> where F is a function that maps T to Optional<U>. | 
|  | 71 | template <typename F> | 
|  | 72 | constexpr auto and_then(F&& f) const& { | 
|  | 73 | using R = details::and_then_result_t<F, decltype(value())>; | 
|  | 74 | if (has_value()) return std::invoke(std::forward<F>(f), value()); | 
|  | 75 | return R(); | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | template <typename F> | 
|  | 79 | constexpr auto and_then(F&& f) & { | 
|  | 80 | using R = details::and_then_result_t<F, decltype(value())>; | 
|  | 81 | if (has_value()) return std::invoke(std::forward<F>(f), value()); | 
|  | 82 | return R(); | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | template <typename F> | 
|  | 86 | constexpr auto and_then(F&& f) const&& { | 
|  | 87 | using R = details::and_then_result_t<F, decltype(std::move(value()))>; | 
|  | 88 | if (has_value()) return std::invoke(std::forward<F>(f), std::move(value())); | 
|  | 89 | return R(); | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | template <typename F> | 
|  | 93 | constexpr auto and_then(F&& f) && { | 
|  | 94 | using R = details::and_then_result_t<F, decltype(std::move(value()))>; | 
|  | 95 | if (has_value()) return std::invoke(std::forward<F>(f), std::move(value())); | 
|  | 96 | return R(); | 
| Dominik Laskowski | a7e2255 | 2022-08-01 08:23:34 -0700 | [diff] [blame] | 97 | } | 
|  | 98 | }; | 
|  | 99 |  | 
| Dominik Laskowski | d48d801 | 2022-08-23 08:36:32 -0700 | [diff] [blame] | 100 | // Deduction guides. | 
| Dominik Laskowski | a7e2255 | 2022-08-01 08:23:34 -0700 | [diff] [blame] | 101 | template <typename T> | 
|  | 102 | Optional(T) -> Optional<T>; | 
|  | 103 |  | 
| Dominik Laskowski | d48d801 | 2022-08-23 08:36:32 -0700 | [diff] [blame] | 104 | template <typename T> | 
|  | 105 | Optional(std::optional<T>) -> Optional<T>; | 
|  | 106 |  | 
| Dominik Laskowski | a7e2255 | 2022-08-01 08:23:34 -0700 | [diff] [blame] | 107 | }  // namespace android::ftl |