Asylo
Macros
status_macros.h File Reference
#include "absl/base/optimization.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.

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 .ValueOrDie() methods. More...
 

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).ValueOrDie(); \
} while (false)

Evaluates an expression rexpr that returns a StatusOr-like object with .ok(), .status(), and .ValueOrDie() 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 { \
const auto _asylo_status_to_verify = (expr); \
if (ABSL_PREDICT_FALSE(!_asylo_status_to_verify.ok())) { \
return _asylo_status_to_verify; \
} \
} while (false)

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.

Example:

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