Google OR-Tools v9.9
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
status_macros.h
Go to the documentation of this file.
1// Copyright 2010-2024 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
21// Run a command that returns a absl::Status. If the called code returns an
22// error status, return that status up out of this method too.
23//
24// Example:
25// RETURN_IF_ERROR(DoThings(4));
26// RETURN_IF_ERROR(DoThings(5)) << "Additional error context";
27#define RETURN_IF_ERROR(expr) \
28 switch (0) \
29 case 0: \
30 default: \
31 if (const ::absl::Status status_macro_internal_adaptor = (expr); \
32 status_macro_internal_adaptor.ok()) { \
33 } else /* NOLINT */ \
34 return ::util::StatusBuilder(status_macro_internal_adaptor)
35
36// Executes an expression that returns an absl::StatusOr, extracting its value
37// into the variable defined by lhs (or returning on error).
38//
39// Example: Assigning to an existing value
40// ValueType value;
41// ASSIGN_OR_RETURN(value, MaybeGetValue(arg));
42// ASSIGN_OR_RETURN((auto [key, val]), MaybeGetValue(arg));
43//
44// WARNING: ASSIGN_OR_RETURN expands into multiple statements; it cannot be used
45// in a single statement (e.g. as the body of an if statement without {})!
46#define ASSIGN_OR_RETURN(lhs, rexpr) \
47 STATUS_MACROS_IMPL_ASSIGN_OR_RETURN_( \
48 STATUS_MACROS_IMPL_CONCAT_(_status_or_value, __COUNTER__), lhs, rexpr);
49
50#define STATUS_MACROS_IMPL_ASSIGN_OR_RETURN_(statusor, lhs, rexpr) \
51 auto statusor = (rexpr); \
52 RETURN_IF_ERROR(statusor.status()); \
53 STATUS_MACROS_IMPL_UNPARENTHESIS(lhs) = std::move(statusor).value()
54
55// Internal helpers for macro expansion.
56#define STATUS_MACROS_IMPL_UNPARENTHESIS_INNER(...) \
57 STATUS_MACROS_IMPL_UNPARENTHESIS_INNER_(__VA_ARGS__)
58#define STATUS_MACROS_IMPL_UNPARENTHESIS_INNER_(...) \
59 STATUS_MACROS_IMPL_VAN##__VA_ARGS__
60#define ISH(...) ISH __VA_ARGS__
61#define STATUS_MACROS_IMPL_VANISH
62
63// If the input is parenthesized, removes the parentheses. Otherwise expands to
64// the input unchanged.
65#define STATUS_MACROS_IMPL_UNPARENTHESIS(...) \
66 STATUS_MACROS_IMPL_UNPARENTHESIS_INNER(ISH __VA_ARGS__)
67
68// Internal helper for concatenating macro values.
69#define STATUS_MACROS_IMPL_CONCAT_INNER_(x, y) x##y
70#define STATUS_MACROS_IMPL_CONCAT_(x, y) STATUS_MACROS_IMPL_CONCAT_INNER_(x, y)
71
72#endif // OR_TOOLS_BASE_STATUS_MACROS_H_