mirror of https://github.com/pvnis/srsRAN_4G.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
195 lines
3.9 KiB
C
195 lines
3.9 KiB
C
4 years ago
|
/**
|
||
5 years ago
|
*
|
||
4 years ago
|
* \section COPYRIGHT
|
||
5 years ago
|
*
|
||
4 years ago
|
* Copyright 2013-2021 Software Radio Systems Limited
|
||
5 years ago
|
*
|
||
4 years ago
|
* By using this file, you agree to the terms and conditions set
|
||
|
* forth in the LICENSE file which can be found at the top level of
|
||
|
* the distribution.
|
||
5 years ago
|
*
|
||
|
*/
|
||
|
|
||
4 years ago
|
#ifndef SRSRAN_EXPECTED_H
|
||
|
#define SRSRAN_EXPECTED_H
|
||
5 years ago
|
|
||
4 years ago
|
#include "adt_utils.h"
|
||
5 years ago
|
#include <memory>
|
||
5 years ago
|
#include <system_error>
|
||
5 years ago
|
|
||
4 years ago
|
namespace srsran {
|
||
5 years ago
|
|
||
5 years ago
|
struct default_error_t {};
|
||
5 years ago
|
|
||
|
template <typename T, typename E = default_error_t>
|
||
|
class expected
|
||
|
{
|
||
4 years ago
|
static_assert(not std::is_same<T, E>::value, "Expected and unexpected types cannot be of the same type");
|
||
|
|
||
5 years ago
|
public:
|
||
|
expected() : has_val(true), val(T{}) {}
|
||
|
expected(T&& t) : has_val(true), val(std::forward<T>(t)) {}
|
||
|
expected(E&& e) : has_val(false), unexpected(std::forward<E>(e)) {}
|
||
5 years ago
|
expected(const expected& other)
|
||
5 years ago
|
{
|
||
5 years ago
|
if (other.has_val) {
|
||
5 years ago
|
construct_val(other.val);
|
||
|
} else {
|
||
|
construct_error(other.unexpected);
|
||
|
}
|
||
|
}
|
||
5 years ago
|
expected(expected&& other) noexcept
|
||
5 years ago
|
{
|
||
5 years ago
|
if (other.has_val) {
|
||
5 years ago
|
construct_val(std::move(other.val));
|
||
|
} else {
|
||
|
construct_error(std::move(other.unexpected));
|
||
|
}
|
||
|
}
|
||
|
expected& operator=(const expected& other)
|
||
|
{
|
||
|
if (this != &other) {
|
||
|
expected(other).swap(*this);
|
||
|
}
|
||
|
return *this;
|
||
|
}
|
||
|
expected& operator=(expected&& other) noexcept
|
||
|
{
|
||
|
expected(std::move(other)).swap(*this);
|
||
|
return *this;
|
||
|
}
|
||
|
expected& operator=(const T& other) noexcept
|
||
|
{
|
||
|
if (not has_value()) {
|
||
|
unexpected.~E();
|
||
|
has_val = true;
|
||
|
}
|
||
|
val = other;
|
||
|
return *this;
|
||
|
}
|
||
|
expected& operator=(T&& other) noexcept
|
||
|
{
|
||
|
if (not has_value()) {
|
||
|
unexpected.~E();
|
||
|
has_val = true;
|
||
|
}
|
||
|
val = std::move(other);
|
||
|
return *this;
|
||
|
}
|
||
|
~expected() { destroy(); }
|
||
|
|
||
|
void set_error()
|
||
|
{
|
||
|
if (has_value()) {
|
||
|
val.~T();
|
||
|
construct_error(E{});
|
||
|
} else {
|
||
|
unexpected = E{};
|
||
|
}
|
||
|
}
|
||
|
template <typename U>
|
||
|
void set_error(U&& other)
|
||
|
{
|
||
|
if (has_value()) {
|
||
|
val.~T();
|
||
|
construct_error(std::forward<U>(other));
|
||
|
} else {
|
||
|
unexpected = std::forward<U>(other);
|
||
|
}
|
||
|
}
|
||
5 years ago
|
|
||
|
explicit operator bool() const { return has_value(); }
|
||
5 years ago
|
bool has_value() const { return has_val; }
|
||
5 years ago
|
bool is_error() const { return not has_value(); }
|
||
|
const T& value() const
|
||
5 years ago
|
{
|
||
|
if (not has_val) {
|
||
|
THROW_BAD_ACCESS("Bad expected value access");
|
||
|
}
|
||
|
return val;
|
||
|
}
|
||
5 years ago
|
T& value()
|
||
5 years ago
|
{
|
||
|
if (not has_val) {
|
||
|
THROW_BAD_ACCESS("Bad expected value access");
|
||
|
}
|
||
|
return val;
|
||
|
}
|
||
5 years ago
|
const E& error() const
|
||
5 years ago
|
{
|
||
|
if (has_val) {
|
||
|
THROW_BAD_ACCESS("Bad expected error access");
|
||
|
}
|
||
|
return unexpected;
|
||
|
}
|
||
5 years ago
|
E& error()
|
||
5 years ago
|
{
|
||
|
if (has_val) {
|
||
|
THROW_BAD_ACCESS("Bad expected error access");
|
||
|
}
|
||
|
return unexpected;
|
||
|
}
|
||
5 years ago
|
|
||
|
void swap(expected& other) noexcept
|
||
|
{
|
||
|
using std::swap;
|
||
|
|
||
|
if (has_value() and other.has_value()) {
|
||
|
swap(val, other.val);
|
||
|
} else if (not has_value() and not other.has_value()) {
|
||
|
swap(unexpected, other.unexpected);
|
||
|
} else if (has_value() and not other.has_value()) {
|
||
5 years ago
|
E err(std::move(other.unexpected));
|
||
5 years ago
|
other.unexpected.~E();
|
||
|
other.construct_val(std::move(val));
|
||
|
val.~T();
|
||
|
construct_error(std::move(err));
|
||
|
} else if (!bool(*this) && bool(other)) {
|
||
|
other.swap(*this);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private:
|
||
|
void construct_val(const T& v) noexcept
|
||
|
{
|
||
|
has_val = true;
|
||
|
new (&val) T(v);
|
||
|
}
|
||
|
void construct_val(T&& v) noexcept
|
||
|
{
|
||
|
has_val = true;
|
||
|
new (&val) T(std::move(v));
|
||
|
}
|
||
|
void construct_error(const E& e) noexcept
|
||
|
{
|
||
|
has_val = false;
|
||
|
new (&unexpected) E(e);
|
||
|
}
|
||
|
void construct_error(E&& e) noexcept
|
||
|
{
|
||
|
has_val = false;
|
||
|
new (&unexpected) E(std::move(e));
|
||
|
}
|
||
|
void destroy()
|
||
|
{
|
||
|
if (has_value()) {
|
||
|
val.~T();
|
||
|
} else {
|
||
|
unexpected.~E();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
bool has_val = false;
|
||
|
union {
|
||
|
T val;
|
||
|
E unexpected;
|
||
|
};
|
||
|
};
|
||
|
|
||
5 years ago
|
template <typename E>
|
||
5 years ago
|
using error_type = expected<std::true_type, E>;
|
||
5 years ago
|
|
||
4 years ago
|
} // namespace srsran
|
||
5 years ago
|
|
||
4 years ago
|
#endif // SRSRAN_EXPECTED_H
|