Skip to content

Commit

Permalink
[FIXUP] Make error UNWRAP compile
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanLF6768 committed Jun 16, 2024
1 parent 65ffcc4 commit c3eebe1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
24 changes: 23 additions & 1 deletion common/Inc/obc/bus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,32 @@ concept Buffer = requires(T& buf, V v, std::size_t i) {
} && std::semiregular<V>;

/**
* @brief Convert a trivial struct into a byte buffer.
* @brief Convert a trivial struct into a readonly byte buffer.
*
* Can be used to facilitate trivial deserialization of C-style structs sent on
* a bus.
*
* @warning For portability, the struct should be tightly packed (containing
* explicit padding fields if required) with fixed sized integers.
*
* @tparam T type of the struct to translate.
*/
template<typename T>
requires std::is_trivially_copyable_v<T>
auto StructAsBuffer(const T& s) -> std::span<const std::byte, sizeof(T)> {
// This function is constrained to only operate on POD structs
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
return {reinterpret_cast<const std::byte*>(&s), sizeof(T)};
}

/**
* @brief Convert a trivial struct into a read-write byte buffer.
*
* Can be used to facilitate trivial deserialization of C-style structs sent on
* a bus.
*
* @warning For portability, the struct should be tightly packed (containing
* explicit padding fields if required) with fixed sized integers.
*
* @tparam T type of the struct to translate.
*/
Expand Down
30 changes: 19 additions & 11 deletions common/Inc/obc/utils/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,23 @@ concept ExpectedReturn =
Specializes<T, std::expected> && MaybeError<typename T::error_type> &&
std::convertible_to<T, std::expected<R, typename T::error_type>>;

#define UNWRAP_ASSIGN_IMPL(lhs, rexpr, temp) \
auto temp = rexpr; \
static_assert(obc::utils::ExpectedReturn<std::remove_cvref<temp>>); \
if constexpr (!std::same_as< \
std::remove_cvref<temp>::error_type, \
obc::utils::Never>) { \
if (!temp) return std::unexpected(std::move(temp).error()); \
} \
lhs = std::move(temp.value());

#define UNWRAP_ASSIGN(lhs, rexpr) UNWRAP_ASSIGN_IMPL(lhs, rexpr, TEMP_NAME)
/*
* This functionality is impossible is impossible to implement without
* macros since unwrapping is an operation which can return from the
* enclosing context.
*/
// NOLINTBEGIN(cppcoreguidelines-macro-usage)
#define UNWRAP(expr) \
({ \
auto res {expr}; \
static_assert(obc::utils::Specializes<decltype(res), std::expected>); \
if constexpr (!std::same_as< \
typename std::remove_cvref<decltype(res \
)>::error_type, \
obc::utils::Never>) { \
if (!res) return std::unexpected(std::move(res.error())); \
} \
res.value(); \
})
// NOLINTEND(cppcoreguidelines-macro-usage)
} // namespace obc::utils

0 comments on commit c3eebe1

Please sign in to comment.