Skip to content

Commit

Permalink
iox-eclipse-iceoryx#542 Fix Windows build by reintroducing const over…
Browse files Browse the repository at this point in the history
…load for get_error()

Signed-off-by: Simon Hoinkis <simon.hoinkis@apex.ai>
  • Loading branch information
mossmaurice authored and marthtz committed May 12, 2021
1 parent 36cb8a1 commit c3025aa
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
10 changes: 10 additions & 0 deletions iceoryx_utils/include/iceoryx_utils/cxx/expected.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ class expected<ErrorType>
/// @return reference to the internally contained error
ErrorType& get_error() & noexcept;

/// @brief returns a const reference to the contained error value, if the expected
/// does not contain an error this is undefined behavior
/// @return const reference to the internally contained error
const ErrorType& get_error() const& noexcept;

/// @brief returns a rvalue reference to the contained error value, if the expected
/// does not contain an error this is undefined behavior
/// @return rvalue reference to the internally contained error
Expand Down Expand Up @@ -392,6 +397,11 @@ class expected<ValueType, ErrorType>
/// @return reference to the internally contained error
ErrorType& get_error() & noexcept;

/// @brief returns a const reference to the contained error value, if the expected
/// does not contain an error this is undefined behavior
/// @return const reference to the internally contained error
const ErrorType& get_error() const& noexcept;

/// @brief returns a rvalue reference to the contained error value, if the expected
/// does not contain an error this is undefined behavior
/// @return rvalue reference to the internally contained error
Expand Down
12 changes: 12 additions & 0 deletions iceoryx_utils/include/iceoryx_utils/internal/cxx/expected.inl
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ template <typename ValueType, typename ErrorType>
return *m_store.template get_at_index<0>();
}

template <typename ErrorType>
inline const ErrorType& expected<ErrorType>::get_error() const& noexcept
{
return *m_store.template get_at_index<0>();
}

template <typename ValueType, typename ErrorType>
inline ValueType expected<ValueType, ErrorType>::value_or(const ValueType& value) noexcept
{
Expand Down Expand Up @@ -466,6 +472,12 @@ template <typename ErrorType>
return std::move(*m_store.template get_at_index<0>());
}

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

template <typename ErrorType>
inline ErrorType& expected<ErrorType>::get_error() & noexcept
{
Expand Down
13 changes: 13 additions & 0 deletions iceoryx_utils/test/moduletests/test_cxx_expected.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ TEST_F(expected_test, CreateWithErrorResultsInError)
EXPECT_THAT(sut.get_error(), Eq(TestError::ERROR1));
}

TEST_F(expected_test, ErrorTypeOnlyConstCreateWithErrorResultsInError)
{
const auto sut = expected<TestError>::create_error(TestError::ERROR2);
ASSERT_THAT(sut.has_error(), Eq(true));
ASSERT_THAT(sut.get_error(), Eq(TestError::ERROR2));
}

TEST_F(expected_test, ErrorTypeOnlyCreateWithErrorResultsInError)
{
auto sut = expected<TestError>::create_error(TestError::ERROR1);
Expand Down Expand Up @@ -151,6 +158,12 @@ TEST_F(expected_test, CreateRValueAndGetErrorResultsInCorrectError)
EXPECT_THAT(sut.m_b, Eq(121));
}

TEST_F(expected_test, ConstCreateLValueAndGetErrorResultsInCorrectError)
{
const auto& sut = expected<int, TestClass>::create_error(343, 232);
EXPECT_THAT(sut.get_error().m_b, Eq(232));
}

TEST_F(expected_test, CreateWithValueAndMoveCtorLeadsToInvalidState)
{
auto sut = expected<int, TestClass>::create_value(177);
Expand Down

0 comments on commit c3025aa

Please sign in to comment.