FiberType.hpp

00001 #ifndef __FIBERTYPE_HPP
00002 #define __FIBERTYPE_HPP "Created 27.02.2001 21:42:27 by werner"
00003 
00004 #include "FieldAPI.h"
00005 
00006 #include <eagle/MetaInfo.hpp>
00007 #include <memcore/RefPtr.hpp>
00008 #include <memcore/Interface.hpp>
00009 #include <memcore/typemap.hpp>
00010 
00011 #include <assert.h>
00012 #include <string>
00013 
00017 namespace Fiber
00018 {
00019         using std::type_info;
00020         using std::string;
00021 
00027 class   FIELD_API FiberTypeBase : public MemCore::Interface<FiberTypeBase>
00028 {
00029 public:
00034         FiberTypeBase(bool isStaticObject);
00035 
00036         virtual ~FiberTypeBase();
00037 
00041         virtual const type_info&getType() const = 0;
00042 
00048         virtual unsigned multiplicity() const = 0;
00049 
00053         virtual unsigned mem_size() const = 0;
00054 
00062         virtual int rank() const = 0;
00063 
00067         virtual int grade() const = 0;
00068 
00074         virtual int element_index(const int i[]) const = 0;
00075 
00079         virtual unsigned element_size() const = 0;
00080 
00081         virtual const FiberTypeBase&element_type() const = 0;
00082 
00083         virtual const char*coordinate_component_name(int i) const = 0;
00084 
00085         virtual const type_info&chart_type() const = 0;
00086 
00087         string chart_name() const
00088         {
00089                 return MemCore::Typename(chart_type() );
00090         }
00091 
00092         virtual int   chart_dimension() const = 0;
00093 
00094         virtual int indexing_scheme(int i) const = 0;
00095 
00096         bool is_lower_index(int i) const
00097         {
00098                 return indexing_scheme(i)<0;
00099         }
00100 
00101         bool is_upper_index(int i) const
00102         {
00103                 return indexing_scheme(i)>0;
00104         }
00105 
00106         virtual const char*nontensor() const = 0;
00107 
00108         std::string description() const;
00109 
00110         friend bool operator<(const FiberTypeBase&l, const FiberTypeBase&r);
00111 };
00112 
00113 struct  FIELD_API FTless
00114 {
00115         bool operator()(const MemCore::WeakPtr<FiberTypeBase>&l, const MemCore::WeakPtr<FiberTypeBase>&r) const;
00116 };
00117 
00118 
00119 class   FIELD_API TypeAction : public MemCore::ReferenceBase<TypeAction>, public MemCore::Intercube
00120 {
00121 public:
00122         TypeAction();
00123         ~TypeAction();
00124 
00125 static  MemCore::RefPtr<TypeAction> create(const MemCore::WeakPtr<FiberTypeBase>&FT);
00126 static  MemCore::RefPtr<TypeAction> find(const MemCore::WeakPtr<FiberTypeBase>&FT);
00127 
00128         struct  FIELD_API Iterator
00129         {
00130                 virtual ~Iterator() = 0;
00131 
00132                 virtual bool apply(const MemCore::WeakPtr<FiberTypeBase>&FTB, 
00133                                    const MemCore::RefPtr<TypeAction>&TA) = 0;
00134         };
00135 
00136 static  int iterate(const type_info&atomic_type, int multiplicity, Iterator&it);
00137 
00138         struct  FIELD_API TypeIterator
00139         {
00140                 virtual ~TypeIterator() = 0;
00141 
00142                 virtual bool apply(const type_info&ti,
00143                                    const MemCore::WeakPtr<FiberTypeBase>&FTB, 
00144                                    const MemCore::RefPtr<TypeAction>&TA) = 0;
00145         };
00146 
00147 static  int iterateAll(Iterator&it);
00148 static  int iterate(TypeIterator&it, int multiplicity = 1);
00149 
00150 static  int coordinate_iterate(const type_info&chart_type, int tensor_rank, int*indexing_scheme,
00151                                Iterator&it);
00152 
00153 static  size_t coordinate_entries(const type_info&chart_type);
00154 
00155 };
00156 
00157 
00158 template <class T> struct SizeOf       { static size_t memsize() { return sizeof(T); } };
00159 template <>        struct SizeOf<void> { static size_t memsize() { return 0;         } };
00160 
00169 template <class T>
00170 class   FiberType : public FiberTypeBase
00171 {
00172 public:
00173         FiberType(bool isStaticObject)
00174         : FiberTypeBase(isStaticObject)
00175         {}
00176 
00177 //static        FiberType TypeInfo;
00178 
00179         typedef Eagle::MetaInfo<T> FiberInfo_t;
00180         typedef typename FiberInfo_t::element_t element_t;
00181 
00182         typedef typename FiberInfo_t::Chart_t Chart_t;
00183 
00184         enum { Dims = Eagle::CoordinateInfo<Chart_t>::Dims };
00185 
00186         override const type_info&getType() const
00187         {
00188                 return typeid(T); 
00189         }
00190 
00191         override unsigned element_size() const
00192         {
00193                 return SizeOf<typename FiberInfo_t::element_t>::memsize();
00194         }
00195 
00196 static  const FiberTypeBase&getFiberType()
00197         {
00198         static  FiberType<T> myType(true); 
00199                 return myType;
00200         }
00201 
00202         override const FiberTypeBase&element_type() const
00203         {
00204                 return FiberType<element_t>::getFiberType();
00205         }
00206 
00207         override unsigned multiplicity() const
00208         {
00209                 return FiberInfo_t::MULTIPLICITY;
00210         }
00211 
00212         override int rank() const
00213         {
00214                 return FiberInfo_t::RANK;
00215         }
00216 
00222         override int grade() const
00223         {
00224                 return FiberInfo_t::GRADE;
00225         }
00226 
00228         override const char*coordinate_component_name(int i) const
00229         {
00230                 return Eagle::CoordinateInfo<Chart_t>::coordinate_name(i);
00231         }
00232 
00234         override const type_info&chart_type() const
00235         {
00236                 return typeid(Chart_t);
00237         }
00238 
00240         override int chart_dimension() const 
00241         {
00242                 return Dims;
00243         }
00244 
00245         override int element_index(const int i[]) const
00246         {
00247                 return Eagle::MetaInfoElementIndex<T>::element_index(i);
00248         }
00249 
00251         override unsigned mem_size() const
00252         {
00253                 return SizeOf<T>::memsize();
00254         }
00255 
00256         override int indexing_scheme(int i) const
00257         {
00258                 return Eagle::TypeIndexingScheme<T>::index(i);
00259         }
00260 
00261         override const char*nontensor() const
00262         {
00263                 return 0;
00264 //              return FiberInfoNonTensor<T>::object_name();
00265         }
00266 };
00267 
00268 
00272 struct  FIELD_API TypeList : public std::set<MemCore::WeakPtr<FiberTypeBase> >
00273 {
00275         TypeList();
00276 
00278         ~TypeList(); 
00279 
00281         bool    contains(const type_info&what) const;
00282 
00284         void speak() const;
00285 };
00286 
00287 typedef TypeList TypeList_t;
00288 
00292 extern FIELD_API bool   containsType(const TypeList_t&TL, const type_info&what);
00293 
00308 template <class T>
00309 struct AcceptType
00310 {
00319         AcceptType(Fiber::TypeList_t&TL)
00320         {
00321                 TL.insert( Fiber::FiberType< T >::getFiberType().self() );
00322         }
00323 
00324 static void accept(Fiber::TypeList_t&TL)
00325         {
00326                 TL.insert( Fiber::FiberType< T >::getFiberType().self() );
00327         } 
00328 };
00329 
00330 
00331 } /* namespace Fiber */ 
00332 
00333 
00334 #endif /* __FIBERTYPE_HPP */
00335