Skip to content

Commit

Permalink
iox-eclipse-iceoryx#1750 Add 'Expects' call to 'expected::value()' an…
Browse files Browse the repository at this point in the history
…d 'has_error'

Signed-off-by: Simon Hoinkis <[email protected]>
  • Loading branch information
mossmaurice committed Oct 19, 2022
1 parent 8df1f7e commit 2c64a99
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 18 deletions.
4 changes: 3 additions & 1 deletion iceoryx_hoofs/include/iceoryx_hoofs/cxx/expected.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ class IOX_NO_DISCARD expected<ErrorType> : public FunctionalInterface<expected<E
ErrorType&& get_error() && noexcept;

private:
const ErrorType& get_error_unchecked() const noexcept;
explicit expected(variant<ErrorType>&& store) noexcept;
variant<ErrorType> m_store;
static constexpr uint64_t ERROR_INDEX = 0U;
Expand Down Expand Up @@ -449,6 +450,8 @@ class IOX_NO_DISCARD expected<ValueType, ErrorType>

private:
explicit expected(variant<ValueType, ErrorType>&& store) noexcept;
const ErrorType& get_error_unchecked() const noexcept;
const ValueType& value_unchecked() const noexcept;
variant<ValueType, ErrorType> m_store;
static constexpr uint64_t VALUE_INDEX = 0U;
static constexpr uint64_t ERROR_INDEX = 1U;
Expand All @@ -461,7 +464,6 @@ class IOX_NO_DISCARD expected<void, ErrorType> : public expected<ErrorType>
using expected<ErrorType>::expected;
};


} // namespace cxx
} // namespace iox

Expand Down
61 changes: 44 additions & 17 deletions iceoryx_hoofs/include/iceoryx_hoofs/internal/cxx/expected.inl
Original file line number Diff line number Diff line change
Expand Up @@ -156,43 +156,55 @@ inline bool expected<ValueType, ErrorType>::has_error() const noexcept
template <typename ValueType, typename ErrorType>
inline ErrorType&& expected<ValueType, ErrorType>::get_error() && noexcept
{
return std::move(*m_store.template get_at_index<ERROR_INDEX>());
return std::move(get_error());
}

template <typename ValueType, typename ErrorType>
inline ErrorType& expected<ValueType, ErrorType>::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<ErrorType&>(const_cast<const expected<ValueType, ErrorType>*>(this)->get_error());
}

template <typename ValueType, typename ErrorType>
inline const ErrorType& expected<ValueType, ErrorType>::get_error() const& noexcept
{
Expects(has_error());
return get_error_unchecked();
}

template <typename ValueType, typename ErrorType>
inline const ErrorType& expected<ValueType, ErrorType>::get_error_unchecked() const noexcept
{
return *m_store.template get_at_index<ERROR_INDEX>();
}

template <typename ValueType, typename ErrorType>
inline ValueType&& expected<ValueType, ErrorType>::value() && noexcept
{
return std::move(*m_store.template get_at_index<VALUE_INDEX>());
return std::move(value());
}

template <typename ValueType, typename ErrorType>
inline const ValueType& expected<ValueType, ErrorType>::value() const& noexcept
{
return *m_store.template get_at_index<VALUE_INDEX>();
Expects(!has_error());
return value_unchecked();
}

template <typename ValueType, typename ErrorType>
inline ValueType& expected<ValueType, ErrorType>::value() & noexcept
{
return *m_store.template get_at_index<VALUE_INDEX>();
}

template <typename ErrorType>
inline const ErrorType& expected<ErrorType>::get_error() const& noexcept
{
return *m_store.template get_at_index<ERROR_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<ValueType&>(const_cast<const expected<ValueType, ErrorType>*>(this)->value());
}

template <typename ValueType, typename ErrorType>
inline ValueType* expected<ValueType, ErrorType>::operator->() noexcept
{
return m_store.template get_at_index<VALUE_INDEX>();
return &value();
}

template <typename ValueType, typename ErrorType>
Expand All @@ -206,7 +218,7 @@ inline const ValueType* expected<ValueType, ErrorType>::operator->() const noexc
template <typename ValueType, typename ErrorType>
inline ValueType& expected<ValueType, ErrorType>::operator*() noexcept
{
return *m_store.template get_at_index<VALUE_INDEX>();
return value();
}

template <typename ValueType, typename ErrorType>
Expand All @@ -217,6 +229,12 @@ inline const ValueType& expected<ValueType, ErrorType>::operator*() const noexce
return const_cast<const ValueType&>(const_cast<expected*>(this)->operator*());
}

template <typename ValueType, typename ErrorType>
const ValueType& expected<ValueType, ErrorType>::value_unchecked() const noexcept
{
return *m_store.template get_at_index<VALUE_INDEX>();
}

template <typename ValueType, typename ErrorType>
template <typename T>
inline expected<ValueType, ErrorType>::operator expected<T>() const noexcept
Expand Down Expand Up @@ -359,17 +377,26 @@ inline bool expected<ErrorType>::has_error() const noexcept
template <typename ErrorType>
inline ErrorType&& expected<ErrorType>::get_error() && noexcept
{
return std::move(*m_store.template get_at_index<ERROR_INDEX>());
return std::move(get_error());
}

template <typename ValueType, typename ErrorType>
inline const ErrorType& expected<ValueType, ErrorType>::get_error() const& noexcept
template <typename ErrorType>
inline ErrorType& expected<ErrorType>::get_error() & noexcept
{
return *m_store.template get_at_index<ERROR_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<ErrorType&>(const_cast<const expected<ErrorType>*>(this)->get_error());
}

template <typename ErrorType>
inline ErrorType& expected<ErrorType>::get_error() & noexcept
inline const ErrorType& expected<ErrorType>::get_error() const& noexcept
{
Expects(has_error());
return get_error_unchecked();
}

template <typename ErrorType>
inline const ErrorType& expected<ErrorType>::get_error_unchecked() const noexcept
{
return *m_store.template get_at_index<ERROR_INDEX>();
}
Expand Down

0 comments on commit 2c64a99

Please sign in to comment.