created error_type and cleaned up expected

This commit is contained in:
Francisco Paisana 2020-05-07 17:41:11 +01:00 committed by Francisco Paisana
parent 9a7f48b6a5
commit 791d84e639
7 changed files with 45 additions and 68 deletions

View File

@ -94,7 +94,7 @@ public:
bool try_push(const myobj& value) { return push_(value, false); }
srslte::expected<bool, myobj> try_push(myobj&& value) { return push_(std::move(value), false); }
srslte::error_type<myobj> try_push(myobj&& value) { return push_(std::move(value), false); }
bool try_pop(myobj* value) { return pop_(value, false); }
@ -178,7 +178,7 @@ private:
return true;
}
srslte::expected<bool, myobj> push_(myobj&& value, bool block)
srslte::error_type<myobj> push_(myobj&& value, bool block)
{
if (!enable) {
return std::move(value);
@ -192,7 +192,7 @@ private:
q.push(std::move(value));
pthread_mutex_unlock(&mutex);
pthread_cond_signal(&cv_empty);
return true;
return {};
}
pthread_mutex_unlock(&mutex);
return std::move(value);

View File

@ -22,11 +22,26 @@
#ifndef SRSLTE_EXPECTED_H
#define SRSLTE_EXPECTED_H
#include "type_utils.h"
#include <memory>
#include <system_error>
namespace srslte {
#if defined(__cpp_exceptions) && (1 == __cpp_exceptions)
class bad_type_access : public std::runtime_error
{
public:
explicit bad_type_access(const std::string& what_arg) : runtime_error(what_arg) {}
explicit bad_type_access(const char* what_arg) : runtime_error(what_arg) {}
};
#define THROW_BAD_ACCESS(msg) throw bad_type_access{msg};
#else
#define THROW_BAD_ACCESS(msg) \
fprintf(stderr, "ERROR: exception thrown with %s", msg); \
std::abort()
#endif
struct default_error_t {};
template <typename T, typename E = default_error_t>
@ -103,64 +118,38 @@ public:
unexpected = std::forward<U>(other);
}
}
operator bool() const { return has_value(); }
explicit operator bool() const { return has_value(); }
bool has_value() const { return has_val; }
const T& value() const&
bool is_error() const { return not has_value(); }
const T& value() const
{
if (not has_val) {
THROW_BAD_ACCESS("Bad expected value access");
}
return val;
}
T& value() &
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&
const E& error() const
{
if (has_val) {
THROW_BAD_ACCESS("Bad expected error access");
}
return unexpected;
}
E& error() &
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
{
@ -218,6 +207,11 @@ private:
};
};
struct success_t {};
template <typename E>
using error_type = expected<success_t, E>;
} // namespace srslte
#endif // SRSLTE_EXPECTED_H

View File

@ -22,6 +22,7 @@
#ifndef SRSLTE_TYPE_UTILS_H
#define SRSLTE_TYPE_UTILS_H
#include "expected.h"
#include <cstring>
#include <limits>
#include <memory>
@ -30,21 +31,6 @@
namespace srslte {
#if defined(__cpp_exceptions) && (1 == __cpp_exceptions)
class bad_type_access : public std::runtime_error
{
public:
explicit bad_type_access(const std::string& what_arg) : runtime_error(what_arg) {}
explicit bad_type_access(const char* what_arg) : runtime_error(what_arg) {}
};
#define THROW_BAD_ACCESS(msg) throw bad_type_access{msg};
#else
#define THROW_BAD_ACCESS(msg) \
fprintf(stderr, "ERROR: exception thrown with %s", msg); \
std::abort()
#endif
/************************************
* get_type_name methods
***********************************/

View File

@ -56,7 +56,7 @@ public:
}
void write(unique_byte_buffer_t msg) { queue.push(std::move(msg)); }
srslte::expected<bool, unique_byte_buffer_t> try_write(unique_byte_buffer_t&& msg)
srslte::error_type<unique_byte_buffer_t> try_write(unique_byte_buffer_t&& msg)
{
return queue.try_push(std::move(msg));
}

View File

@ -44,8 +44,7 @@ rlc_am_lte::rlc_am_lte(srslte::log_ref log_,
lcid(lcid_),
tx(this),
rx(this)
{
}
{}
// Applies new configuration. Must be just reestablished or initiated
bool rlc_am_lte::configure(const rlc_config_t& cfg_)
@ -354,9 +353,9 @@ void rlc_am_lte::rlc_am_lte_tx::write_sdu(unique_byte_buffer_t sdu, bool blockin
tx_sdu_queue.write(std::move(sdu));
} else {
// non-blocking write
uint8_t* msg_ptr = sdu->msg;
uint32_t nof_bytes = sdu->N_bytes;
srslte::expected<bool, unique_byte_buffer_t> ret = tx_sdu_queue.try_write(std::move(sdu));
uint8_t* msg_ptr = sdu->msg;
uint32_t nof_bytes = sdu->N_bytes;
srslte::error_type<unique_byte_buffer_t> ret = tx_sdu_queue.try_write(std::move(sdu));
if (ret) {
log->info_hex(
msg_ptr, nof_bytes, "%s Tx SDU (%d B, tx_sdu_queue_len=%d)\n", RB_NAME, nof_bytes, tx_sdu_queue.size());

View File

@ -97,9 +97,9 @@ void rlc_tm::write_sdu(unique_byte_buffer_t sdu, bool blocking)
ul_queue.size_bytes());
ul_queue.write(std::move(sdu));
} else {
uint8_t* msg_ptr = sdu->msg;
uint32_t nof_bytes = sdu->N_bytes;
srslte::expected<bool, unique_byte_buffer_t> ret = ul_queue.try_write(std::move(sdu));
uint8_t* msg_ptr = sdu->msg;
uint32_t nof_bytes = sdu->N_bytes;
srslte::error_type<unique_byte_buffer_t> ret = ul_queue.try_write(std::move(sdu));
if (ret) {
log->info_hex(msg_ptr,
nof_bytes,

View File

@ -35,8 +35,7 @@ rlc_um_base::rlc_um_base(srslte::log_ref log_,
rrc(rrc_),
timers(timers_),
pool(byte_buffer_pool::get_instance())
{
}
{}
rlc_um_base::~rlc_um_base() {}
@ -194,8 +193,7 @@ rlc_um_base::rlc_um_base_rx::rlc_um_base_rx(rlc_um_base* parent_) :
cfg(parent_->cfg),
metrics(parent_->metrics),
lcid(parent_->lcid)
{
}
{}
rlc_um_base::rlc_um_base_rx::~rlc_um_base_rx() {}
@ -254,9 +252,9 @@ void rlc_um_base::rlc_um_base_tx::write_sdu(unique_byte_buffer_t sdu)
void rlc_um_base::rlc_um_base_tx::try_write_sdu(unique_byte_buffer_t sdu)
{
if (sdu) {
uint8_t* msg_ptr = sdu->msg;
uint32_t nof_bytes = sdu->N_bytes;
srslte::expected<bool, unique_byte_buffer_t> ret = tx_sdu_queue.try_write(std::move(sdu));
uint8_t* msg_ptr = sdu->msg;
uint32_t nof_bytes = sdu->N_bytes;
srslte::error_type<unique_byte_buffer_t> ret = tx_sdu_queue.try_write(std::move(sdu));
if (ret) {
log->info_hex(
msg_ptr, nof_bytes, "%s Tx SDU (%d B, tx_sdu_queue_len=%d)", rb_name.c_str(), nof_bytes, tx_sdu_queue.size());