diff options
| author | Alexander Smirnov <[email protected]> | 2024-03-08 15:28:06 +0000 |
|---|---|---|
| committer | Alexander Smirnov <[email protected]> | 2024-03-08 15:28:06 +0000 |
| commit | 88ff3e3407899373c8530c6022adb9f9b457ed77 (patch) | |
| tree | 0e933be2b04d0f5fcbdaa9e8e5eb23b6c1a84120 /library/cpp | |
| parent | 535661a17cedc08b15dc405fe75107baae61bb67 (diff) | |
| parent | e0439374e8770430b5a391cea94769059544e2a2 (diff) | |
Merge branch 'rightlib' into mergelibs-240308-1527
Diffstat (limited to 'library/cpp')
| -rw-r--r-- | library/cpp/yt/misc/optional.h | 72 |
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 00000000000..cfae5c36d54 --- /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; + } +}; |
