aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/misc/optional.h
blob: cfae5c36d549475a1bd0fbc007c574543c0e1841 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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;
    }
};