abort if bad access

master
Francisco Paisana 5 years ago committed by Francisco Paisana
parent a2b76a4a5f
commit 0f49cb7357

@ -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
{ {

@ -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

Loading…
Cancel
Save