yql_lazy_init.h 641 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #pragma once
  2. #include <util/generic/ptr.h>
  3. #include <functional>
  4. #include <utility>
  5. namespace NYql {
  6. template <class T>
  7. class TLazyInitHolder
  8. : public TPointerBase<TLazyInitHolder<T>, T>
  9. {
  10. public:
  11. using TFactory = std::function<THolder<T>()>;
  12. TLazyInitHolder(TFactory&& factory)
  13. : Factory(std::move(factory))
  14. {
  15. }
  16. T* Get() const noexcept {
  17. if (!Value) {
  18. Value = Factory();
  19. }
  20. return Value.Get();
  21. }
  22. inline explicit operator bool() const noexcept {
  23. return !!Value.Get();
  24. }
  25. private:
  26. TFactory Factory;
  27. mutable THolder<T> Value;
  28. };
  29. } // NYql