Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:12:04

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 (PROTOBUF_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 = DoAssignOrReturn(lhs, (rexpr)); \
0050   if (PROTOBUF_PREDICT_FALSE(!status.ok())) return status;
0051 
0052 // Executes an expression that returns a util::StatusOr, extracting its value
0053 // into the variable defined by lhs (or returning on error).
0054 //
0055 // Example: Assigning to an existing value
0056 //   ValueType value;
0057 //   ASSIGN_OR_RETURN(value, MaybeGetValue(arg));
0058 //
0059 // WARNING: ASSIGN_OR_RETURN expands into multiple statements; it cannot be used
0060 //  in a single statement (e.g. as the body of an if statement without {})!
0061 #define ASSIGN_OR_RETURN(lhs, rexpr) \
0062   ASSIGN_OR_RETURN_IMPL(             \
0063       STATUS_MACROS_CONCAT_NAME(_status_or_value, __COUNTER__), lhs, rexpr);
0064 
0065 }  // namespace util
0066 }  // namespace protobuf
0067 }  // namespace google
0068 
0069 #include "google/protobuf/port_undef.inc"  // NOLINT
0070 
0071 #endif  // GOOGLE_PROTOBUF_STUBS_STATUS_H_