aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/providers/common/transform/yql_lazy_init.h
blob: b9c7a3dd4ad772664e09ef4e015a4a0e95ed96b4 (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
#pragma once

#include <util/generic/ptr.h>

#include <functional>
#include <utility>

namespace NYql {

template <class T>
class TLazyInitHolder
    : public TPointerBase<TLazyInitHolder<T>, T>
{
public:
    using TFactory = std::function<THolder<T>()>;

    TLazyInitHolder(TFactory&& factory)
        : Factory(std::move(factory))
    {
    }

    T* Get() const noexcept {
        if (!Value) {
            Value = Factory();
        }
        return Value.Get();
    }

    inline explicit operator bool() const noexcept {
        return !!Value.Get();
    }

private:
    TFactory Factory;
    mutable THolder<T> Value;

};

} // NYql