Home | History | Annotate | Download | only in lib
      1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 // Helper macros for dealing with the port::Status datatype.
     17 
     18 #ifndef TENSORFLOW_STREAM_EXECUTOR_LIB_STATUS_MACROS_H_
     19 #define TENSORFLOW_STREAM_EXECUTOR_LIB_STATUS_MACROS_H_
     20 
     21 // Early-returns the status if it is in error; otherwise, proceeds.
     22 //
     23 // The argument expression is guaranteed to be evaluated exactly once.
     24 #define SE_RETURN_IF_ERROR(__status) \
     25   do {                               \
     26     auto status = __status;          \
     27     if (!status.ok()) {              \
     28       return status;                 \
     29     }                                \
     30   } while (false)
     31 
     32 // Identifier concatenation helper macros.
     33 #define SE_MACRO_CONCAT_INNER(__x, __y) __x##__y
     34 #define SE_MACRO_CONCAT(__x, __y) SE_MACRO_CONCAT_INNER(__x, __y)
     35 
     36 // Implementation of SE_ASSIGN_OR_RETURN that uses a unique temporary identifier
     37 // for avoiding collision in the enclosing scope.
     38 #define SE_ASSIGN_OR_RETURN_IMPL(__lhs, __rhs, __name) \
     39   auto __name = (__rhs);                               \
     40   if (!__name.ok()) {                                  \
     41     return __name.status();                            \
     42   }                                                    \
     43   __lhs = std::move(__name.ValueOrDie());
     44 
     45 // Early-returns the status if it is in error; otherwise, assigns the
     46 // right-hand-side expression to the left-hand-side expression.
     47 //
     48 // The right-hand-side expression is guaranteed to be evaluated exactly once.
     49 #define SE_ASSIGN_OR_RETURN(__lhs, __rhs) \
     50   SE_ASSIGN_OR_RETURN_IMPL(__lhs, __rhs,  \
     51                            SE_MACRO_CONCAT(__status_or_value, __COUNTER__))
     52 
     53 #endif  // TENSORFLOW_STREAM_EXECUTOR_LIB_STATUS_MACROS_H_
     54