OR-Tools  9.2
status_macros.h
Go to the documentation of this file.
1// Copyright 2010-2021 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#ifndef OR_TOOLS_BASE_STATUS_MACROS_H_
15#define OR_TOOLS_BASE_STATUS_MACROS_H_
16
17#include "absl/status/status.h"
18#include "absl/status/statusor.h"
20
21namespace absl {
22
23// Run a command that returns a absl::Status. If the called code returns an
24// error status, return that status up out of this method too.
25//
26// Example:
27// RETURN_IF_ERROR(DoThings(4));
28// RETURN_IF_ERROR(DoThings(5)) << "Additional error context";
29#define RETURN_IF_ERROR(expr) \
30 switch (0) \
31 case 0: \
32 default: \
33 if (const ::absl::Status status_macro_internal_adaptor = (expr); \
34 status_macro_internal_adaptor.ok()) { \
35 } else /* NOLINT */ \
36 return ::util::StatusBuilder(status_macro_internal_adaptor)
37
38// Executes an expression that returns an absl::StatusOr, extracting its value
39// into the variable defined by lhs (or returning on error).
40//
41// Example: Assigning to an existing value
42// ValueType value;
43// ASSIGN_OR_RETURN(value, MaybeGetValue(arg));
44// ASSIGN_OR_RETURN((auto [key, val]), MaybeGetValue(arg));
45//
46// WARNING: ASSIGN_OR_RETURN expands into multiple statements; it cannot be used
47// in a single statement (e.g. as the body of an if statement without {})!
48#define ASSIGN_OR_RETURN(lhs, rexpr) \
49 STATUS_MACROS_IMPL_ASSIGN_OR_RETURN_( \
50 STATUS_MACROS_IMPL_CONCAT_(_status_or_value, __COUNTER__), lhs, rexpr);
51
52#define STATUS_MACROS_IMPL_ASSIGN_OR_RETURN_(statusor, lhs, rexpr) \
53 auto statusor = (rexpr); \
54 RETURN_IF_ERROR(statusor.status()); \
55 STATUS_MACROS_IMPL_UNPARENTHESIS(lhs) = std::move(statusor).value()
56
57// Internal helpers for macro expansion.
58#define STATUS_MACROS_IMPL_UNPARENTHESIS_INNER(...) \
59 STATUS_MACROS_IMPL_UNPARENTHESIS_INNER_(__VA_ARGS__)
60#define STATUS_MACROS_IMPL_UNPARENTHESIS_INNER_(...) \
61 STATUS_MACROS_IMPL_VAN##__VA_ARGS__
62#define ISH(...) ISH __VA_ARGS__
63#define STATUS_MACROS_IMPL_VANISH
64
65// If the input is parenthesized, removes the parentheses. Otherwise expands to
66// the input unchanged.
67#define STATUS_MACROS_IMPL_UNPARENTHESIS(...) \
68 STATUS_MACROS_IMPL_UNPARENTHESIS_INNER(ISH __VA_ARGS__)
69
70// Internal helper for concatenating macro values.
71#define STATUS_MACROS_IMPL_CONCAT_INNER_(x, y) x##y
72#define STATUS_MACROS_IMPL_CONCAT_(x, y) STATUS_MACROS_IMPL_CONCAT_INNER_(x, y)
73
74} // namespace absl
75
76#endif // OR_TOOLS_BASE_STATUS_MACROS_H_
Definition: cleanup.h:22