abort if bad access

This commit is contained in:
Francisco Paisana 2020-05-05 16:48:03 +01:00 committed by Francisco Paisana
parent a2b76a4a5f
commit 0f49cb7357
2 changed files with 61 additions and 13 deletions

View File

@ -22,12 +22,12 @@
#ifndef SRSLTE_EXPECTED_H #ifndef SRSLTE_EXPECTED_H
#define SRSLTE_EXPECTED_H #define SRSLTE_EXPECTED_H
#include "type_utils.h"
#include <memory> #include <memory>
namespace srslte { namespace srslte {
struct default_error_t { struct default_error_t {};
};
template <typename T, typename E = default_error_t> template <typename T, typename E = default_error_t>
class expected class expected
@ -103,16 +103,64 @@ public:
unexpected = std::forward<U>(other); unexpected = std::forward<U>(other);
} }
} }
operator bool() const { return has_value(); } operator bool() const { return has_value(); }
bool has_value() const { return has_val; } bool has_value() const { return has_val; }
const T& value() const& { return val; } const T& value() const&
T& value() & { return val; } {
T&& value() && { return std::move(val); } if (not has_val) {
const T&& value() const&& { return std::move(val); } THROW_BAD_ACCESS("Bad expected value access");
const E& error() const& { return unexpected; } }
E& error() & { return unexpected; } return val;
E&& error() && { return std::move(unexpected); } }
const E&& error() const&& { return std::move(unexpected); } 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 void swap(expected& other) noexcept
{ {

View File

@ -41,7 +41,7 @@ public:
#define THROW_BAD_ACCESS(msg) throw bad_type_access{msg}; #define THROW_BAD_ACCESS(msg) throw bad_type_access{msg};
#else #else
#define THROW_BAD_ACCESS(msg) \ #define THROW_BAD_ACCESS(msg) \
fprintf(stderr, "ERROR: exception thrown at %s", msg); \ fprintf(stderr, "ERROR: exception thrown with %s", msg); \
std::abort() std::abort()
#endif #endif