aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic
diff options
context:
space:
mode:
authordiver <diver@yandex-team.ru>2022-02-10 16:48:07 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:48:07 +0300
commit7629e1f9ef7f9d2a3c345c97e6a4e5a4b32ee786 (patch)
treec3371bfa47641a52244267b63009d16e4e7054ff /util/generic
parentcc573d2716c99ba22afc98753971cd97fd5283e0 (diff)
downloadydb-7629e1f9ef7f9d2a3c345c97e6a4e5a4b32ee786.tar.gz
Restoring authorship annotation for <diver@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'util/generic')
-rw-r--r--util/generic/lazy_value.cpp2
-rw-r--r--util/generic/lazy_value.h86
-rw-r--r--util/generic/lazy_value_ut.cpp66
3 files changed, 77 insertions, 77 deletions
diff --git a/util/generic/lazy_value.cpp b/util/generic/lazy_value.cpp
index e687ec1a59c..6b6738404e7 100644
--- a/util/generic/lazy_value.cpp
+++ b/util/generic/lazy_value.cpp
@@ -1 +1 @@
-#include "lazy_value.h"
+#include "lazy_value.h"
diff --git a/util/generic/lazy_value.h b/util/generic/lazy_value.h
index 3c720f76b5a..033b46eaf23 100644
--- a/util/generic/lazy_value.h
+++ b/util/generic/lazy_value.h
@@ -1,53 +1,53 @@
-#pragma once
-
-#include "maybe.h"
+#pragma once
+
+#include "maybe.h"
#include "function.h"
-
-template <class T>
+
+template <class T>
class TLazyValueBase {
-public:
+public:
using TInitializer = std::function<T()>;
-
+
TLazyValueBase() = default;
TLazyValueBase(TInitializer initializer)
: Initializer(std::move(initializer))
- {
- }
-
- explicit operator bool() const noexcept {
- return Defined();
- }
-
- bool Defined() const noexcept {
- return ValueHolder.Defined();
- }
-
- const T& GetRef() const {
- if (!Defined()) {
- InitDefault();
- }
- return *ValueHolder;
- }
-
- const T& operator*() const {
- return GetRef();
- }
-
- const T* operator->() const {
- return &GetRef();
- }
-
- void InitDefault() const {
+ {
+ }
+
+ explicit operator bool() const noexcept {
+ return Defined();
+ }
+
+ bool Defined() const noexcept {
+ return ValueHolder.Defined();
+ }
+
+ const T& GetRef() const {
+ if (!Defined()) {
+ InitDefault();
+ }
+ return *ValueHolder;
+ }
+
+ const T& operator*() const {
+ return GetRef();
+ }
+
+ const T* operator->() const {
+ return &GetRef();
+ }
+
+ void InitDefault() const {
Y_ASSERT(Initializer);
- ValueHolder = Initializer();
- }
-
-private:
- mutable TMaybe<T> ValueHolder;
- TInitializer Initializer;
-};
-
+ ValueHolder = Initializer();
+ }
+
+private:
+ mutable TMaybe<T> ValueHolder;
+ TInitializer Initializer;
+};
+
// we need this to get implicit construction TLazyValue from lambda
// and save default copy constructor and operator= for type TLazyValue
template <class T>
@@ -63,4 +63,4 @@ public:
template <typename F>
TLazyValue<TFunctionResult<F>> MakeLazy(F&& f) {
return {std::forward<F>(f)};
-}
+}
diff --git a/util/generic/lazy_value_ut.cpp b/util/generic/lazy_value_ut.cpp
index f6135880c3c..ba159f19f7b 100644
--- a/util/generic/lazy_value_ut.cpp
+++ b/util/generic/lazy_value_ut.cpp
@@ -1,22 +1,22 @@
-#include "lazy_value.h"
-
+#include "lazy_value.h"
+
#include <library/cpp/testing/unittest/registar.h>
-
+
Y_UNIT_TEST_SUITE(TLazyValueTestSuite) {
Y_UNIT_TEST(TestLazyValue) {
TLazyValue<int> value([]() {
- return 5;
- });
- UNIT_ASSERT(!value);
- UNIT_ASSERT_EQUAL(*value, 5);
- UNIT_ASSERT(value);
- }
-
+ return 5;
+ });
+ UNIT_ASSERT(!value);
+ UNIT_ASSERT_EQUAL(*value, 5);
+ UNIT_ASSERT(value);
+ }
+
Y_UNIT_TEST(TestLazyValueInitialization) {
TLazyValue<int> value1([]() { return 5; });
-
+
TLazyValue<int> value2 = []() { return 5; };
-
+
TLazyValue<int> notInitialized{};
TLazyValue<int> copy1(value1);
@@ -90,17 +90,17 @@ Y_UNIT_TEST_SUITE(TLazyValueTestSuite) {
}
class TValueProvider {
- public:
+ public:
static size_t CountParseDataCalled;
- TValueProvider()
+ TValueProvider()
: Data_([&] { return this->ParseData(); })
- {
- }
-
+ {
+ }
+
const TString& GetData() const {
return *Data_;
- }
+ }
private:
TLazyValue<TString> Data_;
@@ -109,16 +109,16 @@ Y_UNIT_TEST_SUITE(TLazyValueTestSuite) {
CountParseDataCalled++;
return "hi";
}
- };
-
+ };
+
size_t TValueProvider::CountParseDataCalled = 0;
Y_UNIT_TEST(TestValueProvider) {
- TValueProvider provider;
-
- UNIT_ASSERT(provider.GetData() == "hi");
- }
-
+ TValueProvider provider;
+
+ UNIT_ASSERT(provider.GetData() == "hi");
+ }
+
Y_UNIT_TEST(TestValueProviderCopy) {
TValueProvider provider;
provider.GetData();
@@ -147,11 +147,11 @@ Y_UNIT_TEST_SUITE(TLazyValueTestSuite) {
}
Y_UNIT_TEST(TestMakeLazy) {
- auto lv = MakeLazy([] {
- return 100500;
- });
- UNIT_ASSERT(!lv);
- UNIT_ASSERT(lv.GetRef() == 100500);
- UNIT_ASSERT(lv);
- }
-}
+ auto lv = MakeLazy([] {
+ return 100500;
+ });
+ UNIT_ASSERT(!lv);
+ UNIT_ASSERT(lv.GetRef() == 100500);
+ UNIT_ASSERT(lv);
+ }
+}