diff --git a/lib/include/srslte/common/block_queue.h b/lib/include/srslte/common/block_queue.h index ae91beb13..ab14d5079 100644 --- a/lib/include/srslte/common/block_queue.h +++ b/lib/include/srslte/common/block_queue.h @@ -94,7 +94,7 @@ public: bool try_push(const myobj& value) { return push_(value, false); } - srslte::expected try_push(myobj&& value) { return push_(std::move(value), false); } + srslte::error_type 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 push_(myobj&& value, bool block) + srslte::error_type 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); diff --git a/lib/include/srslte/common/expected.h b/lib/include/srslte/common/expected.h index b2bb28eba..6c7cee2e5 100644 --- a/lib/include/srslte/common/expected.h +++ b/lib/include/srslte/common/expected.h @@ -22,11 +22,26 @@ #ifndef SRSLTE_EXPECTED_H #define SRSLTE_EXPECTED_H -#include "type_utils.h" #include +#include 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 @@ -103,64 +118,38 @@ public: unexpected = std::forward(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 +using error_type = expected; + } // namespace srslte #endif // SRSLTE_EXPECTED_H diff --git a/lib/include/srslte/common/type_utils.h b/lib/include/srslte/common/type_utils.h index 12cc9d97c..ea158d3d4 100644 --- a/lib/include/srslte/common/type_utils.h +++ b/lib/include/srslte/common/type_utils.h @@ -22,6 +22,7 @@ #ifndef SRSLTE_TYPE_UTILS_H #define SRSLTE_TYPE_UTILS_H +#include "expected.h" #include #include #include @@ -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 ***********************************/ diff --git a/lib/include/srslte/upper/rlc_tx_queue.h b/lib/include/srslte/upper/rlc_tx_queue.h index e6d9875f4..515c94311 100644 --- a/lib/include/srslte/upper/rlc_tx_queue.h +++ b/lib/include/srslte/upper/rlc_tx_queue.h @@ -56,7 +56,7 @@ public: } void write(unique_byte_buffer_t msg) { queue.push(std::move(msg)); } - srslte::expected try_write(unique_byte_buffer_t&& msg) + srslte::error_type try_write(unique_byte_buffer_t&& msg) { return queue.try_push(std::move(msg)); } diff --git a/lib/src/upper/rlc_am_lte.cc b/lib/src/upper/rlc_am_lte.cc index 3bd44ff56..7097afbd9 100644 --- a/lib/src/upper/rlc_am_lte.cc +++ b/lib/src/upper/rlc_am_lte.cc @@ -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 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 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()); diff --git a/lib/src/upper/rlc_tm.cc b/lib/src/upper/rlc_tm.cc index e67ebe2e7..b711f64f2 100644 --- a/lib/src/upper/rlc_tm.cc +++ b/lib/src/upper/rlc_tm.cc @@ -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 ret = ul_queue.try_write(std::move(sdu)); + uint8_t* msg_ptr = sdu->msg; + uint32_t nof_bytes = sdu->N_bytes; + srslte::error_type ret = ul_queue.try_write(std::move(sdu)); if (ret) { log->info_hex(msg_ptr, nof_bytes, diff --git a/lib/src/upper/rlc_um_base.cc b/lib/src/upper/rlc_um_base.cc index 22fcefe93..5f7748ace 100644 --- a/lib/src/upper/rlc_um_base.cc +++ b/lib/src/upper/rlc_um_base.cc @@ -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 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 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());