2020-01-31 01:32:18 +00:00
|
|
|
// Copyright 2020 The Pigweed Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
// use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
// the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing permissions and limitations under
|
|
|
|
// the License.
|
|
|
|
#pragma once
|
|
|
|
|
2020-02-13 19:25:10 +00:00
|
|
|
// Macros for cleanly working with Status or StatusWithSize objects in functions
|
|
|
|
// that return Status.
|
2020-01-31 01:32:18 +00:00
|
|
|
#define PW_TRY(expr) _PW_TRY(_PW_TRY_UNIQUE(__LINE__), expr)
|
|
|
|
|
2020-02-13 19:25:10 +00:00
|
|
|
#define _PW_TRY(result, expr) \
|
|
|
|
do { \
|
|
|
|
if (auto result = (expr); !result.ok()) { \
|
|
|
|
return ::pw::internal::ConvertToStatus(result); \
|
|
|
|
} \
|
2020-01-31 01:32:18 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define PW_TRY_ASSIGN(assignment_lhs, expression) \
|
|
|
|
_PW_TRY_ASSIGN(_PW_TRY_UNIQUE(__LINE__), assignment_lhs, expression)
|
|
|
|
|
2020-02-13 19:25:10 +00:00
|
|
|
#define _PW_TRY_ASSIGN(result, lhs, expr) \
|
|
|
|
auto result = (expr); \
|
|
|
|
if (!result.ok()) { \
|
|
|
|
return ::pw::internal::ConvertToStatus(result); \
|
|
|
|
} \
|
2020-01-31 01:32:18 +00:00
|
|
|
lhs = std::move(result.size())
|
|
|
|
|
2020-02-13 19:25:10 +00:00
|
|
|
// Macro for cleanly working with Status or StatusWithSize objects in functions
|
|
|
|
// that return StatusWithSize.
|
|
|
|
#define PW_TRY_WITH_SIZE(expr) _PW_TRY_WITH_SIZE(_PW_TRY_UNIQUE(__LINE__), expr)
|
|
|
|
|
|
|
|
#define _PW_TRY_WITH_SIZE(result, expr) \
|
|
|
|
do { \
|
|
|
|
if (auto result = (expr); !result.ok()) { \
|
|
|
|
return ::pw::internal::ConvertToStatusWithSize(result); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2020-01-31 01:32:18 +00:00
|
|
|
#define _PW_TRY_UNIQUE(line) _PW_TRY_UNIQUE_EXPANDED(line)
|
|
|
|
#define _PW_TRY_UNIQUE_EXPANDED(line) _pw_try_unique_name_##line
|
|
|
|
|
|
|
|
#define TRY PW_TRY
|
2020-02-13 19:25:10 +00:00
|
|
|
#define TRY_WITH_SIZE PW_TRY_WITH_SIZE
|
2020-01-31 01:32:18 +00:00
|
|
|
#define TRY_ASSIGN PW_TRY_ASSIGN
|
2020-02-13 19:25:10 +00:00
|
|
|
|
|
|
|
namespace pw::internal {
|
|
|
|
|
|
|
|
inline Status ConvertToStatus(Status status) { return status; }
|
|
|
|
|
|
|
|
inline Status ConvertToStatus(StatusWithSize status_with_size) {
|
|
|
|
return status_with_size.status();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline StatusWithSize ConvertToStatusWithSize(Status status) {
|
|
|
|
return StatusWithSize(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline StatusWithSize ConvertToStatusWithSize(StatusWithSize status_with_size) {
|
|
|
|
return status_with_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace pw::internal
|