diff options
author | zakarum <zakarum@yandex-team.ru> | 2022-02-10 16:49:34 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:34 +0300 |
commit | 0041d99876ae3dccc3f0fa8787131d85ddfd486b (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /util | |
parent | dbfeb17da4d5c610309076cffa8aeb7ac1da411c (diff) | |
download | ydb-0041d99876ae3dccc3f0fa8787131d85ddfd486b.tar.gz |
Restoring authorship annotation for <zakarum@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'util')
-rw-r--r-- | util/generic/maybe.h | 86 | ||||
-rw-r--r-- | util/generic/maybe_ut.cpp | 274 |
2 files changed, 180 insertions, 180 deletions
diff --git a/util/generic/maybe.h b/util/generic/maybe.h index 28eeca88c6..34d21aebcd 100644 --- a/util/generic/maybe.h +++ b/util/generic/maybe.h @@ -49,7 +49,7 @@ private: static_assert(std::is_destructible<T>::value, "Instantiation of TMaybe with non-destructible type is ill-formed"); - template <class U> + template <class U> struct TConstructibleFromMaybeSomehow { public: static constexpr bool value = std::is_constructible<T, TMaybe<U, Policy>&>::value || @@ -61,8 +61,8 @@ private: std::is_convertible<TMaybe<U, Policy>&&, T>::value || std::is_convertible<const TMaybe<U, Policy>&&, T>::value; }; - - template <class U> + + template <class U> struct TAssignableFromMaybeSomehow { public: static constexpr bool value = TConstructibleFromMaybeSomehow<U>::value || @@ -71,7 +71,7 @@ private: std::is_assignable<T&, TMaybe<U, Policy>&&>::value || std::is_assignable<T&, const TMaybe<U, Policy>&&>::value; }; - + template <class U> struct TImplicitCopyCtor { public: @@ -180,35 +180,35 @@ public: template <class U, class = std::enable_if_t<TImplicitCopyCtor<U>::value>> TMaybe(const TMaybe<U, Policy>& right) { if (right.Defined()) { - new (Data()) T(right.GetRef()); + new (Data()) T(right.GetRef()); this->Defined_ = true; - } - } - + } + } + template <class U, std::enable_if_t<TExplicitCopyCtor<U>::value, bool> = false> explicit TMaybe(const TMaybe<U, Policy>& right) { if (right.Defined()) { - new (Data()) T(right.GetRef()); + new (Data()) T(right.GetRef()); this->Defined_ = true; - } - } - + } + } + template <class U, class = std::enable_if_t<TImplicitMoveCtor<U>::value>> TMaybe(TMaybe<U, Policy>&& right) noexcept(std::is_nothrow_constructible<T, U&&>::value) { if (right.Defined()) { - new (Data()) T(std::move(right.GetRef())); + new (Data()) T(std::move(right.GetRef())); this->Defined_ = true; - } - } - + } + } + template <class U, std::enable_if_t<TExplicitMoveCtor<U>::value, bool> = false> explicit TMaybe(TMaybe<U, Policy>&& right) noexcept(std::is_nothrow_constructible<T, U&&>::value) { if (right.Defined()) { - new (Data()) T(std::move(right.GetRef())); + new (Data()) T(std::move(right.GetRef())); this->Defined_ = true; - } - } - + } + } + template <class U = T, class = std::enable_if_t<TImplicitAnyCtor<U>::value>> constexpr TMaybe(U&& right) : TBase(TInPlace{}, std::forward<U>(right)) @@ -246,37 +246,37 @@ public: TMaybe&> operator=(const TMaybe<U, Policy>& right) { if (right.Defined()) { - if (Defined()) { + if (Defined()) { *Data() = right.GetRef(); - } else { - Init(right.GetRef()); - } - } else { - Clear(); - } - - return *this; - } - + } else { + Init(right.GetRef()); + } + } else { + Clear(); + } + + return *this; + } + template <class U> std::enable_if_t<TMoveAssignable<U>::value, TMaybe&> operator=(TMaybe<U, Policy>&& right) noexcept( std::is_nothrow_assignable<T&, U&&>::value&& std::is_nothrow_constructible<T, U&&>::value) { - if (right.Defined()) { - if (Defined()) { + if (right.Defined()) { + if (Defined()) { *Data() = std::move(right.GetRef()); - } else { - Init(std::move(right.GetRef())); - } - } else { - Clear(); - } - - return *this; - } - + } else { + Init(std::move(right.GetRef())); + } + } else { + Clear(); + } + + return *this; + } + template <typename... Args> T& ConstructInPlace(Args&&... args) { Clear(); diff --git a/util/generic/maybe_ut.cpp b/util/generic/maybe_ut.cpp index d1abd3eafe..2c1a425c5e 100644 --- a/util/generic/maybe_ut.cpp +++ b/util/generic/maybe_ut.cpp @@ -808,162 +808,162 @@ Y_UNIT_TEST_SUITE(TMaybeTest) { output << TMaybe<int>(42); UNIT_ASSERT_EQUAL("42", s); } - + Y_UNIT_TEST(TestMaybeCovarianceImplicit) { - struct TestStruct { - TestStruct(int value) - : Value_(value) + struct TestStruct { + TestStruct(int value) + : Value_(value) { } - - operator int() const { - return Value_; - } - - static TMaybe<int> Unwrap(TMaybe<TestStruct> testStructMaybe) { - return testStructMaybe; - } - - int Value_; - }; - - TMaybe<int> testMaybeFull = TestStruct::Unwrap(TMaybe<int>(42)); - UNIT_ASSERT(testMaybeFull.Defined()); - UNIT_ASSERT_EQUAL(testMaybeFull.GetRef(), 42); - - TMaybe<int> testMaybeEmpty = TestStruct::Unwrap(TMaybe<int>()); - UNIT_ASSERT(!testMaybeEmpty.Defined()); - } - + + operator int() const { + return Value_; + } + + static TMaybe<int> Unwrap(TMaybe<TestStruct> testStructMaybe) { + return testStructMaybe; + } + + int Value_; + }; + + TMaybe<int> testMaybeFull = TestStruct::Unwrap(TMaybe<int>(42)); + UNIT_ASSERT(testMaybeFull.Defined()); + UNIT_ASSERT_EQUAL(testMaybeFull.GetRef(), 42); + + TMaybe<int> testMaybeEmpty = TestStruct::Unwrap(TMaybe<int>()); + UNIT_ASSERT(!testMaybeEmpty.Defined()); + } + Y_UNIT_TEST(TestMaybeCovarianceExplicit) { - struct TestStruct { - explicit TestStruct(int value) - : Value_(value) + struct TestStruct { + explicit TestStruct(int value) + : Value_(value) { } - int Value_; - }; - - TMaybe<TestStruct> testStructMaybeFull(TMaybe<int>(42)); - UNIT_ASSERT(testStructMaybeFull.Defined()); - UNIT_ASSERT_EQUAL(testStructMaybeFull.GetRef().Value_, 42); - - TMaybe<int> empty; - TMaybe<TestStruct> testStructMaybeEmpty(empty); - UNIT_ASSERT(!testStructMaybeEmpty.Defined()); - } - + int Value_; + }; + + TMaybe<TestStruct> testStructMaybeFull(TMaybe<int>(42)); + UNIT_ASSERT(testStructMaybeFull.Defined()); + UNIT_ASSERT_EQUAL(testStructMaybeFull.GetRef().Value_, 42); + + TMaybe<int> empty; + TMaybe<TestStruct> testStructMaybeEmpty(empty); + UNIT_ASSERT(!testStructMaybeEmpty.Defined()); + } + Y_UNIT_TEST(TestMaybeCovarianceAssign) { - struct TestStruct { - explicit TestStruct(int value) - : Value_(value) + struct TestStruct { + explicit TestStruct(int value) + : Value_(value) { } - TestStruct& operator=(int value) { - Value_ = value; - return *this; - } - int Value_; - }; - - TMaybe<TestStruct> testStructMaybe(Nothing()); - UNIT_ASSERT(!testStructMaybe.Defined()); - - testStructMaybe = TMaybe<int>(42); - UNIT_ASSERT(testStructMaybe.Defined()); - UNIT_ASSERT_EQUAL(testStructMaybe.GetRef().Value_, 42); - - testStructMaybe = TMaybe<int>(23); - UNIT_ASSERT(testStructMaybe.Defined()); - UNIT_ASSERT_EQUAL(testStructMaybe.GetRef().Value_, 23); - - testStructMaybe = TMaybe<int>(); - UNIT_ASSERT(!testStructMaybe.Defined()); - } - + TestStruct& operator=(int value) { + Value_ = value; + return *this; + } + int Value_; + }; + + TMaybe<TestStruct> testStructMaybe(Nothing()); + UNIT_ASSERT(!testStructMaybe.Defined()); + + testStructMaybe = TMaybe<int>(42); + UNIT_ASSERT(testStructMaybe.Defined()); + UNIT_ASSERT_EQUAL(testStructMaybe.GetRef().Value_, 42); + + testStructMaybe = TMaybe<int>(23); + UNIT_ASSERT(testStructMaybe.Defined()); + UNIT_ASSERT_EQUAL(testStructMaybe.GetRef().Value_, 23); + + testStructMaybe = TMaybe<int>(); + UNIT_ASSERT(!testStructMaybe.Defined()); + } + Y_UNIT_TEST(TestMaybeCovarianceNonTrivial) { - struct TestStruct { - enum { - FromValue, - FromMaybe, - }; - TestStruct(int value) - : Value_(value) - , From_(FromValue) + struct TestStruct { + enum { + FromValue, + FromMaybe, + }; + TestStruct(int value) + : Value_(value) + , From_(FromValue) { } - TestStruct(TMaybe<int> value) - : Value_(value.Defined() ? value.GetRef() : 0) - , From_(FromMaybe) + TestStruct(TMaybe<int> value) + : Value_(value.Defined() ? value.GetRef() : 0) + , From_(FromMaybe) { } - int Value_; - int From_; - }; - - TMaybe<TestStruct> testStructFromMaybe(TMaybe<int>(42)); - UNIT_ASSERT(testStructFromMaybe.Defined()); - UNIT_ASSERT_EQUAL(testStructFromMaybe.GetRef().From_, TestStruct::FromMaybe); - UNIT_ASSERT_EQUAL(testStructFromMaybe.GetRef().Value_, 42); - - TMaybe<int> empty; - TMaybe<TestStruct> testStructFromEmptyMaybe(empty); - UNIT_ASSERT(testStructFromEmptyMaybe.Defined()); - UNIT_ASSERT_EQUAL(testStructFromEmptyMaybe.GetRef().From_, TestStruct::FromMaybe); - UNIT_ASSERT_EQUAL(testStructFromEmptyMaybe.GetRef().Value_, 0); - - TMaybe<TestStruct> testStructFromValue(23); - UNIT_ASSERT(testStructFromValue.Defined()); - UNIT_ASSERT_EQUAL(testStructFromValue.GetRef().From_, TestStruct::FromValue); - UNIT_ASSERT_EQUAL(testStructFromValue.GetRef().Value_, 23); - } - + int Value_; + int From_; + }; + + TMaybe<TestStruct> testStructFromMaybe(TMaybe<int>(42)); + UNIT_ASSERT(testStructFromMaybe.Defined()); + UNIT_ASSERT_EQUAL(testStructFromMaybe.GetRef().From_, TestStruct::FromMaybe); + UNIT_ASSERT_EQUAL(testStructFromMaybe.GetRef().Value_, 42); + + TMaybe<int> empty; + TMaybe<TestStruct> testStructFromEmptyMaybe(empty); + UNIT_ASSERT(testStructFromEmptyMaybe.Defined()); + UNIT_ASSERT_EQUAL(testStructFromEmptyMaybe.GetRef().From_, TestStruct::FromMaybe); + UNIT_ASSERT_EQUAL(testStructFromEmptyMaybe.GetRef().Value_, 0); + + TMaybe<TestStruct> testStructFromValue(23); + UNIT_ASSERT(testStructFromValue.Defined()); + UNIT_ASSERT_EQUAL(testStructFromValue.GetRef().From_, TestStruct::FromValue); + UNIT_ASSERT_EQUAL(testStructFromValue.GetRef().Value_, 23); + } + Y_UNIT_TEST(TestMaybeCovarianceNonTrivialAssign) { - struct TestStruct { - enum { - FromValue, - FromMaybe, - }; - TestStruct(int value) - : Value_(value) - , From_(FromValue) + struct TestStruct { + enum { + FromValue, + FromMaybe, + }; + TestStruct(int value) + : Value_(value) + , From_(FromValue) { } - TestStruct(TMaybe<int> value) - : Value_(value.Defined() ? value.GetRef() : 0) - , From_(FromMaybe) + TestStruct(TMaybe<int> value) + : Value_(value.Defined() ? value.GetRef() : 0) + , From_(FromMaybe) { } - TestStruct& operator=(int value) { - Value_ = value; - From_ = FromValue; - return *this; - } - TestStruct& operator=(TMaybe<int> value) { - Value_ = value.Defined() ? value.GetRef() : 0; - From_ = FromMaybe; - return *this; - } - int Value_; - int From_; - }; - - TMaybe<TestStruct> testStructMaybe(Nothing()); - testStructMaybe = TMaybe<int>(42); - UNIT_ASSERT(testStructMaybe.Defined()); - UNIT_ASSERT_EQUAL(testStructMaybe.GetRef().From_, TestStruct::FromMaybe); - UNIT_ASSERT_EQUAL(testStructMaybe.GetRef().Value_, 42); - - testStructMaybe = TMaybe<int>(); - UNIT_ASSERT(testStructMaybe.Defined()); - UNIT_ASSERT_EQUAL(testStructMaybe.GetRef().From_, TestStruct::FromMaybe); - UNIT_ASSERT_EQUAL(testStructMaybe.GetRef().Value_, 0); - - testStructMaybe = 23; - UNIT_ASSERT(testStructMaybe.Defined()); - UNIT_ASSERT_EQUAL(testStructMaybe.GetRef().From_, TestStruct::FromValue); - UNIT_ASSERT_EQUAL(testStructMaybe.GetRef().Value_, 23); - } + TestStruct& operator=(int value) { + Value_ = value; + From_ = FromValue; + return *this; + } + TestStruct& operator=(TMaybe<int> value) { + Value_ = value.Defined() ? value.GetRef() : 0; + From_ = FromMaybe; + return *this; + } + int Value_; + int From_; + }; + + TMaybe<TestStruct> testStructMaybe(Nothing()); + testStructMaybe = TMaybe<int>(42); + UNIT_ASSERT(testStructMaybe.Defined()); + UNIT_ASSERT_EQUAL(testStructMaybe.GetRef().From_, TestStruct::FromMaybe); + UNIT_ASSERT_EQUAL(testStructMaybe.GetRef().Value_, 42); + + testStructMaybe = TMaybe<int>(); + UNIT_ASSERT(testStructMaybe.Defined()); + UNIT_ASSERT_EQUAL(testStructMaybe.GetRef().From_, TestStruct::FromMaybe); + UNIT_ASSERT_EQUAL(testStructMaybe.GetRef().Value_, 0); + + testStructMaybe = 23; + UNIT_ASSERT(testStructMaybe.Defined()); + UNIT_ASSERT_EQUAL(testStructMaybe.GetRef().From_, TestStruct::FromValue); + UNIT_ASSERT_EQUAL(testStructMaybe.GetRef().Value_, 23); + } Y_UNIT_TEST(TestMaybeConvertion) { struct TSrc {}; |