1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 // From: util/task/contrib/status_macros/status_macros.h
32 
33 #ifndef GOOGLE_PROTOBUF_STUBS_STATUS_MACROS_H_
34 #define GOOGLE_PROTOBUF_STUBS_STATUS_MACROS_H_
35 
36 #include <google/protobuf/stubs/common.h>
37 #include <google/protobuf/stubs/status.h>
38 #include <google/protobuf/stubs/statusor.h>
39 
40 namespace google {
41 namespace protobuf {
42 namespace util {
43 
44 // Run a command that returns a util::Status.  If the called code returns an
45 // error status, return that status up out of this method too.
46 //
47 // Example:
48 //   RETURN_IF_ERROR(DoThings(4));
49 #define RETURN_IF_ERROR(expr)                                                \
50   do {                                                                       \
51     /* Using _status below to avoid capture problems if expr is "status". */ \
52     const PROTOBUF_NAMESPACE_ID::util::Status _status = (expr);              \
53     if (PROTOBUF_PREDICT_FALSE(!_status.ok())) return _status;               \
54   } while (0)
55 
56 // Internal helper for concatenating macro values.
57 #define STATUS_MACROS_CONCAT_NAME_INNER(x, y) x##y
58 #define STATUS_MACROS_CONCAT_NAME(x, y) STATUS_MACROS_CONCAT_NAME_INNER(x, y)
59 
60 template<typename T>
DoAssignOrReturn(T & lhs,StatusOr<T> result)61 Status DoAssignOrReturn(T& lhs, StatusOr<T> result) {
62   if (result.ok()) {
63     lhs = result.ValueOrDie();
64   }
65   return result.status();
66 }
67 
68 #define ASSIGN_OR_RETURN_IMPL(status, lhs, rexpr) \
69   Status status = DoAssignOrReturn(lhs, (rexpr)); \
70   if (PROTOBUF_PREDICT_FALSE(!status.ok())) return status;
71 
72 // Executes an expression that returns a util::StatusOr, extracting its value
73 // into the variable defined by lhs (or returning on error).
74 //
75 // Example: Assigning to an existing value
76 //   ValueType value;
77 //   ASSIGN_OR_RETURN(value, MaybeGetValue(arg));
78 //
79 // WARNING: ASSIGN_OR_RETURN expands into multiple statements; it cannot be used
80 //  in a single statement (e.g. as the body of an if statement without {})!
81 #define ASSIGN_OR_RETURN(lhs, rexpr) \
82   ASSIGN_OR_RETURN_IMPL( \
83       STATUS_MACROS_CONCAT_NAME(_status_or_value, __COUNTER__), lhs, rexpr);
84 
85 }  // namespace util
86 }  // namespace protobuf
87 }  // namespace google
88 
89 #endif  // GOOGLE_PROTOBUF_STUBS_STATUS_H_
90