aboutsummaryrefslogblamecommitdiffstats
path: root/util/generic/lazy_value.h
blob: 3c720f76b5a9b1a67d8b9c0c86bef611af8a5d46 (plain) (tree)
1
2
3
4
5
6
7
8
9

                  
                     
                  
                      
       
                                            
 
                               

                                             

























                                             
                              






                                    

                                                                      
                                            


                                                         
     
  

                                                
 
#pragma once

#include "maybe.h"
#include "function.h"

template <class T>
class TLazyValueBase {
public:
    using TInitializer = std::function<T()>;

    TLazyValueBase() = default;

    TLazyValueBase(TInitializer initializer)
        : Initializer(std::move(initializer))
    {
    }

    explicit operator bool() const noexcept {
        return Defined();
    }

    bool Defined() const noexcept {
        return ValueHolder.Defined();
    }

    const T& GetRef() const {
        if (!Defined()) {
            InitDefault();
        }
        return *ValueHolder;
    }

    const T& operator*() const {
        return GetRef();
    }

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

    void InitDefault() const {
        Y_ASSERT(Initializer);
        ValueHolder = Initializer();
    }

private:
    mutable TMaybe<T> ValueHolder;
    TInitializer Initializer;
};

// we need this to get implicit construction TLazyValue from lambda
// and save default copy constructor and operator= for type TLazyValue
template <class T>
class TLazyValue: public TLazyValueBase<T> {
public:
    template <typename... TArgs>
    TLazyValue(TArgs&&... args)
        : TLazyValueBase<T>(std::forward<TArgs>(args)...)
    {
    }
};

template <typename F>
TLazyValue<TFunctionResult<F>> MakeLazy(F&& f) {
    return {std::forward<F>(f)};
}