created error_type and cleaned up expected

master
Francisco Paisana 5 years ago committed by Francisco Paisana
parent 9a7f48b6a5
commit 791d84e639

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

@ -22,11 +22,26 @@
#ifndef SRSLTE_EXPECTED_H #ifndef SRSLTE_EXPECTED_H
#define SRSLTE_EXPECTED_H #define SRSLTE_EXPECTED_H
#include "type_utils.h"
#include <memory> #include <memory>
#include <system_error>
namespace srslte { 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 {}; struct default_error_t {};
template <typename T, typename E = default_error_t> template <typename T, typename E = default_error_t>
@ -103,64 +118,38 @@ public:
unexpected = std::forward<U>(other); 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; } 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) { if (not has_val) {
THROW_BAD_ACCESS("Bad expected value access"); THROW_BAD_ACCESS("Bad expected value access");
} }
return val; return val;
} }
T& value() & T& value()
{ {
if (not has_val) { if (not has_val) {
THROW_BAD_ACCESS("Bad expected value access"); THROW_BAD_ACCESS("Bad expected value access");
} }
return val; return val;
} }
T&& value() && const E& error() const
{
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) { if (has_val) {
THROW_BAD_ACCESS("Bad expected error access"); THROW_BAD_ACCESS("Bad expected error access");
} }
return unexpected; return unexpected;
} }
E& error() & E& error()
{ {
if (has_val) { if (has_val) {
THROW_BAD_ACCESS("Bad expected error access"); THROW_BAD_ACCESS("Bad expected error access");
} }
return unexpected; 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
{ {
@ -218,6 +207,11 @@ private:
}; };
}; };
struct success_t {};
template <typename E>
using error_type = expected<success_t, E>;
} // namespace srslte } // namespace srslte
#endif // SRSLTE_EXPECTED_H #endif // SRSLTE_EXPECTED_H

@ -22,6 +22,7 @@
#ifndef SRSLTE_TYPE_UTILS_H #ifndef SRSLTE_TYPE_UTILS_H
#define SRSLTE_TYPE_UTILS_H #define SRSLTE_TYPE_UTILS_H
#include "expected.h"
#include <cstring> #include <cstring>
#include <limits> #include <limits>
#include <memory> #include <memory>
@ -30,21 +31,6 @@
namespace srslte { 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 * get_type_name methods
***********************************/ ***********************************/

@ -56,7 +56,7 @@ public:
} }
void write(unique_byte_buffer_t msg) { queue.push(std::move(msg)); } 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)); return queue.try_push(std::move(msg));
} }

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

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

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

Loading…
Cancel
Save