blob: 57448dc1e0d45b3a2117bd8de00d786593d50b59 [file] [log] [blame]
Dominik Laskowski9bb429a2024-01-28 15:20:47 -05001/*
2 * Copyright 2024 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 <android-base/expected.h>
20#include <ftl/optional.h>
Dominik Laskowski189d1822024-05-03 17:30:26 -040021#include <ftl/unit.h>
Dominik Laskowski9bb429a2024-01-28 15:20:47 -050022
23#include <utility>
24
Dominik Laskowski189d1822024-05-03 17:30:26 -040025// Given an expression `expr` that evaluates to an ftl::Expected<T, E> result (R for short), FTL_TRY
26// unwraps T out of R, or bails out of the enclosing function F if R has an error E. The return type
27// of F must be R, since FTL_TRY propagates R in the error case. As a special case, ftl::Unit may be
28// used as the error E to allow FTL_TRY expressions when F returns `void`.
29//
30// The non-standard syntax requires `-Wno-gnu-statement-expression-from-macro-expansion` to compile.
31// The UnitToVoid conversion allows the macro to be used for early exit from a function that returns
32// `void`.
33//
34// Example usage:
35//
36// using StringExp = ftl::Expected<std::string, std::errc>;
37//
38// StringExp repeat(StringExp exp) {
39// const std::string str = FTL_TRY(exp);
40// return StringExp(str + str);
41// }
42//
43// assert(StringExp("haha"s) == repeat(StringExp("ha"s)));
44// assert(repeat(ftl::Unexpected(std::errc::bad_message)).has_error([](std::errc e) {
45// return e == std::errc::bad_message;
46// }));
47//
48//
49// FTL_TRY may be used in void-returning functions by using ftl::Unit as the error type:
50//
51// void uppercase(char& c, ftl::Optional<char> opt) {
52// c = std::toupper(FTL_TRY(std::move(opt).ok_or(ftl::Unit())));
53// }
54//
55// char c = '?';
56// uppercase(c, std::nullopt);
57// assert(c == '?');
58//
59// uppercase(c, 'a');
60// assert(c == 'A');
61//
62#define FTL_TRY(expr) \
63 ({ \
64 auto exp_ = (expr); \
65 if (!exp_.has_value()) { \
66 using E = decltype(exp_)::error_type; \
67 return android::ftl::details::UnitToVoid<E>::from(std::move(exp_)); \
68 } \
69 exp_.value(); \
70 })
71
Dominik Laskowski9bb429a2024-01-28 15:20:47 -050072namespace android::ftl {
73
74// Superset of base::expected<T, E> with monadic operations.
75//
76// TODO: Extend std::expected<T, E> in C++23.
77//
78template <typename T, typename E>
79struct Expected final : base::expected<T, E> {
80 using Base = base::expected<T, E>;
81 using Base::expected;
82
83 using Base::error;
84 using Base::has_value;
85 using Base::value;
86
87 template <typename P>
88 constexpr bool has_error(P predicate) const {
89 return !has_value() && predicate(error());
90 }
91
92 constexpr Optional<T> value_opt() const& {
93 return has_value() ? Optional(value()) : std::nullopt;
94 }
95
96 constexpr Optional<T> value_opt() && {
97 return has_value() ? Optional(std::move(value())) : std::nullopt;
98 }
99
100 // Delete new for this class. Its base doesn't have a virtual destructor, and
101 // if it got deleted via base class pointer, it would cause undefined
102 // behavior. There's not a good reason to allocate this object on the heap
103 // anyway.
104 static void* operator new(size_t) = delete;
105 static void* operator new[](size_t) = delete;
106};
107
108template <typename E>
109constexpr auto Unexpected(E&& error) {
110 return base::unexpected(std::forward<E>(error));
111}
112
113} // namespace android::ftl