Sierra Toolkit  Version of the Day
PropertyBase.hpp
1 /*------------------------------------------------------------------------*/
2 /* Copyright 2010 Sandia Corporation. */
3 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */
4 /* license for use of this work by or on behalf of the U.S. Government. */
5 /* Export of this program may require a license from the */
6 /* United States Government. */
7 /*------------------------------------------------------------------------*/
8 
9 
10 #ifndef stk_mesh_base_PropertyBase_hpp
11 #define stk_mesh_base_PropertyBase_hpp
12 
13 #include <iosfwd>
14 #include <string>
15 #include <map>
16 #include <vector>
17 
18 #include <stk_util/environment/ReportHandler.hpp>
19 
20 #include <stk_mesh/base/Types.hpp>
21 
22 namespace stk_classic {
23 namespace mesh {
24 
29 template<>
30 class Property< void > {
31 public:
32 
33  MetaData & mesh_meta_data() const { return m_meta_data ; }
34 
38  unsigned mesh_meta_data_ordinal() const { return m_meta_data_ordinal ; }
39 
41  const std::string & name() const { return m_name ; }
42 
44  template<class DataType> bool type_is() const
45  { return m_type == typeid(DataType); }
46 
48  unsigned size() const { return m_size ; }
49 
51  template< typename DataType >
53  {
54  Property< DataType > * p = NULL ;
55  if ( m_type == typeid(DataType) ) {
56  p = static_cast< Property< DataType > * >( this );
57  }
58  return p ;
59  }
60 
62  template< typename DataType >
64  {
65  const Property< DataType > * p = NULL ;
66  if ( m_type == typeid(DataType) ) {
67  p = static_cast< const Property< DataType > * >( this );
68  }
69  return p ;
70  }
71 
72  //----------------------------------------
73 
74 #ifndef DOXYGEN_COMPILE
75 
76 protected:
77 
78  Property( MetaData & my_meta_data ,
79  unsigned meta_data_ordinal ,
80  const std::string & input_name ,
81  const std::type_info & type ,
82  unsigned n )
83  : m_name( input_name ),
84  m_meta_data( my_meta_data ),
85  m_meta_data_ordinal( meta_data_ordinal ),
86  m_type( type ), m_size( n ) {}
87 
88  virtual void add_property( unsigned ) = 0 ;
89 
90  virtual ~Property();
91 
92 private:
93 
97  MetaData & meta_data() const { return m_meta_data ; }
98 
99  const std::string m_name ;
100  MetaData & m_meta_data ;
101  const unsigned m_meta_data_ordinal ;
102  const std::type_info & m_type ;
103  const unsigned m_size ;
104 
105  Property();
106  Property( const Property & );
107  Property & operator = ( const Property & );
108 
109  friend class MetaData ;
110 
111 #endif /* DOXYGEN_COMPILE */
112 };
113 
114 //----------------------------------------------------------------------
115 
120 template< typename DataType >
121 class Property : public PropertyBase {
122 #ifndef DOXYGEN_COMPILE
123 private:
124  friend class MetaData ;
125 
126  typedef std::map< unsigned , DataType > map_scalar ;
127 
128  map_scalar m_data_scalar ;
129 
130 protected:
131 
132  Property( MetaData & my_meta_data, unsigned meta_data_ordinal ,
133  const std::string & input_name, unsigned input_size = 1 )
134  : PropertyBase( my_meta_data, meta_data_ordinal ,
135  input_name, typeid(DataType), input_size ) {}
136 
137  virtual void add_property( unsigned key ) { m_data_scalar[ key ]; }
138 
139  virtual ~Property() {}
140 
141 #endif /* DOXYGEN_COMPILE */
142 public:
143 
145  typedef DataType data_type ;
146 
148  virtual data_type * data( unsigned key )
149  {
150  const typename map_scalar::iterator i = m_data_scalar.find( key );
151  return i != m_data_scalar.end() ? & (*i).second : (data_type*) NULL ;
152  }
153 
155  virtual const data_type * data( unsigned key ) const
156  {
157  const typename map_scalar::const_iterator i = m_data_scalar.find( key );
158  return i != m_data_scalar.end() ? & (*i).second : (data_type*) NULL ;
159  }
160 
161 };
162 
163 #ifndef DOXYGEN_COMPILE
164 
165 template< typename DataType >
166 class Property< std::vector< DataType > > : public Property<DataType> {
167 private:
168  friend class MetaData ;
169 
170  typedef std::map< unsigned , std::vector< DataType > > map_array ;
171 
172  map_array m_data_array ;
173 
174  void add_property( unsigned key )
175  { m_data_array[ key ].resize( Property<void>::size() ); }
176 
177  ~Property() {}
178  Property();
179  Property( const Property & );
180  Property & operator = ( const Property & );
181 
182 public:
183 
184  Property( MetaData & my_meta_data ,
185  unsigned meta_data_ordinal ,
186  const std::string & name ,
187  unsigned size )
188  : Property<DataType>( my_meta_data, meta_data_ordinal, name, size ) {}
189 
190  typedef DataType data_type ;
191 
192  data_type * data( unsigned key )
193  {
194  const typename map_array::iterator i = m_data_array.find( key );
195  return i != m_data_array.end() ? & (*i).second[0] : (data_type*) NULL ;
196  }
197 
198  const data_type * data( unsigned key ) const
199  {
200  const typename map_array::const_iterator i = m_data_array.find( key );
201  return i != m_data_array.end() ? & (*i).second[0] : (data_type*) NULL ;
202  }
203 };
204 
205 #endif /* DOXYGEN_COMPILE */
206 
207 
208 } // namespace mesh
209 } // namespace stk_classic
210 
211 #endif // stk_mesh_base_PropertyBase_hpp
212 
const Property< DataType > * property() const
Type-checked cast to property with members of the given type.
Property with defined data type and multi-dimensions (if any)
virtual data_type * data(unsigned key)
Access the properties&#39; members.
const std::string & name() const
Application-defined text name of this property.
The manager of an integrated collection of parts and fields.
Definition: MetaData.hpp:56
Property< DataType > * property()
Type-checked cast to property with members of the given type.
DataType data_type
Type of the properties&#39; members.
unsigned size() const
Query number of members, if an array.
Sierra Toolkit.
bool type_is() const
Query if the scalar type is DataType.
virtual const data_type * data(unsigned key) const
Access the properties&#39; members.
unsigned mesh_meta_data_ordinal() const
Internally generated ordinal of this property that is unique within the owning meta data manager...
Property base class with an anonymous data type and anonymous multi-dimension.