00001 // Copyright (c) 2017, The Regents of the University of California 00002 // All rights reserved. 00003 // 00004 // Redistribution and use in source and binary forms, with or without 00005 // modification, are permitted provided that the following conditions are met: 00006 // * Redistributions of source code must retain the above copyright 00007 // notice, this list of conditions and the following disclaimer. 00008 // * Redistributions in binary form must reproduce the above copyright 00009 // notice, this list of conditions and the following disclaimer in the 00010 // documentation and/or other materials provided with the distribution. 00011 // * Neither the name of the University of California nor the 00012 // names of its contributors may be used to endorse or promote products 00013 // derived from this software without specific prior written permission. 00014 // 00015 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00016 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00017 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00018 // ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OF THE UNIVERSITY OF CALIFORNIA 00019 // BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00020 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00021 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00022 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00023 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00024 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00025 // POSSIBILITY OF SUCH DAMAGE. 00026 00027 #ifndef UTIL_STATUS_MACROS_H_ 00028 #define UTIL_STATUS_MACROS_H_ 00029 00030 #include "util/status.h" 00031 #include "util/statusor.h" 00032 00033 namespace util { 00034 00035 // Run a command that returns a util::Status. If the called code returns an 00036 // error status, return that status up out of this method too. 00037 // 00038 // Example: 00039 // RETURN_IF_ERROR(DoThings(4)); 00040 #define RETURN_IF_ERROR(expr) \ 00041 do { \ 00042 /* Using _status below to avoid capture problems if expr is "status". */ \ 00043 const ::util::Status _status = (expr); \ 00044 if (!_status.ok()) return _status; \ 00045 } while (0) 00046 00047 // Internal helper for concatenating macro values. 00048 #define STATUS_MACROS_CONCAT_NAME_INNER(x, y) x##y 00049 #define STATUS_MACROS_CONCAT_NAME(x, y) STATUS_MACROS_CONCAT_NAME_INNER(x, y) 00050 00051 template<typename T> 00052 Status DoAssignOrReturn(T& lhs, StatusOr<T> result) { // NOLINT 00053 if (result.ok()) { 00054 lhs = result.ValueOrDie(); 00055 } 00056 return result.status(); 00057 } 00058 00059 #define ASSIGN_OR_RETURN_IMPL(status, lhs, rexpr) \ 00060 ::util::Status status = DoAssignOrReturn(lhs, (rexpr)); \ 00061 if (!status.ok()) return status; 00062 00063 // Executes an expression that returns a util::StatusOr, extracting its value 00064 // into the variable defined by lhs (or returning on error). 00065 // 00066 // Example: Assigning to an existing value 00067 // ValueType value; 00068 // ASSIGN_OR_RETURN(value, MaybeGetValue(arg)); 00069 // 00070 // WARNING: ASSIGN_OR_RETURN expands into multiple statements; it cannot be used 00071 // in a single statement (e.g. as the body of an if statement without {})! 00072 #define ASSIGN_OR_RETURN(lhs, rexpr) \ 00073 ASSIGN_OR_RETURN_IMPL( \ 00074 STATUS_MACROS_CONCAT_NAME(_status_or_value, __COUNTER__), lhs, rexpr); 00075 00076 } // namespace util 00077 00078 #endif // UTIL_STATUS_MACROS_H_ 00079 00080 00081 // Protocol Buffers - Google's data interchange format 00082 // Copyright 2008 Google Inc. All rights reserved. 00083 // https://developers.google.com/protocol-buffers/ 00084 // 00085 // Redistribution and use in source and binary forms, with or without 00086 // modification, are permitted provided that the following conditions are 00087 // met: 00088 // 00089 // * Redistributions of source code must retain the above copyright 00090 // notice, this list of conditions and the following disclaimer. 00091 // * Redistributions in binary form must reproduce the above 00092 // copyright notice, this list of conditions and the following disclaimer 00093 // in the documentation and/or other materials provided with the 00094 // distribution. 00095 // * Neither the name of Google Inc. nor the names of its 00096 // contributors may be used to endorse or promote products derived from 00097 // this software without specific prior written permission. 00098 // 00099 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00100 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00101 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00102 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00103 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00104 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00105 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00106 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00107 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00108 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00109 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.