blob: 76b35a07ec1afcc9220d0121c81e0fe555ca1e47 [file] [log] [blame]
// Copyright 2020 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef DIAGNOSTICS_COMMON_STATUS_MACROS_H_
#define DIAGNOSTICS_COMMON_STATUS_MACROS_H_
#include <utility>
#include "diagnostics/common/statusor.h"
#define RETURN_IF_ERROR(rexpr) \
do { \
auto status = (rexpr); \
if (!status.ok()) \
return status; \
} while (0)
// Generates multi-line expression.
#define ASSIGN_OR_RETURN(lhs, rexpr) \
STATUS_MACROS_IMPL_ASSIGN_OR_RETURN_( \
STATUS_MACROS_IMPL_CONCAT_(_status_or_value, __LINE__), lhs, rexpr)
// Implementation details.
#define STATUS_MACROS_IMPL_ASSIGN_OR_RETURN_(statusor, lhs, rexpr) \
auto statusor = (rexpr); \
if (!statusor.ok()) { \
return (statusor.status()); \
} \
lhs = std::move(statusor).value()
// Internal helper for concatenating macro values.
#define STATUS_MACROS_IMPL_CONCAT_INNER_(x, y) x##y
#define STATUS_MACROS_IMPL_CONCAT_(x, y) STATUS_MACROS_IMPL_CONCAT_INNER_(x, y)
#endif // DIAGNOSTICS_COMMON_STATUS_MACROS_H_