Main Page | Namespace List | Class Hierarchy | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages

Type.hh

Go to the documentation of this file.
00001 #ifndef RFL_TYPE_HH
00002 #define RFL_TYPE_HH
00003 /* C++ Reflection & Serice Library
00004  * Copyright (C) 2003  Marcus Perlick
00005  * mailto: riffraff@users.sf.net
00006  * 
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  * 
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00020  * USA
00021  *
00022  * This file is part of the "C++ Reflection & Serice Library" */
00023 #include <typeinfo>
00024 
00025 #include <mpu/String.hh>
00026 #include <avcall.h>
00027 
00028 #include "AspectVector.hh"
00029 #include "Aspect.hh"
00030 
00037 namespace rfl {
00038 
00052   class Type {
00053   public:
00054 
00060     enum Id {
00061       ID_VOID = 0,
00062 
00063       // Funadmental Types
00064       ID_BOOL,
00065       ID_CHAR,
00066       ID_UCHAR,
00067       ID_SHORT,
00068       ID_USHORT,
00069       ID_INT,
00070       ID_UINT,
00071       ID_LONG,
00072       ID_ULONG,
00073       ID_FLOAT,
00074       ID_DOUBLE,
00075 
00078       ID_FUNDAMENTAL_END,
00079 
00080       // Ths first non-fundamental type
00081       ID_PTR = ID_FUNDAMENTAL_END,
00082       ID_ARRAY,
00083       ID_CLASS,
00084       ID_FUNCTION
00085     };
00086 
00088     Type( Id id,
00089           const ::mpu::String& name,
00090           std::size_t size,
00091           std::size_t alignment,
00092           std::size_t elementNum,
00093           const std::type_info* typeId,
00094           void(*create)(const Type*,void*),
00095           void(*cCreate)(const Type*,void*,const void*),
00096           void(*destroy)(const Type*,void*),
00097           void(*assign)(const Type*,void*,const void*),
00098           void*(*newObj)(const Type*),
00099           void*(*newCopy)(const Type*,const void*),
00100           void(*delObj)(const Type*,void*) ) :
00101       elmNo_( elementNum ),
00102       name_( name ),
00103       create_( create ), cCreate_(cCreate), destroy_( destroy ),
00104       assign_( assign ), newObj_( newObj ), newCopy_( newCopy ),
00105       delObj_( delObj ),
00106       size_( size ), align_( alignment ), id_( id )
00107     {}
00108     
00109     virtual ~Type( void ) {}
00110 
00112     Id getId( void ) const { return id_; }
00113 
00119     const std::type_info* getTypeId( void ) { return typeId_; }
00120 
00121     const mpu::String& getName( void ) const { return name_; }
00122     std::size_t getSize( void ) const { return size_; }
00123     std::size_t getAlignment( void ) const { return align_; }
00124 
00125     void create( void* addr ) const;
00126     void create( void* addr, const void* obj ) const;
00127     void destroy( void* obj ) const;
00128     void assign( void* dst, const void* src ) const;
00129 
00130     void* newObj( void ) const;
00131     void* newObj( const void* obj ) const;
00132     void delObj( void* obj ) const;
00133 
00134     virtual void avStartCall( av_alist& avList, 
00135                               void* fnPtr,
00136                               void *retVal ) const = 0;
00137 
00138     virtual void avPutArg( av_alist& avList, void* arg ) const = 0;
00139 
00140     std::size_t getElmNo( void ) const { return elmNo_; }
00141     virtual const Type& getElmType( std::size_t idx ) const = 0;
00142     virtual void* getElmAddr( void* obj, std::size_t idx ) const = 0;
00143     const void* getElmAddr( const void* obj, std::size_t idx ) const;
00144 
00146     virtual unsigned long hash( void ) const = 0;
00147     virtual bool operator==( const Type& type ) const = 0;
00148     bool operator!=( const Type& type ) const { return !operator==( type ); }
00149 
00150     AspectValue& aspectData( Aspect::Id id );
00151     const AspectValue& aspectData( Aspect::Id id ) const;
00152 
00153   protected:
00154 
00155     static void updateHash( unsigned long& h, unsigned long id )
00156     { h = 5 * h + id; }
00157 
00158     std::size_t elmNo_;
00159     mpu::String name_;
00160 
00161     void (*create_)( const Type*, void* );
00162     void (*cCreate_)( const Type*, void*, const void* );
00163     void (*destroy_)( const Type*, void* );
00164     void (*assign_)( const Type*, void*, const void* );
00165     void* (*newObj_)( const Type* );
00166     void* (*newCopy_)( const Type*, const void* );
00167     void (*delObj_)( const Type*, void* );
00168 
00169   private:
00170 
00171     std::size_t size_;
00172     std::size_t align_;
00173     Id id_;
00174     const std::type_info* typeId_;
00175 
00176     AspectVector aspects_;
00177 
00178     Type( const Type& );
00179     Type& operator=( const Type& );
00180   };
00181 
00182   inline const void*
00183   Type::getElmAddr( const void* obj, std::size_t idx ) const
00184   {
00185     return getElmAddr( const_cast<void*>(obj), idx );
00186   }
00187 
00188   inline AspectValue&
00189   Type::aspectData( Aspect::Id id )
00190   {
00191     return aspects_.data( id );
00192   }
00193 
00194   inline const AspectValue&
00195   Type::aspectData( Aspect::Id id ) const
00196   {
00197     return aspects_.data( id );
00198   }
00199 
00200   extern const Type& TypeVoid;
00201 
00202 } // namespace rfl
00203 #endif // RFL_TYPE_HH

Generated on Thu Dec 18 11:32:54 2003 for Reflection&ServiceLibrary by doxygen 1.3.2