Skip to content

Commit

Permalink
iox-eclipse-iceoryx#1969 Add 'in_place_t' ctors for 'expected'
Browse files Browse the repository at this point in the history
  • Loading branch information
elBoberido committed Apr 18, 2023
1 parent e7b572b commit 73eda66
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
16 changes: 16 additions & 0 deletions iceoryx_hoofs/test/moduletests/test_vocabulary_expected.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,22 @@ TEST_F(expected_test, ErrorTypeOnlyMoveAssignmentLeadsToMovedSource)
EXPECT_EQ(sutDestination.get_error().m_b, B);
}

TEST_F(expected_test, CreateFromInPlaceTypeLeadsToValidErrorOnlySut)
{
::testing::Test::RecordProperty("TEST_ID", "91a8ad7f-4843-4bd9-a56b-0561ae6b56cb");
expected<TestError> sut{in_place};
ASSERT_THAT(sut.has_error(), Eq(false));
}

TEST_F(expected_test, CreateFromInPlaceTypeLeadsToValidSut)
{
::testing::Test::RecordProperty("TEST_ID", "3a527c62-aaea-44ae-9b99-027c19d032b5");
constexpr int VALUE = 42;
expected<int, TestError> sut{in_place, VALUE};
ASSERT_THAT(sut.has_error(), Eq(false));
EXPECT_THAT(sut.value(), Eq(VALUE));
}

TEST_F(expected_test, CreateFromEmptySuccessTypeLeadsToValidSut)
{
::testing::Test::RecordProperty("TEST_ID", "0204f08f-fb6d-45bb-aac7-fd14152ab1bf");
Expand Down
13 changes: 13 additions & 0 deletions iceoryx_hoofs/vocabulary/include/iox/detail/expected.inl
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ inline expected<ValueType, ErrorType>::expected(expected<ValueType, ErrorType>&&
{
}

template <typename ValueType, typename ErrorType>
template <typename... Targs>
inline expected<ValueType, ErrorType>::expected(in_place_t, Targs&&... args) noexcept
: m_store(in_place_index<VALUE_INDEX>(), std::forward<Targs>(args)...)
{
}

template <typename ValueType, typename ErrorType>
inline expected<ValueType, ErrorType>&
expected<ValueType, ErrorType>::operator=(expected<ValueType, ErrorType>&& rhs) noexcept
Expand Down Expand Up @@ -264,6 +271,11 @@ inline expected<ErrorType>::expected(const success<void>) noexcept
{
}

template <typename ErrorType>
inline expected<ErrorType>::expected(in_place_t) noexcept
{
}

template <typename ErrorType>
inline expected<ErrorType>::expected(expected<ErrorType>&& rhs) noexcept
: m_store(std::move(rhs.m_store))
Expand Down Expand Up @@ -292,6 +304,7 @@ inline expected<ErrorType>::expected(error<ErrorType>&& errorValue) noexcept
: m_store(in_place_index<ERROR_INDEX>(), std::move(errorValue.value))
{
}

// AXIVION DISABLE STYLE AutosarC++19_03-A16.0.1: Required for Windows due to MSVC deficiencies
#if defined(_WIN32)
template <typename ErrorType>
Expand Down
12 changes: 12 additions & 0 deletions iceoryx_hoofs/vocabulary/include/iox/expected.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ class IOX_NO_DISCARD expected<ErrorType> final : public FunctionalInterface<expe
// NOLINTNEXTLINE(hicpp-explicit-conversions)
expected(const success<void>) noexcept;

/// @brief Creates an expected which is signaling success. This mirrors the c'tor for the 'expected' with a
/// 'ValueType'.
/// @param[in] in_place_t compile time variable to distinguish between constructors with certain behavior
explicit expected(in_place_t) noexcept;

/// @brief constructs an expected which is signaling an error and stores the
/// error value provided by errorValue
/// @param[in] errorValue error value which will be stored in the expected
Expand Down Expand Up @@ -277,6 +282,13 @@ class IOX_NO_DISCARD expected<ValueType, ErrorType> final
/// ValueType or ErrorType to correctly invalidate the stored object
expected(expected&& rhs) noexcept;

/// @brief Creates an expected which is signaling success and perfectly forwards
/// the args to the constructor of ValueType
/// @param[in] in_place_t compile time variable to distinguish between constructors with certain behavior
/// @param[in] args... arguments which will be forwarded to the 'ValueType' constructor
template <typename... Targs>
explicit expected(in_place_t, Targs&&... args) noexcept;

/// @brief calls the destructor of the success value or error value - depending on what
/// is stored in the expected
~expected() noexcept = default;
Expand Down

0 comments on commit 73eda66

Please sign in to comment.