From 0f49cb735735d6a3d237f20df50f9871a8aaf773 Mon Sep 17 00:00:00 2001 From: Francisco Paisana Date: Tue, 5 May 2020 16:48:03 +0100 Subject: [PATCH] abort if bad access --- lib/include/srslte/common/expected.h | 72 +++++++++++++++++++++----- lib/include/srslte/common/type_utils.h | 2 +- 2 files changed, 61 insertions(+), 13 deletions(-) diff --git a/lib/include/srslte/common/expected.h b/lib/include/srslte/common/expected.h index 99033d448..749e3b773 100644 --- a/lib/include/srslte/common/expected.h +++ b/lib/include/srslte/common/expected.h @@ -22,12 +22,12 @@ #ifndef SRSLTE_EXPECTED_H #define SRSLTE_EXPECTED_H +#include "type_utils.h" #include namespace srslte { -struct default_error_t { -}; +struct default_error_t {}; template class expected @@ -103,16 +103,64 @@ public: unexpected = std::forward(other); } } - operator bool() const { return has_value(); } - bool has_value() const { return has_val; } - const T& value() const& { return val; } - T& value() & { return val; } - T&& value() && { return std::move(val); } - const T&& value() const&& { return std::move(val); } - const E& error() const& { return unexpected; } - E& error() & { return unexpected; } - E&& error() && { return std::move(unexpected); } - const E&& error() const&& { return std::move(unexpected); } + operator bool() const { return has_value(); } + bool has_value() const { return has_val; } + const T& value() const& + { + if (not has_val) { + THROW_BAD_ACCESS("Bad expected value access"); + } + return val; + } + T& value() & + { + if (not has_val) { + THROW_BAD_ACCESS("Bad expected value access"); + } + return val; + } + T&& value() && + { + if (not has_val) { + THROW_BAD_ACCESS("Bad expected value access"); + } + return std::move(val); + } + const T&& value() const&& + { + if (not has_val) { + THROW_BAD_ACCESS("Bad expected value access"); + } + return std::move(val); + } + const E& error() const& + { + if (has_val) { + THROW_BAD_ACCESS("Bad expected error access"); + } + return unexpected; + } + E& error() & + { + if (has_val) { + THROW_BAD_ACCESS("Bad expected error access"); + } + return unexpected; + } + E&& error() && + { + if (has_val) { + THROW_BAD_ACCESS("Bad expected error access"); + } + return std::move(unexpected); + } + const E&& error() const&& + { + if (has_val) { + THROW_BAD_ACCESS("Bad expected error access"); + } + return std::move(unexpected); + } void swap(expected& other) noexcept { diff --git a/lib/include/srslte/common/type_utils.h b/lib/include/srslte/common/type_utils.h index fd31faa4f..12cc9d97c 100644 --- a/lib/include/srslte/common/type_utils.h +++ b/lib/include/srslte/common/type_utils.h @@ -41,7 +41,7 @@ public: #define THROW_BAD_ACCESS(msg) throw bad_type_access{msg}; #else #define THROW_BAD_ACCESS(msg) \ - fprintf(stderr, "ERROR: exception thrown at %s", msg); \ + fprintf(stderr, "ERROR: exception thrown with %s", msg); \ std::abort() #endif