aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp
diff options
context:
space:
mode:
authorAlexSm <alex@ydb.tech>2024-03-12 11:36:58 +0100
committerGitHub <noreply@github.com>2024-03-12 11:36:58 +0100
commit96b75572b42484fbcc5f128c727205d85a16e218 (patch)
tree3592279bfb00d6428b3232041b19f3de74b85ead /library/cpp
parent196eab23300e1c293a748efb74e3cbcf95e3c83a (diff)
parentc046cee958ce7740a4917d65034028618d254624 (diff)
downloadydb-96b75572b42484fbcc5f128c727205d85a16e218.tar.gz
Merge pull request #2576 from ydb-platform/mergelibs-240308-1527
Library import 240308-1527
Diffstat (limited to 'library/cpp')
-rw-r--r--library/cpp/yt/misc/optional.h72
1 files changed, 72 insertions, 0 deletions
diff --git a/library/cpp/yt/misc/optional.h b/library/cpp/yt/misc/optional.h
new file mode 100644
index 0000000000..cfae5c36d5
--- /dev/null
+++ b/library/cpp/yt/misc/optional.h
@@ -0,0 +1,72 @@
+#pragma once
+
+#include <util/string/cast.h>
+
+#include <optional>
+
+namespace NYT {
+
+////////////////////////////////////////////////////////////////////////////////
+
+template <class T>
+struct TOptionalTraits
+{
+ using TOptional = std::optional<T>;
+ using TValue = T;
+};
+
+template <class T>
+struct TOptionalTraits<std::optional<T>>
+{
+ using TOptional = std::optional<T>;
+ using TValue = T;
+};
+
+template <class T>
+struct TOptionalTraits<T*>
+{
+ using TOptional = T*;
+ using TValue = T*;
+};
+
+template <class T>
+struct TOptionalTraits<TIntrusivePtr<T>>
+{
+ using TOptional = TIntrusivePtr<T>;
+ using TValue = TIntrusivePtr<T>;
+};
+
+////////////////////////////////////////////////////////////////////////////////
+
+template <class T>
+struct TStdOptionalTraits
+{
+ static constexpr bool IsStdOptional = false;
+ using TValueType = T;
+};
+
+template <class T>
+struct TStdOptionalTraits<std::optional<T>>
+{
+ static constexpr bool IsStdOptional = true;
+ using TValueType = T;
+};
+
+////////////////////////////////////////////////////////////////////////////////
+
+} // namespace NYT
+
+template <class T>
+TString ToString(const std::optional<T>& nullable)
+{
+ return nullable ? ToString(*nullable) : "<Null>";
+}
+
+template <class T>
+struct THash<std::optional<T>>
+{
+ size_t operator()(const std::optional<T>& nullable) const
+ {
+ return nullable ? THash<T>()(*nullable) : 0;
+ }
+};