Skip to content

Commit

Permalink
Merge pull request #1970 from ApexAI/iox-1969-rework-constructors-for…
Browse files Browse the repository at this point in the history
…-expected

iox-#1969 rework constructors for expected
  • Loading branch information
elBoberido authored Apr 18, 2023
2 parents 0cdf9ed + 18adcee commit e29d6c6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
18 changes: 17 additions & 1 deletion iceoryx_hoofs/test/moduletests/test_vocabulary_expected.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2021 - 2023 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down 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
15 changes: 14 additions & 1 deletion iceoryx_hoofs/vocabulary/include/iox/detail/expected.inl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2021 - 2023 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down 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
15 changes: 14 additions & 1 deletion iceoryx_hoofs/vocabulary/include/iox/expected.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2019 - 2020 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2020 - 2022 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2020 - 2023 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down 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,14 @@ 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
/// @tparam Targs is the template parameter pack for the perfectly forwarded arguments
/// @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 e29d6c6

Please sign in to comment.