Google OR-Tools v9.11
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_UTIL_STATUS_MACROS_H_
15#define OR_TOOLS_UTIL_STATUS_MACROS_H_
16
18
19// Executes an expression that returns an absl::StatusOr, extracting its value
20// into the variable defined by lhs or returning on error and enabling
21// decorating it with the third argument.
22//
23// Example: Assigning to an existing value
24// ValueType value;
25// OR_ASSIGN_OR_RETURN3(value, MaybeGetValue(arg), _ << "context");
26// OR_ASSIGN_OR_RETURN3((auto [key, val]), MaybeGetValue(arg),
27// _ << "with key " << key);
28//
29// WARNING: OR_ASSIGN_OR_RETURN3 expands into multiple statements; it cannot be
30// used in a single statement (e.g. as the body of an if statement without {})!
31//
32#define OR_ASSIGN_OR_RETURN3(lhs, rexpr, error_expression) \
33 OR_ASSIGN_OR_RETURN3_IMPL_( \
34 STATUS_MACROS_IMPL_CONCAT_(_status_or_value, __COUNTER__), lhs, rexpr, \
35 error_expression)
36
37#define OR_ASSIGN_OR_RETURN3_IMPL_(statusor, lhs, rexpr, error_expression) \
38 auto statusor = (rexpr); \
39 if (!statusor.ok()) { \
40 ::util::StatusBuilder _(std::move(statusor).status()); \
41 return (error_expression); \
42 } \
43 STATUS_MACROS_IMPL_UNPARENTHESIS(lhs) = std::move(statusor).value()
44
45#endif // OR_TOOLS_UTIL_STATUS_MACROS_H_