Android-cuttlefish cvd tool
expect.h
Go to the documentation of this file.
1//
2// Copyright (C) 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#pragma once
17
18#include <optional>
19#include <type_traits>
20#include <utility>
21
22#include "fmt/format.h" // IWYU pragma: keep: preprocessor
23#include "tl/expected.hpp" // IWYU pragma: export
24
26#include "cuttlefish/result/result_type.h" // IWYU pragma: export
27
28namespace cuttlefish {
29
48#define CF_ERR(MSG) \
49 StackTraceError<void>(::cuttlefish::StackTraceEntry("") << MSG)
50#define CF_ERRNO(MSG) \
51 StackTraceError<void>(::cuttlefish::StackTraceEntry("") << MSG)
52#define CF_ERRF(MSG, ...) \
53 StackTraceError<void>(::cuttlefish::StackTraceEntry("") \
54 << fmt::format(FMT_STRING(MSG), __VA_ARGS__))
55#define CF_ERRVF(VALUE, MSG, ...) \
56 StackTraceError(VALUE, ::cuttlefish::StackTraceEntry("") \
57 << fmt::format(FMT_STRING(MSG), __VA_ARGS__))
58
59template <typename T>
60T OutcomeDereference(std::optional<T>&& value) {
61 return std::move(*value);
62}
63
64template <typename E>
66
67template <typename T, typename E>
69 return std::move(*result);
70}
71
72template <typename T>
73typename std::enable_if<std::is_convertible_v<T, bool>, T>::type
75 return std::forward<T>(value);
76}
77
78inline bool TypeIsSuccess(bool value) { return value; }
79
80template <typename T>
81bool TypeIsSuccess(std::optional<T>& value) {
82 return value.has_value();
83}
84
85template <typename T, typename E>
87 return value.has_value();
88}
89
90inline auto ErrorFromType(bool) { return StackTraceError<void>(); }
91
92template <typename T>
93inline auto ErrorFromType(std::optional<T>) {
94 return StackTraceError<void>();
95}
96
97template <typename T, typename E>
99 return value.error();
100}
101
102#define CF_EXPECT_OVERLOAD(_1, _2, NAME, ...) NAME
103
104#define CF_EXPECT2(RESULT, MSG) \
105 ({ \
106 decltype(RESULT)&& macro_intermediate_result = RESULT; \
107 if (!TypeIsSuccess(macro_intermediate_result)) { \
108 auto current_entry = ::cuttlefish::StackTraceEntry(#RESULT); \
109 current_entry << MSG; \
110 auto error = ErrorFromType(macro_intermediate_result); \
111 error.PushEntry(std::move(current_entry)); \
112 return tl::unexpected(std::move(error)); \
113 }; \
114 OutcomeDereference(std::move(macro_intermediate_result)); \
115 })
116
117#define CF_EXPECT1(RESULT) CF_EXPECT2(RESULT, "")
118
119#define CF_EXPECTV(RESULT, VALUE, MSG) \
120 ({ \
121 decltype(RESULT)&& macro_intermediate_result = RESULT; \
122 if (!TypeIsSuccess(macro_intermediate_result)) { \
123 auto current_entry = ::cuttlefish::StackTraceEntry(#RESULT); \
124 current_entry << MSG; \
125 auto error = ErrorFromType(macro_intermediate_result).WithValue(VALUE); \
126 error.PushEntry(std::move(current_entry)); \
127 return tl::unexpected(std::move(error)); \
128 }; \
129 OutcomeDereference(std::move(macro_intermediate_result)); \
130 })
131
184#define CF_EXPECT(...) \
185 CF_EXPECT_OVERLOAD(__VA_ARGS__, CF_EXPECT2, CF_EXPECT1)(__VA_ARGS__)
186
187#define CF_EXPECTF(RESULT, MSG, ...) \
188 CF_EXPECT(RESULT, fmt::format(FMT_STRING(MSG), __VA_ARGS__))
189
190#define CF_EXPECTVF(RESULT, VALUE, MSG, ...) \
191 CF_EXPECTV(RESULT, VALUE, fmt::format(FMT_STRING(MSG), __VA_ARGS__))
192
193#define CF_COMPARE_EXPECT4(COMPARE_OP, LHS_RESULT, RHS_RESULT, MSG) \
194 ({ \
195 auto&& lhs_macro_intermediate_result = LHS_RESULT; \
196 auto&& rhs_macro_intermediate_result = RHS_RESULT; \
197 bool comparison_result = lhs_macro_intermediate_result COMPARE_OP \
198 rhs_macro_intermediate_result; \
199 if (!comparison_result) { \
200 auto current_entry = ::cuttlefish::StackTraceEntry(""); \
201 current_entry << "Expected \"" << #LHS_RESULT << "\" " << #COMPARE_OP \
202 << " \"" << #RHS_RESULT << "\" but was " \
203 << lhs_macro_intermediate_result << " vs " \
204 << rhs_macro_intermediate_result << ". "; \
205 current_entry << MSG; \
206 auto error = ErrorFromType(false); \
207 error.PushEntry(std::move(current_entry)); \
208 return tl::unexpected(std::move(error)); \
209 }; \
210 comparison_result; \
211 })
212
213#define CF_COMPARE_EXPECT3(COMPARE_OP, LHS_RESULT, RHS_RESULT) \
214 CF_COMPARE_EXPECT4(COMPARE_OP, LHS_RESULT, RHS_RESULT, "")
215
216#define CF_COMPARE_EXPECT_OVERLOAD(_1, _2, _3, _4, NAME, ...) NAME
217
218#define CF_COMPARE_EXPECT(...) \
219 CF_COMPARE_EXPECT_OVERLOAD(__VA_ARGS__, CF_COMPARE_EXPECT4, \
220 CF_COMPARE_EXPECT3) \
221 (__VA_ARGS__)
222
223#define CF_EXPECT_EQ(LHS_RESULT, RHS_RESULT, ...) \
224 CF_COMPARE_EXPECT(==, LHS_RESULT, RHS_RESULT, ##__VA_ARGS__)
225#define CF_EXPECT_NE(LHS_RESULT, RHS_RESULT, ...) \
226 CF_COMPARE_EXPECT(!=, LHS_RESULT, RHS_RESULT, ##__VA_ARGS__)
227#define CF_EXPECT_LE(LHS_RESULT, RHS_RESULT, ...) \
228 CF_COMPARE_EXPECT(<=, LHS_RESULT, RHS_RESULT, ##__VA_ARGS__)
229#define CF_EXPECT_LT(LHS_RESULT, RHS_RESULT, ...) \
230 CF_COMPARE_EXPECT(<, LHS_RESULT, RHS_RESULT, ##__VA_ARGS__)
231#define CF_EXPECT_GE(LHS_RESULT, RHS_RESULT, ...) \
232 CF_COMPARE_EXPECT(>=, LHS_RESULT, RHS_RESULT, ##__VA_ARGS__)
233#define CF_EXPECT_GT(LHS_RESULT, RHS_RESULT, ...) \
234 CF_COMPARE_EXPECT(>, LHS_RESULT, RHS_RESULT, ##__VA_ARGS__)
235
236} // namespace cuttlefish
Definition: error_type.h:35
Definition: alloc_driver.h:21
T OutcomeDereference(std::optional< T > &&value)
Definition: expect.h:60
auto ErrorFromType(bool)
Definition: expect.h:90
tl::expected< T, StackTraceError< E > > Result
Definition: result_type.h:30
bool TypeIsSuccess(bool value)
Definition: expect.h:78
uint8_t type
Definition: pairing_connection.h:0