phdMesh  Version of the Day
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Groups
CSet.hpp
1 /*------------------------------------------------------------------------*/
2 /* phdMesh : Parallel Heterogneous Dynamic unstructured Mesh */
3 /* Copyright (2007) Sandia Corporation */
4 /* */
5 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */
6 /* license for use of this work by or on behalf of the U.S. Government. */
7 /* */
8 /* This library is free software; you can redistribute it and/or modify */
9 /* it under the terms of the GNU Lesser General Public License as */
10 /* published by the Free Software Foundation; either version 2.1 of the */
11 /* License, or (at your option) any later version. */
12 /* */
13 /* This library is distributed in the hope that it will be useful, */
14 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
15 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
16 /* Lesser General Public License for more details. */
17 /* */
18 /* You should have received a copy of the GNU Lesser General Public */
19 /* License along with this library; if not, write to the Free Software */
20 /* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 */
21 /* USA */
22 /*------------------------------------------------------------------------*/
28 #ifndef util_CSet_hpp
29 #define util_CSet_hpp
30 
31 #include <typeinfo>
32 #include <vector>
33 
34 namespace phdmesh {
35 
36 //----------------------------------------------------------------------
40 class CSet {
41 public:
42 
46  template<class T> const T * get() const ;
47 
62  template<class T>
63  const T * insert_with_delete( const T * );
64 
79  template<class T>
80  const T * insert_no_delete( const T * );
81 
86  template<class T> bool remove( const T * );
87 
88  //--------------------------------
89 
90  ~CSet();
91  CSet();
92 
93 private:
94 
95  typedef void (*DeleteFunction)(void *);
96 
97  typedef std::pair< const std::type_info * , DeleteFunction > Manager ;
98 
99  const void * p_get( const std::type_info & ) const ;
100 
101  const void * p_insert( const Manager & , const void * );
102 
103  bool p_remove( const std::type_info & , const void * );
104 
105  std::vector< Manager > m_manager ;
106  std::vector< const void * > m_value ;
107 
108  CSet( const CSet & );
109  CSet & operator = ( const CSet & );
110 };
111 
112 }
113 
114 //----------------------------------------------------------------------
115 //----------------------------------------------------------------------
116 
117 #ifndef DOXYGEN_COMPILE
118 
119 // Inlined template methods have casting.
120 
121 namespace phdmesh {
122 
123 namespace {
124 template<class T>
125 void cset_member_delete( void * v ) { delete reinterpret_cast<T*>( v ); }
126 }
127 
128 template<class T>
129 inline
130 const T * CSet::get() const
131 { return (const T*) p_get( typeid(T) ); }
132 
133 template<class T>
134 inline
135 const T * CSet::insert_with_delete( const T * arg_value )
136 {
137  Manager m ;
138  m.first = & typeid(T);
139  m.second = & cset_member_delete<T> ;
140  return (const T *) p_insert( m , arg_value );
141 }
142 
143 template<class T>
144 inline
145 const T * CSet::insert_no_delete( const T * arg_value )
146 {
147  Manager m ;
148  m.first = & typeid(T);
149  m.second = NULL ;
150  return (const T *) p_insert( m , arg_value );
151 }
152 
153 template<class T>
154 inline
155 bool CSet::remove( const T * arg_value )
156 { return p_remove( typeid(T) , arg_value ); }
157 
158 } // namespace phdmesh
159 
160 #endif /* DOXYGEN_COMPILE */
161 
162 #endif /* util_CSet_hpp */
163 
164 
const T * insert_no_delete(const T *)
Insert a member of a given type T but never invoke the delete operator.
const T * get() const
Get member conforming to the given type T . Return NULL if there is no member of that type...
const T * insert_with_delete(const T *)
Insert a member of a given type T and invoke the delete operator on destruction.
bool remove(const T *)
Remove a member of the given type without deleting it. The caller assumes responsibility for the remo...
Set of entities of arbitrary types.
Definition: CSet.hpp:40