diff --git a/iceoryx_hoofs/include/iceoryx_hoofs/cxx/expected.hpp b/iceoryx_hoofs/include/iceoryx_hoofs/cxx/expected.hpp index 7c2038a9d4c..cc660e54f8e 100644 --- a/iceoryx_hoofs/include/iceoryx_hoofs/cxx/expected.hpp +++ b/iceoryx_hoofs/include/iceoryx_hoofs/cxx/expected.hpp @@ -258,6 +258,7 @@ class IOX_NO_DISCARD expected : public FunctionalInterface&& store) noexcept; variant m_store; static constexpr uint64_t ERROR_INDEX = 0U; @@ -449,6 +450,8 @@ class IOX_NO_DISCARD expected private: explicit expected(variant&& store) noexcept; + const ErrorType& get_error_unchecked() const noexcept; + const ValueType& value_unchecked() const noexcept; variant m_store; static constexpr uint64_t VALUE_INDEX = 0U; static constexpr uint64_t ERROR_INDEX = 1U; @@ -461,7 +464,6 @@ class IOX_NO_DISCARD expected : public expected using expected::expected; }; - } // namespace cxx } // namespace iox diff --git a/iceoryx_hoofs/include/iceoryx_hoofs/internal/cxx/expected.inl b/iceoryx_hoofs/include/iceoryx_hoofs/internal/cxx/expected.inl index 3b0e191d1e5..42a2012f994 100644 --- a/iceoryx_hoofs/include/iceoryx_hoofs/internal/cxx/expected.inl +++ b/iceoryx_hoofs/include/iceoryx_hoofs/internal/cxx/expected.inl @@ -156,11 +156,26 @@ inline bool expected::has_error() const noexcept template inline ErrorType&& expected::get_error() && noexcept { - return std::move(*m_store.template get_at_index()); + return std::move(get_error()); } template inline ErrorType& expected::get_error() & noexcept +{ + // AXIVION Next Construct AutosarC++19_03-A5.2.3 : const cast to avoid code duplication + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) + return const_cast(const_cast*>(this)->get_error()); +} + +template +inline const ErrorType& expected::get_error() const& noexcept +{ + Expects(has_error()); + return get_error_unchecked(); +} + +template +inline const ErrorType& expected::get_error_unchecked() const noexcept { return *m_store.template get_at_index(); } @@ -168,31 +183,28 @@ inline ErrorType& expected::get_error() & noexcept template inline ValueType&& expected::value() && noexcept { - return std::move(*m_store.template get_at_index()); + return std::move(value()); } template inline const ValueType& expected::value() const& noexcept { - return *m_store.template get_at_index(); + Expects(!has_error()); + return value_unchecked(); } template inline ValueType& expected::value() & noexcept { - return *m_store.template get_at_index(); -} - -template -inline const ErrorType& expected::get_error() const& noexcept -{ - return *m_store.template get_at_index(); + // AXIVION Next Construct AutosarC++19_03-A5.2.3 : const cast to avoid code duplication + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) + return const_cast(const_cast*>(this)->value()); } template inline ValueType* expected::operator->() noexcept { - return m_store.template get_at_index(); + return &value(); } template @@ -206,7 +218,7 @@ inline const ValueType* expected::operator->() const noexc template inline ValueType& expected::operator*() noexcept { - return *m_store.template get_at_index(); + return value(); } template @@ -217,6 +229,12 @@ inline const ValueType& expected::operator*() const noexce return const_cast(const_cast(this)->operator*()); } +template +const ValueType& expected::value_unchecked() const noexcept +{ + return *m_store.template get_at_index(); +} + template template inline expected::operator expected() const noexcept @@ -359,17 +377,26 @@ inline bool expected::has_error() const noexcept template inline ErrorType&& expected::get_error() && noexcept { - return std::move(*m_store.template get_at_index()); + return std::move(get_error()); } -template -inline const ErrorType& expected::get_error() const& noexcept +template +inline ErrorType& expected::get_error() & noexcept { - return *m_store.template get_at_index(); + // AXIVION Next Construct AutosarC++19_03-A5.2.3 : const cast to avoid code duplication + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) + return const_cast(const_cast*>(this)->get_error()); } template -inline ErrorType& expected::get_error() & noexcept +inline const ErrorType& expected::get_error() const& noexcept +{ + Expects(has_error()); + return get_error_unchecked(); +} + +template +inline const ErrorType& expected::get_error_unchecked() const noexcept { return *m_store.template get_at_index(); }