/* * Copyright 2022 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include namespace android::ftl { // Superset of std::optional with monadic operations, as proposed in https://wg21.link/P0798R8. // // TODO: Remove in C++23. // template struct Optional final : std::optional { using std::optional::optional; using std::optional::has_value; using std::optional::value; // Returns Optional where F is a function that maps T to U. template constexpr auto transform(F&& f) const& { using U = std::remove_cv_t>; if (has_value()) return Optional(std::invoke(std::forward(f), value())); return Optional(); } template constexpr auto transform(F&& f) & { using U = std::remove_cv_t>; if (has_value()) return Optional(std::invoke(std::forward(f), value())); return Optional(); } template constexpr auto transform(F&& f) const&& { using U = std::invoke_result_t; if (has_value()) return Optional(std::invoke(std::forward(f), std::move(value()))); return Optional(); } template constexpr auto transform(F&& f) && { using U = std::invoke_result_t; if (has_value()) return Optional(std::invoke(std::forward(f), std::move(value()))); return Optional(); } }; // Deduction guide. template Optional(T) -> Optional; } // namespace android::ftl