aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/maybe_traits.h
blob: c7f2fed3e7dca6769222436448ae79c90cb464cf (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#pragma once

#include <memory>
#include <type_traits>
#include <initializer_list>

namespace NMaybe {
    struct TInPlace {};

    template <class T, bool = std::is_trivially_destructible<T>::value>
    struct TStorageBase {
        constexpr TStorageBase() noexcept
            : NullState_('\0') 
        { 
        } 

        template <class... Args>
        constexpr TStorageBase(TInPlace, Args&&... args)
            : Data_(std::forward<Args>(args)...)
            , Defined_(true) 
        { 
        } 

        ~TStorageBase() = default;

        union {
            char NullState_;
            T Data_;
        };
        bool Defined_ = false;
    };

    template <class T>
    struct TStorageBase<T, false> {
        constexpr TStorageBase() noexcept
            : NullState_('\0') 
        { 
        } 

        template <class... Args>
        constexpr TStorageBase(TInPlace, Args&&... args)
            : Data_(std::forward<Args>(args)...)
            , Defined_(true) 
        { 
        } 

        ~TStorageBase() {
            if (this->Defined_) {
                this->Data_.~T();
            }
        }

        union {
            char NullState_;
            T Data_;
        };
        bool Defined_ = false;
    };

    // -------------------- COPY CONSTRUCT --------------------

    template <class T, bool = std::is_trivially_copy_constructible<T>::value>
    struct TCopyBase: TStorageBase<T> { 
        using TStorageBase<T>::TStorageBase;
    };

    template <class T>
    struct TCopyBase<T, false>: TStorageBase<T> { 
        using TStorageBase<T>::TStorageBase;

        constexpr TCopyBase() = default;
        constexpr TCopyBase(const TCopyBase& rhs) {
            if (rhs.Defined_) {
                new (std::addressof(this->Data_)) T(rhs.Data_);
                this->Defined_ = true;
            }
        }
        constexpr TCopyBase(TCopyBase&&) = default;
        TCopyBase& operator=(const TCopyBase&) = default;
        TCopyBase& operator=(TCopyBase&&) = default;
    };

    // -------------------- MOVE CONSTRUCT --------------------

    template <class T, bool = std::is_trivially_move_constructible<T>::value>
    struct TMoveBase: TCopyBase<T> { 
        using TCopyBase<T>::TCopyBase;
    };

    template <class T>
    struct TMoveBase<T, false>: TCopyBase<T> { 
        using TCopyBase<T>::TCopyBase;

        constexpr TMoveBase() noexcept = default;
        constexpr TMoveBase(const TMoveBase&) = default;
        constexpr TMoveBase(TMoveBase&& rhs) noexcept(std::is_nothrow_move_constructible<T>::value) {
            if (rhs.Defined_) {
                new (std::addressof(this->Data_)) T(std::move(rhs.Data_));
                this->Defined_ = true;
            }
        }
        TMoveBase& operator=(const TMoveBase&) = default;
        TMoveBase& operator=(TMoveBase&&) = default;
    };

    // -------------------- COPY ASSIGN --------------------

    template <class T, bool = std::is_trivially_copy_assignable<T>::value>
    struct TCopyAssignBase: TMoveBase<T> { 
        using TMoveBase<T>::TMoveBase;
    };

    template <class T>
    struct TCopyAssignBase<T, false>: TMoveBase<T> { 
        using TMoveBase<T>::TMoveBase;

        constexpr TCopyAssignBase() noexcept = default;
        constexpr TCopyAssignBase(const TCopyAssignBase&) = default;
        constexpr TCopyAssignBase(TCopyAssignBase&&) = default;
        TCopyAssignBase& operator=(const TCopyAssignBase& rhs) {
            if (this->Defined_) {
                if (rhs.Defined_) {
                    this->Data_ = rhs.Data_;
                } else {
                    this->Data_.~T();
                    this->Defined_ = false;
                }
            } else if (rhs.Defined_) {
                new (std::addressof(this->Data_)) T(rhs.Data_);
                this->Defined_ = true;
            }
            return *this;
        }
        TCopyAssignBase& operator=(TCopyAssignBase&&) = default;
    };

    // -------------------- MOVE ASSIGN --------------------

    template <class T, bool = std::is_trivially_copy_assignable<T>::value>
    struct TMoveAssignBase: TCopyAssignBase<T> { 
        using TCopyAssignBase<T>::TCopyAssignBase;
    };

    template <class T>
    struct TMoveAssignBase<T, false>: TCopyAssignBase<T> { 
        using TCopyAssignBase<T>::TCopyAssignBase;

        constexpr TMoveAssignBase() noexcept = default;
        constexpr TMoveAssignBase(const TMoveAssignBase&) = default;
        constexpr TMoveAssignBase(TMoveAssignBase&&) = default;
        TMoveAssignBase& operator=(const TMoveAssignBase&) = default;
        TMoveAssignBase& operator=(TMoveAssignBase&& rhs) noexcept(
            std::is_nothrow_move_assignable<T>::value&& 
                std::is_nothrow_move_constructible<T>::value) 
        {
            if (this->Defined_) {
                if (rhs.Defined_) {
                    this->Data_ = std::move(rhs.Data_);
                } else {
                    this->Data_.~T();
                    this->Defined_ = false;
                }
            } else if (rhs.Defined_) {
                new (std::addressof(this->Data_)) T(std::move(rhs.Data_));
                this->Defined_ = true;
            }
            return *this;
        }
    };
}

template <class T>
using TMaybeBase = NMaybe::TMoveAssignBase<T>;