Asylo
Namespaces | Macros | Functions
status_macros.h File Reference
#include <type_traits>
#include <utility>
#include "absl/base/optimization.h"
#include "absl/status/status.h"
Include dependency graph for status_macros.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Namespaces

 asylo
 
 asylo::internal
 

Macros

#define ASYLO_RETURN_IF_ERROR(expr)
 Evaluates an expression that produces an Status-like object with a .ok() method. More...
 
#define ASYLO_ASSIGN_OR_RETURN(lhs, rexpr)
 Evaluates an expression rexpr that returns a StatusOr-like object with .ok(), .status(), and .value() methods. More...
 

Functions

template<typename T >
constexpr bool asylo::internal::HasStatus (decltype(std::declval< T >().status()) *)
 
template<typename T >
constexpr bool asylo::internal::HasStatus (...)
 
template<typename T , typename std::enable_if< HasStatus< T >(nullptr), int >::type = 0>
auto asylo::internal::ToStatus (T &&status_or) -> decltype(status_or.status())
 
template<typename T , typename std::enable_if<!HasStatus< T >(nullptr), int >::type = 0>
asylo::internal::ToStatus (T &&status_like)
 

Macro Definition Documentation

◆ ASYLO_ASSIGN_OR_RETURN

#define ASYLO_ASSIGN_OR_RETURN (   lhs,
  rexpr 
)
Value:
do { \
auto _asylo_status_or_value = (rexpr); \
if (ABSL_PREDICT_FALSE(!_asylo_status_or_value.ok())) { \
return _asylo_status_or_value.status(); \
} \
lhs = std::move(_asylo_status_or_value).value(); \
} while (false)

Evaluates an expression rexpr that returns a StatusOr-like object with .ok(), .status(), and .value() methods.

If the result is OK, moves its value into the variable defined by lhs, otherwise returns the result of the .status() from the current function. The error result of .status is returned unchanged. If there is an error, lhs is not evaluated: thus any side effects of evaluating lhs will only occur if rexpr.ok() is true.

Interface:

Example: Assigning to an existing variable:

ValueType value;
ASYLO_ASSIGN_OR_RETURN(value, MaybeGetValue(arg));

Example: Assigning to an expression with side effects:

MyProto data;
ASYLO_ASSIGN_OR_RETURN(*data.mutable_str(), MaybeGetValue(arg));
// No field "str" is added on error.

Example: Assigning to a std::unique_ptr.

std::unique_ptr<T> ptr;
ASYLO_ASSIGN_OR_RETURN(ptr, MaybeGetPtr(arg));

◆ ASYLO_RETURN_IF_ERROR

#define ASYLO_RETURN_IF_ERROR (   expr)
Value:
do { \
auto _asylo_status_to_verify = (expr); \
if (ABSL_PREDICT_FALSE(!_asylo_status_to_verify.ok())) { \
std::forward<decltype(_asylo_status_to_verify)>( \
_asylo_status_to_verify)); \
} \
} while (false)
T ToStatus(T &&status_like)
Definition: status_macros.h:53

Evaluates an expression that produces an Status-like object with a .ok() method.

If this method returns false, the object is returned from the current function. If the expression evaluates to a StatusOr object, then it is converted to a Status on return.

Example:

::asylo::Status MultiStepFunction() {
ASYLO_RETURN_IF_ERROR(Function(args...));
ASYLO_RETURN_IF_ERROR(foo.Method(args...));
}