#include #include #include #include "prelude.h" using namespace fcpp; using std::make_pair; using std::pair; using std::string; using std::cout; using std::endl; template struct Make1 : public CFunType { T* operator()(A1 x) const { return new T(x); } }; template class LazyPtrProxy { Fun0 f; mutable T* p; void cache() const { if(!p) p = f(); } public: LazyPtrProxy( const Fun0 ff ) : f(ff), p(0) {} template LazyPtrProxy( F ff ) : f(makeFun0(ff)), p(0) {} T& operator*() const { cache(); return *p; } T* operator->() const { cache(); return p; } }; typedef pair Extent; class Graphic { public: virtual Extent get_extent() const =0; virtual ~Graphic() {} }; class Image : public Graphic { public: Image( string filename ) { cout << "Opening file: " << filename << endl; } Extent get_extent() const { return make_pair(40,30); } }; class ImageProxy : public Graphic { LazyPtrProxy image; public: ImageProxy( string filename ) : image(curry(Make1(),filename)) {} Extent get_extent() const { return image->get_extent(); } }; int main() { cout << "make images" << endl; Graphic *i = new Image("g1"); Graphic *ip = new ImageProxy("g2"); cout << "find extents" << endl; i ->get_extent(); ip->get_extent(); }