Field.hpp

00001 #ifndef __FIBER_FIELD_HPP
00002 #define __FIBER_FIELD_HPP "Created 27.02.2001 21:42:27 by werner"
00003 
00004 #include "FieldAPI.h"
00005 #include <memcore/RefPtr.hpp>
00006 #include <memcore/Creator.hpp>
00007 #include <memcore/Interface.hpp>
00008 #include <memcore/Ageable.hpp>
00009 #include <string>
00010 #include "MemArray.hpp"
00011 #include "CreativeArray.hpp"
00012 #include <map>
00013 
00014 #include "FragmentSelector.hpp"
00015 #include "FragmentID.hpp"
00016 #include "Attributes.hpp"
00017 #include "MemArrayProperties.hpp"
00018 
00019 
00020 namespace Fiber
00021 {
00022         using MemCore::ReferenceBase;
00023         using MemCore::Intercube;
00024         using MemCore::Interface;
00025         using MemCore::interface_cast;
00026         using MemCore::StrongPtr;
00027         using MemCore::NullPtr;
00028         using std::string;
00029         using std::map;
00030         using MemCore::memsize_t;
00031 
00037 class   FIELD_API FieldContainer : public ReferenceBase<FieldContainer>
00038 {
00039 public:
00041         FieldContainer();
00042 
00044         ~FieldContainer();
00045 };
00046 
00047 
00076 class   FIELD_API Field : public ReferenceBase<Field>, public Intercube, public MemCore::Ageable, public Attributes
00077 {
00078         /*TODO:
00079           Restructure. Not use a map from fragment ID's to arrays,
00080           but a vector of arrays, plus a reference to a FragmentProperties entry
00081           which is a vector of FragmentID's with reverse lookup.
00082          */
00083 
00084         typedef map< RefPtr<FragmentID>, RefPtr<CreativeArrayBase>, CompareFragmentIDs > fragments_t; 
00085 
00086 //      typedef map< index_t, RefPtr<CreativeArrayBase>, CompareFragmentIDs > Nfragments_t;
00087 
00088         fragments_t fragments;
00089 
00090 public:
00092         Field();
00093 
00095         Field(const RefPtr<CreativeArrayBase>&FFC);
00096 
00098         Field(const RefPtr<MemBase>&Mb);
00099 
00102         Field(const RefPtr<MemBase>&Mb, const RefPtr<MemCore::Cache>&Cache);
00103 
00107         template <class T, int N>
00108         Field(const MemCore::Ref<MemArray<N, T> >&MemRefPtr)
00109         : ReferenceBase<Field>(this)
00110         {
00111                 MemCore::SaveRegistry<Field>::addInterfaces(this); 
00112         RefPtr<MemBase> MembasePtr = MemRefPtr;
00113                 setPersistentData(MembasePtr);
00114         }
00115 
00121         bool setDiscardableData(const RefPtr<MemBase>&Mb, 
00122                                 const RefPtr<MemCore::Cache>&Cache = MemCore::Cache::MemCache(), 
00123                                 const RefPtr<FragmentID>& =NullPtr() ); 
00124 
00125 
00129         bool hasData(const RefPtr<FragmentID>&FID=NullPtr() ) const
00130         {
00131         RefPtr<CreativeArrayBase> CA = getCreator(FID);
00132                 if (!CA)
00133                         return false; 
00134 
00135                 return CA->hasData();
00136         } 
00137 
00138 
00148         bool setPersistentData( const RefPtr<MemBase>&Mb, const RefPtr<FragmentID>& =NullPtr(), 
00149                                 const RefPtr<MemCore::Cache>&Cache = MemCore::Cache::MemCache() );
00150 
00151 
00157         template <class T, int N>
00158         bool setPersistentData( const MemCore::RefPtr<Chunk<T> >&Mb, const MultiIndex<N>&Dimensions,
00159                                 const RefPtr<FragmentID>&FragID =NullPtr(), 
00160                                 const RefPtr<MemCore::Cache>&Cache = MemCore::Cache::MemCache() )
00161         {
00162                 return setPersistentData( new MemArray<N,T>(Mb, Dimensions), FragID, Cache);
00163         }
00176         template <class T>
00177         bool setPersistentData1D(const MemCore::RefPtr<Chunk<T> >&data,
00178                                  const RefPtr<FragmentID>&FragID =NullPtr(), 
00179                                  const RefPtr<MemCore::Cache>&Cache = MemCore::Cache::MemCache() )
00180         {
00181                 if (!data)
00182                         return false;
00183 
00184                 return setPersistentData( data, MIndex(data->size() ), FragID, Cache);
00185         }
00186 
00188         ~Field();
00189 
00198         RefPtr<MemArrayProperties> getProperties() const;
00199 
00209         const type_info&getFieldStorageType() const;
00210 
00216         const type_info&getElementType() const
00217         {
00218                 if (RefPtr<MemArrayProperties> MP = getProperties() )
00219                 {
00220                         return MP->myElementType();
00221                 }
00222                 else
00223                         return typeid(void);
00224         }
00225 
00226         MemCore::WeakPtr<FiberTypeBase> getElementFiberType() const
00227         {
00228                 if (RefPtr<MemArrayProperties> MP = getProperties() )
00229                         return MP->getFiberType().self(); 
00230 
00231                 return NullPtr();       
00232         } 
00233 
00241         int     getMemoryUsage(memsize_t&UsedMemory, memsize_t&WantedMemory) const;
00242 
00247         bool    isType(const TypeList_t&TL) const
00248         {
00249                 return containsType(TL, getElementType() );
00250         }
00251 
00257         const FiberTypeBase&getFiberType() const
00258         {
00259                 if (RefPtr<MemArrayProperties> MP = getProperties() )
00260                 {
00261                         return MP->getFiberType();
00262                 }
00263                 else
00264                         return FiberType<void>::getFiberType();
00265         }
00266 
00279         bool addFiberType(TypeList_t&TypeList) const
00280         {
00281                 if (RefPtr<MemArrayProperties> MP = getProperties() )
00282                 {
00283                         TypeList.insert(  MP->getFiberType().self() ); 
00284                         return true;
00285                 } 
00286                 return false;
00287         }
00288 
00297         void setCreator(const RefPtr<CreativeArrayBase>&FFC, const RefPtr<FragmentID>& =NullPtr() );
00298 
00300         size_t nFragments() const
00301         {
00302                 return fragments.size();
00303         } 
00304 
00305         RefPtr<FragmentID> getFragmentID(const string&FragmentName) const;
00306 
00312         RefPtr<CreativeArrayBase> getCreator(const RefPtr<FragmentID>& =NullPtr() ) const; 
00313 
00318         RefPtr<CreativeArrayBase> getCreator(const string&FragmentName) const;
00319 
00324         RefPtr<CreativeArrayBase> getCompatibleCreator(const RefPtr<FragmentID>&F =NullPtr() ) const
00325         {
00326                 if (!F) return getCreator();
00327         RefPtr<CreativeArrayBase> CAB = getCreator(F); 
00328                 if (CAB) return CAB; 
00329                 return getCreator( F->Name() );
00330         }
00331 
00332 
00339         RefPtr<MemBase> getData(const RefPtr<FragmentID>&FID=NullPtr() ) const
00340         {
00341                 if (RefPtr<CreativeArrayBase> CA = getCreator(FID) )
00342                         return CA->create();
00343 
00344                 return NullPtr();
00345         } 
00346 
00351         RefPtr<MemCore::ChunkBase> getChunk(const RefPtr<FragmentID>&FID=NullPtr() ) const
00352         {
00353                 if (RefPtr<MemBase> Mb = getData( FID ) )
00354                         return Mb->getChunk();
00355 
00356                 return NullPtr();
00357         }
00358 
00363         RefPtr<MemBase> getCompatibleData(const RefPtr<FragmentID>&F =NullPtr() ) const
00364         {
00365         RefPtr<CreativeArrayBase> CA = getCompatibleCreator(F);
00366                 if (!CA)
00367                         return NullPtr();
00368 
00369                 return CA->create();
00370         } 
00371 
00372         typedef FragmentIterator Iterator;
00373 
00377         int     iterate(FragmentIterator&, const MemCore::WeakPtr<FragmentSelector>&FS = NullPtr() );
00378 
00384         int rank() const; 
00385 
00393         RefPtr<SizeInterface> getSize() const; 
00394 
00399         void    Speak(int indent=0, int maxindent=-1) const;
00400 };
00401 
00402 } /* namespace Fiber */
00403 
00404 #endif /* __FIBER_FIELD_HPP */