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


gcloud_speech_utils
Author(s):
autogenerated on Mon Jun 10 2019 13:20:56