Sierra Toolkit Version of the Day
PropertyBase.hpp
00001 /*------------------------------------------------------------------------*/
00002 /*                 Copyright 2010 Sandia Corporation.                     */
00003 /*  Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive   */
00004 /*  license for use of this work by or on behalf of the U.S. Government.  */
00005 /*  Export of this program may require a license from the                 */
00006 /*  United States Government.                                             */
00007 /*------------------------------------------------------------------------*/
00008 
00009 
00010 #ifndef stk_mesh_base_PropertyBase_hpp
00011 #define stk_mesh_base_PropertyBase_hpp
00012 
00013 #include <iosfwd>
00014 #include <string>
00015 #include <map>
00016 #include <vector>
00017 
00018 #include <stk_util/environment/ReportHandler.hpp>
00019 
00020 #include <stk_mesh/base/Types.hpp>
00021 
00022 namespace stk {
00023 namespace mesh {
00024 
00029 template<>
00030 class Property< void > {
00031 public:
00032 
00033   MetaData & mesh_meta_data() const { return m_meta_data ; }
00034 
00038   unsigned mesh_meta_data_ordinal() const { return m_meta_data_ordinal ; }
00039 
00041   const std::string & name() const { return m_name ; }
00042 
00044   template<class DataType> bool type_is() const
00045     { return m_type == typeid(DataType); }
00046 
00048   unsigned size() const { return m_size ; }
00049 
00051   template< typename DataType >
00052   Property< DataType > * property()
00053     {
00054       Property< DataType > * p = NULL ;
00055       if ( m_type == typeid(DataType) ) {
00056         p = static_cast< Property< DataType > * >( this );
00057       }
00058       return p ;
00059     }
00060 
00062   template< typename DataType >
00063   const Property< DataType > * property() const
00064     {
00065       const Property< DataType > * p = NULL ;
00066       if ( m_type == typeid(DataType) ) {
00067         p = static_cast< const Property< DataType > * >( this );
00068       }
00069       return p ;
00070     }
00071 
00072   //----------------------------------------
00073 
00074 #ifndef DOXYGEN_COMPILE
00075 
00076 protected:
00077 
00078   Property( MetaData & my_meta_data ,
00079             unsigned meta_data_ordinal ,
00080             const std::string & input_name ,
00081             const std::type_info & type ,
00082             unsigned n )
00083     : m_name( input_name ),
00084       m_meta_data( my_meta_data ),
00085       m_meta_data_ordinal( meta_data_ordinal ),
00086       m_type( type ), m_size( n ) {}
00087 
00088   virtual void add_property( unsigned ) = 0 ;
00089 
00090   virtual ~Property();
00091 
00092 private:
00093 
00097   MetaData & meta_data() const { return m_meta_data ; }
00098 
00099   const std::string      m_name ;       
00100   MetaData             & m_meta_data ;  
00101   const unsigned         m_meta_data_ordinal ;
00102   const std::type_info & m_type ;       
00103   const unsigned         m_size ;       
00104 
00105   Property();
00106   Property( const Property & );
00107   Property & operator = ( const Property & );
00108 
00109   friend class MetaData ;
00110 
00111 #endif /* DOXYGEN_COMPILE */
00112 };
00113 
00114 //----------------------------------------------------------------------
00115 
00120 template< typename DataType >
00121 class Property : public PropertyBase {
00122 #ifndef DOXYGEN_COMPILE
00123 private:
00124   friend class MetaData ;
00125 
00126   typedef std::map< unsigned , DataType > map_scalar ;
00127 
00128   map_scalar m_data_scalar ;
00129 
00130 protected:
00131 
00132   Property( MetaData & my_meta_data, unsigned meta_data_ordinal ,
00133             const std::string & input_name, unsigned input_size = 1 )
00134     : PropertyBase( my_meta_data, meta_data_ordinal ,
00135                     input_name, typeid(DataType), input_size ) {}
00136 
00137   virtual void add_property( unsigned key ) { m_data_scalar[ key ]; }
00138 
00139   virtual ~Property() {}
00140 
00141 #endif /* DOXYGEN_COMPILE */
00142 public:
00143 
00145   typedef DataType data_type ;
00146 
00148   virtual data_type * data( unsigned key )
00149   {
00150     const typename map_scalar::iterator i = m_data_scalar.find( key );
00151     return i != m_data_scalar.end() ? & (*i).second : (data_type*) NULL ;
00152   }
00153 
00155   virtual const data_type * data( unsigned key ) const
00156   {
00157     const typename map_scalar::const_iterator i = m_data_scalar.find( key );
00158     return i != m_data_scalar.end() ? & (*i).second : (data_type*) NULL ;
00159   }
00160 
00161 };
00162 
00163 #ifndef DOXYGEN_COMPILE
00164 
00165 template< typename DataType >
00166 class Property< std::vector< DataType > > : public Property<DataType> {
00167 private:
00168   friend class MetaData ;
00169 
00170   typedef std::map< unsigned , std::vector< DataType > > map_array ;
00171 
00172   map_array m_data_array ;
00173 
00174   void add_property( unsigned key )
00175     { m_data_array[ key ].resize( Property<void>::size() ); }
00176 
00177   ~Property() {}
00178   Property();
00179   Property( const Property & );
00180   Property & operator = ( const Property & );
00181 
00182 public:
00183 
00184   Property( MetaData & my_meta_data ,
00185             unsigned   meta_data_ordinal ,
00186             const std::string & name ,
00187             unsigned size )
00188     : Property<DataType>( my_meta_data, meta_data_ordinal, name, size ) {}
00189 
00190   typedef DataType data_type ;
00191 
00192   data_type * data( unsigned key )
00193   {
00194     const typename map_array::iterator i = m_data_array.find( key );
00195     return i != m_data_array.end() ? & (*i).second[0] : (data_type*) NULL ;
00196   }
00197 
00198   const data_type * data( unsigned key ) const
00199   {
00200     const typename map_array::const_iterator i = m_data_array.find( key );
00201     return i != m_data_array.end() ? & (*i).second[0] : (data_type*) NULL ;
00202   }
00203 };
00204 
00205 #endif /* DOXYGEN_COMPILE */
00206 
00207 
00208 } // namespace mesh
00209 } // namespace stk
00210 
00211 #endif // stk_mesh_base_PropertyBase_hpp
00212 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends