summaryrefslogtreecommitdiff
path: root/common/external/nillable.h
blob: 40cf01c6f883fe2779eac64c8307a4f9026b025b (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
inline constexpr struct nil_t {} nil;

// Requirements: T is default-constructible and trivially-destructible
template<typename T>
struct nillable {
    bool has_value = false;
    T value;

    nillable() = default;

    nillable(nil_t) : nillable() {}
    nillable(const T& v) : has_value(true), value(v) {}

    nillable& operator=(nil_t) {
        has_value = false;
        return *this;
    }
    nillable& operator=(const T& v) {
        value = v;
        has_value = true;
        return *this;
    }

    const T* operator->() const { return &value; }
    T* operator->() { return &value; }

    explicit operator bool() const { return has_value; }
};

template<typename T> nillable(T)->nillable<T>;