MemBase.hpp

00001 #ifndef __FIBER_MEMBASE_HPP
00002 #define __FIBER_MEMBASE_HPP
00003 
00004 #include "FieldAPI.h"
00005 #include <memcore/RefPtr.hpp>
00006 #include <memcore/Interface.hpp>
00007 #include <memcore/Persistencer.hpp>
00008 #include <memcore/Cacheable.hpp>
00009 #include <memcore/Cache.hpp>
00010 #include <memcore/Creator.hpp>
00011 #include <memcore/Chunk.hpp>
00012 
00013 #include <vector/MultiArray.hpp>
00014 
00015 #include <typeinfo>
00016 #include "FiberType.hpp"
00017 #include "DynamicSize.hpp"
00018 
00019 namespace Fiber
00020 {
00021 class FIELD_API MemBase;
00022 }
00023 
00024 namespace MemCore
00025 {
00026 
00027 template <>
00028 struct  DeferredInitializationTrait<Fiber::MemBase>
00029 {
00030 static  void call(Fiber::MemBase*ma);
00031 };
00032 
00033 }
00034 
00035 namespace Fiber
00036 {
00037         using std::type_info;
00038 
00039         using MemCore::RefPtr;
00040         using MemCore::ReferenceBase;
00041         using MemCore::Chunk;
00042 
00043 
00057 class FIELD_API MemBase : public MemCore::Cacheable, public MemCore::Intercube
00058 {
00059 //      set<MemCore::WeakPtr<MemArrayBase> > > Sources;
00060 public:
00061         typedef MemCore::WeakPtr<MemCore::CreatorBase<MemBase> > Creator_t;
00062 
00064         MemBase(const Creator_t&theCreator);
00065 
00067         ~MemBase();
00068 
00072         virtual void DeferredConstructor() = 0;
00073 //      {       puts("MemArrayBase:: DeferredConstructor() called..."); }
00074 
00076         virtual int rank() const = 0; 
00077 
00079         virtual DynamicSize getSize() const = 0; 
00080 
00082         virtual index_t nElements() const
00083         {
00084                 return getSize().nElements();
00085         } 
00086 
00088         virtual const type_info&getType() const = 0;
00089 
00090         virtual HyperslabParameters&getHyperslabParameters() = 0; 
00091 
00092 
00094 
00096         virtual void*getPtr() = 0; 
00097 
00099         virtual RefPtr<MemCore::ChunkBase> getChunk() const = 0; 
00100 
00102         virtual const void*OriginPtr() const = 0; 
00104         virtual const void*DeltaPtr()  const = 0; 
00105 
00107 
00108         virtual const FiberTypeBase&getFiberType() const = 0; 
00109 
00115         virtual RefPtr<MemBase> makeMemArray(const MemBase::Creator_t&C) const = 0;
00116 
00121         virtual RefPtr<MemBase> newMemArray(const MemBase::Creator_t&C) const = 0;
00122 
00123         virtual RefPtr<MemBase> getComponentArray(int member, const MemBase::Creator_t&C) = 0; 
00124 
00125         virtual RefPtr<MemBase> createSubArray(const DynamicSize&Offset, const DynamicSize&CopySize, const MemBase::Creator_t&C) const = 0; 
00126 
00133         virtual RefPtr<MemBase> createEquallySizedMemArray(const RefPtr<MemCore::ChunkBase>&Storage, const MemBase::Creator_t&C) const = 0; 
00134 
00135         /*
00136           Create new array of the same type as the current one, but with the specified dimensions.
00137           No initializations are performed here.
00138          */
00139         virtual RefPtr<MemBase> newMemArray(const DynamicSize&newSize, const MemBase::Creator_t&C) const = 0;
00140 
00147         virtual RefPtr<MemBase> createIndirection(const DynamicSize&newSize,
00148                                                   const std::vector<index_t>&Indirection, 
00149                                                   const MemBase::Creator_t&theCreator) const = 0;
00150 
00156         class FIELD_API Allocator : public MemCore::Interface<Allocator>
00157         {
00158         public:
00159                 Allocator();
00160                 ~Allocator(); 
00161 
00162                 virtual RefPtr<MemBase> create(int rank, const index_t dims[], const Creator_t&) const = 0;
00163         };
00164 
00165 
00173 static  RefPtr<MemBase> create(const RefPtr<FiberTypeBase>&FT,
00174                                int rank, const index_t dims[], 
00175                                const Creator_t&C,
00176                                const type_info&layout = typeid(void) );
00177 
00178         RefPtr<MemBase> create(const RefPtr<FiberTypeBase>&FT, 
00179                                const Creator_t&C,
00180                                const type_info&layout = typeid(void) )
00181         {
00182                 return create(FT, getSize().size(),
00183                               getSize().dims_ptr(),
00184                               C, layout);
00185         }
00186 
00187 static  bool registerCreator(const RefPtr<Allocator>&A,
00188                              const RefPtr<FiberTypeBase>&FT);
00189 
00190 static  FIELD_API int  RegisteredTypes;
00191 static  FIELD_API int  StandardTypes;
00192 
00193 };
00194 
00203 typedef MemCore::CreatorBase<MemBase> CreativeArrayBase;
00204 
00205 
00206 inline RefPtr<MemCore::ChunkBase> getChunk(const MemCore::WeakPtr<MemBase>&MemData)
00207 {
00208         if (MemData) 
00209                 return MemData->getChunk(); 
00210 
00211         return MemCore::NullPtr();
00212 }
00213 
00214 inline RefPtr<MemCore::ChunkBase> getChunk(const MemCore::WeakPtr<CreativeArrayBase>&MemDataCreator)
00215 {
00216         if (MemDataCreator) 
00217                 return getChunk(MemDataCreator->create() ); 
00218 
00219         return MemCore::NullPtr();
00220 }
00221 
00222 
00223 
00224 template <class T>
00225 class   MemArrayAllocator : public MemBase::Allocator
00226 {
00227 public:
00228         override RefPtr<MemBase> create(int rank, const index_t dims[], const MemCore::WeakPtr<CreativeArrayBase>&) const;
00229 
00230 static  const FiberTypeBase&getFiberType()
00231         {
00232                 return FiberType<T>::getFiberType();
00233         }
00234 
00235 static  bool    registerMe()//const type_info&layout = typeid(void) )
00236         {
00237         RefPtr<MemBase::Allocator> A = new MemArrayAllocator(); 
00238 //              printf("MemArrayAllocator<%s>::registerMe()\n", typeid(T).name() );
00239                 return MemBase::registerCreator( A, getFiberType().self());
00240         }
00241 
00242 static  bool    isRegistered()
00243         {
00244         static  bool b = registerMe(); 
00245 //              printf(b?"isRegistered is TRUE":"isRegistered is FALSE");
00246                 return b||true;
00247         }
00248 };
00249 
00250 } // namespace Fiber
00251 
00252 namespace MemCore
00253 {
00254 inline  void DeferredInitializationTrait<Fiber::MemBase>::call(Fiber::MemBase*ma)
00255         {
00256                 ma->DeferredConstructor();
00257         }
00258 }
00259 
00260 #endif /* __FIBER_MEMARRAY_HPP */
00261