OnDemandCreator.hpp

00001 #ifndef __FIBER_FIELD_ONDEMAND_CREATOR_HPP
00002 #define __FIBER_FIELD_ONDEMAND_CREATOR_HPP
00003 
00004 #include "CreativeArray.hpp"
00005 #include "MemArrayProperties.hpp"
00006 
00007 namespace Fiber
00008 {
00009 
00054 template <class Computer>
00055 class   OnDemandCreator : public CreativeArrayBase
00056 {
00057         typedef typename Computer::ResultArray_t        ResultArray_t;
00058         typedef typename Computer::Constructor_t        Constructor_t;
00059 
00060         Constructor_t           MyConstructor;
00061         RefPtr<ResultArray_t>   MyData;
00062 
00063 public:
00064         enum
00065         {
00067                 RANK = ResultArray_t::Dims
00068         };
00069 
00071         typedef typename ResultArray_t::value_type value_type;
00072 
00074 static  const FiberTypeBase&getFiberType()
00075         {
00076                 return MemArrayAllocator<value_type>::getFiberType();
00077         }
00078 
00082         OnDemandCreator(const Constructor_t&myConstructor, const MemCore::RefPtr<MemCore::Cache>&theCache)
00083         : CreativeArrayBase(theCache)
00084         , MyConstructor(myConstructor)
00085         {
00086                 addInterface( new MemArrayProperties(RANK, getFiberType() )); 
00087         }
00088 
00089         override bool release()
00090         {
00091                 MyData = ::MemCore::NullPtr();
00092                 return true;
00093         }
00094 
00095         override bool hasData() const
00096         {
00097                 if (MyData)
00098                         return true; 
00099                 else
00100                         return false;
00101         }
00102 
00103         override const std::type_info& getType()
00104         {
00105                 return typeid( typename ResultArray_t::value_type );
00106         }
00107 
00108         override RefPtr<MemBase> get() const
00109         {
00110                 return MyData; 
00111         }
00112 
00113         override RefPtr<MemBase> create()
00114         {
00115                 if (MyData)
00116                         return MyData; 
00117 
00118         Computer MyComputer( MyConstructor, self() ); 
00119                 MyData = MyComputer.result(); 
00120                 return MyData;
00121         }
00122 
00123 };
00124 
00125 } // namespace Fiber
00126 
00127 #endif  // __FIBER_FIELD_ONDEMAND_CREATOR_HPP
00128 
00129