#ifndef ADHOCUTIL_LAZYPOINTER_H #define ADHOCUTIL_LAZYPOINTER_H #include #include #include template > class LazyPointer { public: typedef T element_type; typedef P pointer_type; typedef boost::function0

Factory; typedef boost::variant Source; // Constructors LazyPointer(const Factory & f) : source(f) { } LazyPointer(const P & p) : source(p) { } LazyPointer(T * p) : source(P(p)) { } LazyPointer() : source(P(NULL)) { } // Getters operator P() const { return deref(); } T * operator->() const { return get(); } T & operator*() const { return *get(); } T * get() const { return deref().get(); } P deref() const { if (Factory * f = boost::get(&source)) { P p = (*f)(); source = p; return p; } else { return boost::get

(source); } } bool operator!() const { return get() == nullptr; } operator bool() const { return get() != nullptr; } bool operator==(const P & o) const { return (deref() == o); } bool operator==(const T * o) const { return (deref().get() == o); } // Setters LazyPointer & operator=(const P & p) { source = p; return *this; } LazyPointer & operator=(T * t) { source = P(t); return *this; } LazyPointer & operator=(const Factory & f) { source = f; return *this; } bool hasValue() const { return boost::get

(&source); } private: mutable Source source; }; namespace boost { template R * dynamic_pointer_cast(const LazyPointer & p) { return dynamic_cast(p.get()); } } #endif