class TestClass{public: static TestClass &get() { static TestClass instance; return instance; }};
template <class T>class Lazy{ public: T* operator ->() { return GetObject(); } private: T *GetObject() { if (_instance.isNull()) { _instance = QSharedPointer<T>::create(); } return _instance.data(); } QSharedPointer<T> _instance;};
template<typename... Values>
C++ (Qt)template<typename T, typename... Ts>class Lazy{public: template<typename... Us> explicit Lazy(Us &&... args) : args_(std::make_tuple(std::forward<Us>(args)...)) { } T *GetObject() { if (!ptr_) { ptr_.reset(createObject<T>(args_)); } return ptr_.get(); } private: std::tuple<Ts...> args_; std::unique_ptr<T> ptr_;};
C++ (Qt)#include <functional>#include <memory> using std::shared_ptr;using std::make_shared;using std::function;using std::bind; template <typename T, typename ...Param>class Lazy{ using pointer = shared_ptr<T>; public: explicit Lazy( Param... p ) : m_builder( bind( &Lazy::build, this, p... ) ) { } pointer operator ->() { return ptr(); } private: pointer ptr() { if( !m_obj ) m_obj = m_builder(); return m_obj; } pointer build( Param... p ) { return make_shared<T>( p... ); } function<pointer ()> m_builder; shared_ptr<T> m_obj;};
C++ (Qt) Lazy<Foo,int> p1( 10 ); cout << p1->a() << endl; Lazy<Foo,int,int> p2( 10, 20 ); cout << p2->a() << endl;
C++ (Qt)template <typename T, typename ...Param>Lazy<T, Param...> makeLazy(Param... p) { return Lazy<T, Param...>(p...);}
C++ (Qt) auto p1 = makeLazy<Foo>( 10 ); cout << p1->a() << p1->b() << endl; auto p2 = makeLazy<Foo>( 10, 20 ); cout << p2->a() << p2->b() << endl;
C++ (Qt)#include <functional>#include <memory> using std::shared_ptr;using std::make_shared;using std::function;using std::bind; template <typename T, typename ...Param>class Lazy{ using pointer = shared_ptr<T>; public: explicit Lazy( Param... p ) : m_builder( [=](){ return make_shared<T>( p... ); } ) { } pointer operator ->() { return ptr(); } private: pointer ptr() { if( !m_obj ) m_obj = m_builder(); return m_obj; } function<pointer ()> m_builder; shared_ptr<T> m_obj;};
template<class T>class Lazy{public: template<typename... Args> Lazy(Args ... arguments) : _instance(nullptr) , _factory([=]() { return new T(arguments...); }) { } ~Lazy() { if (_instance != nullptr) { delete _instance; } } T* operator ->() { return GetObject(); }private: T* GetObject() { if (_instance == Q_NULLPTR) { _instance = _factory(); } return _instance; }private: T* _instance; std::function<T*()> _factory;};
C++ (Qt) ~Lazy() { if (_instance != nullptr) { delete _instance; } }
C++ (Qt)#include <functional>#include <memory> using std::shared_ptr;using std::make_shared;using std::function;using std::bind; template <typename T>class Lazy{ using pointer = shared_ptr<T>; public: typename < typename ... Param> explicit Lazy( Param ... p ) : m_builder( [=](){ return make_shared<T>( p... ); } ) { } pointer operator ->() { return ptr(); } private: pointer ptr() { if( !m_obj ) m_obj = m_builder(); return m_obj; } function<pointer ()> m_builder; shared_ptr<T> m_obj;public: template < typename ... Param> static Lazy<T> make ( Param ... p) { return Lazy<T>( p... ); }};
C++ (Qt) using LazyFoo = Lazy< Foo >; LazyFoo p1 = LazyFoo::make( 10 ); LazyFoo p2 = LazyFoo::make( 10, 20 ); LazyFoo p3 = p1;
C++ (Qt) T* GetObject() { if (_instance == Q_NULLPTR) { _instance = _factory(); } return _instance; }
C++ (Qt) T* GetObject() { if (!_instance) { _instance = _factory(); } return _instance; }