Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-07-07 08:32:45

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2008 Google Inc.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 // From: util/task/contrib/status_macros/status_macros.h
0009 
0010 #ifndef GOOGLE_PROTOBUF_STUBS_STATUS_MACROS_H_
0011 #define GOOGLE_PROTOBUF_STUBS_STATUS_MACROS_H_
0012 
0013 #include "absl/status/status.h"
0014 #include "absl/status/statusor.h"
0015 #include "google/protobuf/stubs/common.h"
0016 
0017 // Needs to be last.
0018 #include "google/protobuf/port_def.inc"  // NOLINT
0019 
0020 namespace google {
0021 namespace protobuf {
0022 namespace util {
0023 
0024 // Run a command that returns a util::Status.  If the called code returns an
0025 // error status, return that status up out of this method too.
0026 //
0027 // Example:
0028 //   RETURN_IF_ERROR(DoThings(4));
0029 #define RETURN_IF_ERROR(expr)                                                \
0030   do {                                                                       \
0031     /* Using _status below to avoid capture problems if expr is "status". */ \
0032     const absl::Status _status = (expr);                                     \
0033     if (ABSL_PREDICT_FALSE(!_status.ok())) return _status;                   \
0034   } while (0)
0035 
0036 // Internal helper for concatenating macro values.
0037 #define STATUS_MACROS_CONCAT_NAME_INNER(x, y) x##y
0038 #define STATUS_MACROS_CONCAT_NAME(x, y) STATUS_MACROS_CONCAT_NAME_INNER(x, y)
0039 
0040 template <typename T>
0041 absl::Status DoAssignOrReturn(T& lhs, absl::StatusOr<T> result) {
0042   if (result.ok()) {
0043     lhs = result.value();
0044   }
0045   return result.status();
0046 }
0047 
0048 #define ASSIGN_OR_RETURN_IMPL(status, lhs, rexpr)               \
0049   absl::Status status =                                         \
0050       ::google::protobuf::util::DoAssignOrReturn(lhs, (rexpr)); \
0051   if (ABSL_PREDICT_FALSE(!status.ok())) return status;
0052 
0053 // Executes an expression that returns a util::StatusOr, extracting its value
0054 // into the variable defined by lhs (or returning on error).
0055 //
0056 // Example: Assigning to an existing value
0057 //   ValueType value;
0058 //   ASSIGN_OR_RETURN(value, MaybeGetValue(arg));
0059 //
0060 // WARNING: ASSIGN_OR_RETURN expands into multiple statements; it cannot be used
0061 //  in a single statement (e.g. as the body of an if statement without {})!
0062 #define ASSIGN_OR_RETURN(lhs, rexpr) \
0063   ASSIGN_OR_RETURN_IMPL(             \
0064       STATUS_MACROS_CONCAT_NAME(_status_or_value, __COUNTER__), lhs, rexpr);
0065 
0066 }  // namespace util
0067 }  // namespace protobuf
0068 }  // namespace google
0069 
0070 #include "google/protobuf/port_undef.inc"  // NOLINT
0071 
0072 #endif  // GOOGLE_PROTOBUF_STUBS_STATUS_H_