Property.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_Property_hpp
00011 #define stk_mesh_Property_hpp
00012 
00013 //----------------------------------------------------------------------
00014 
00015 #include <iosfwd>
00016 #include <string>
00017 #include <map>
00018 #include <vector>
00019 
00020 #include <stk_mesh/base/Types.hpp>
00021 
00022 //----------------------------------------------------------------------
00023 
00024 namespace stk {
00025 namespace mesh {
00026 
00032 std::ostream & operator << ( std::ostream & , const PropertyBase & );
00033 
00035 std::ostream & print( std::ostream & ,
00036                       const char * const , const PropertyBase & );
00037 
00039 void property_data_throw( const PropertyBase & , const Part & );
00040 
00042 template< typename property_type >
00043 inline
00044 const typename property_type::data_type *
00045 property_data( const property_type & prop , const Part & part )
00046 {
00047   if ( & prop.mesh_meta_data() != & part.mesh_meta_data() ) {
00048     property_data_throw( prop , part );
00049   }
00050   return prop.data( part.mesh_meta_data_ordinal() );
00051 }
00052 
00054 template< typename property_type >
00055 inline
00056 typename property_type::data_type *
00057 property_data( property_type & prop , const Part & part )
00058 {
00059   if ( & prop.mesh_meta_data() != & part.mesh_meta_data() ) {
00060     property_data_throw( prop , part );
00061   }
00062   return prop.data( part.mesh_meta_data_ordinal() );
00063 }
00064 
00066 //----------------------------------------------------------------------
00067 //----------------------------------------------------------------------
00072 template<>
00073 class Property< void > {
00074 public:
00078   MetaData & mesh_meta_data() const { return m_meta_data ; }
00079 
00083   unsigned mesh_meta_data_ordinal() const { return m_meta_data_ordinal ; }
00084 
00086   const std::string & name() const { return m_name ; }
00087 
00089   template<class DataType> bool type_is() const
00090     { return m_type == typeid(DataType); }
00091 
00093   unsigned size() const { return m_size ; }
00094 
00096   template< typename DataType >
00097   Property< DataType > * property()
00098     {
00099       Property< DataType > * p = NULL ;
00100       if ( m_type == typeid(DataType) ) {
00101         p = static_cast< Property< DataType > * >( this ); 
00102       }
00103       return p ;
00104     }
00105 
00107   template< typename DataType >
00108   const Property< DataType > * property() const
00109     {
00110       const Property< DataType > * p = NULL ;
00111       if ( m_type == typeid(DataType) ) {
00112         p = static_cast< const Property< DataType > * >( this ); 
00113       }
00114       return p ;
00115     }
00116 
00117   //----------------------------------------
00118 
00119 #ifndef DOXYGEN_COMPILE
00120 
00121 protected:
00122 
00123   Property( MetaData & meta_data ,
00124             unsigned meta_data_ordinal ,
00125             const std::string & name ,
00126             const std::type_info & type ,
00127             unsigned n )
00128     : m_name( name ),
00129       m_meta_data( meta_data ),
00130       m_meta_data_ordinal( meta_data_ordinal ),
00131       m_type( type ), m_size( n ) {}
00132 
00133   virtual void add_property( unsigned ) = 0 ;
00134 
00135   virtual ~Property();
00136 
00137 private:
00138 
00139   const std::string      m_name ;       
00140   MetaData             & m_meta_data ;  
00141   const unsigned         m_meta_data_ordinal ;
00142   const std::type_info & m_type ;       
00143   const unsigned         m_size ;       
00144 
00145   Property();
00146   Property( const Property & );
00147   Property & operator = ( const Property & );
00148  
00149   friend class MetaData ;
00150   friend class UnitTestMetaData ;
00151 
00152 #endif /* DOXYGEN_COMPILE */
00153 };
00154 
00155 //----------------------------------------------------------------------
00156 
00161 template< typename DataType >
00162 class Property : public PropertyBase {
00163 #ifndef DOXYGEN_COMPILE
00164 private:
00165   friend class MetaData ;
00166 
00167   typedef std::map< unsigned , DataType > map_scalar ;
00168 
00169   map_scalar m_data_scalar ;
00170 
00171 protected:
00172 
00173   Property( MetaData & meta_data, unsigned meta_data_ordinal ,
00174             const std::string & name, unsigned size = 1 )
00175     : PropertyBase( meta_data, meta_data_ordinal ,
00176                     name, typeid(DataType), size ) {}
00177 
00178   virtual void add_property( unsigned key ) { m_data_scalar[ key ]; }
00179 
00180   virtual ~Property() {}
00181 
00182 #endif /* DOXYGEN_COMPILE */
00183 public:
00184 
00186   typedef DataType data_type ;
00187 
00189   virtual data_type * data( unsigned key )
00190   {
00191     const typename map_scalar::iterator i = m_data_scalar.find( key );
00192     return i != m_data_scalar.end() ? & (*i).second : (data_type*) NULL ;
00193   }
00194 
00196   virtual const data_type * data( unsigned key ) const
00197   {
00198     const typename map_scalar::const_iterator i = m_data_scalar.find( key );
00199     return i != m_data_scalar.end() ? & (*i).second : (data_type*) NULL ;
00200   }
00201 
00202 };
00203 
00204 #ifndef DOXYGEN_COMPILE
00205 
00206 template< typename DataType >
00207 class Property< std::vector< DataType > > : public Property<DataType> {
00208 private:
00209   friend class MetaData ;
00210 
00211   typedef std::map< unsigned , std::vector< DataType > > map_array ;
00212 
00213   map_array m_data_array ;
00214 
00215   void add_property( unsigned key )
00216     { m_data_array[ key ].resize( Property<void>::size() ); }
00217 
00218   ~Property() {}
00219   Property();
00220   Property( const Property & );
00221   Property & operator = ( const Property & );
00222 
00223 public:
00224 
00225   Property( MetaData & meta_data ,
00226             unsigned   meta_data_ordinal ,
00227             const std::string & name ,
00228             unsigned size )
00229     : Property<DataType>( meta_data, meta_data_ordinal, name, size ) {}
00230 
00231   typedef DataType data_type ;
00232 
00233   data_type * data( unsigned key )
00234   {
00235     const typename map_array::iterator i = m_data_array.find( key );
00236     return i != m_data_array.end() ? & (*i).second[0] : (data_type*) NULL ;
00237   }
00238 
00239   const data_type * data( unsigned key ) const
00240   {
00241     const typename map_array::const_iterator i = m_data_array.find( key );
00242     return i != m_data_array.end() ? & (*i).second[0] : (data_type*) NULL ;
00243   }
00244 };
00245 
00246 #endif /* DOXYGEN_COMPILE */
00247 
00248 //----------------------------------------------------------------------
00249 //----------------------------------------------------------------------
00250 
00251 } // namespace mesh
00252 } // namespace stk
00253 
00254 #endif /* stk_mesh_Property_hpp */
00255 

Generated on Tue Jul 13 09:27:32 2010 for Sierra Toolkit by  doxygen 1.4.7