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
#define SRSLTE_EXPECTED_H
#include "type_utils.h"
#include <memory>
namespace srslte {
struct default_error_t {
};
struct default_error_t {};
template <typename T, typename E = default_error_t>
class expected
@ -105,14 +105,62 @@ public:
}
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); }
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
{

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

Loading…
Cancel
Save