Sierra Toolkit Version of the Day
CSet.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 #ifndef stk_util_util_CSet_hpp
00010 #define stk_util_util_CSet_hpp
00011 
00012 #include <typeinfo>
00013 #include <vector>
00014 
00015 namespace stk {
00016 
00017 //----------------------------------------------------------------------
00051 class CSet {
00052 public:
00053 
00055   template<class T> const T * get() const ;
00056 
00060   template<class T> const T * insert_with_delete( const T *);
00061 
00065   template<class T> const T * insert_no_delete( const T * );
00066 
00070   template<class T> bool remove( const T * );
00071 
00072   //--------------------------------
00073 
00074   ~CSet();
00075   CSet();
00076 
00077 private:
00078 
00079   typedef void (*DeleteFunction)(void *);
00080 
00081   typedef std::pair< const std::type_info * , DeleteFunction > Manager ;
00082 
00083   const void * p_get( const std::type_info & ) const ;
00084 
00085   const void * p_insert( const Manager & , const void * );
00086 
00087   bool p_remove( const std::type_info & , const void * );
00088 
00089   std::vector< Manager > m_manager ;
00090   std::vector< const void * > m_value ;
00091 
00092   CSet( const CSet & );
00093   CSet & operator = ( const CSet & );
00094 };
00095 
00096 } // namespace stk
00097 
00098 //----------------------------------------------------------------------
00099 //----------------------------------------------------------------------
00100 // Inlined template methods have casting.
00101 
00102 #ifndef DOXYGEN_COMPILE
00103 
00104 namespace stk {
00105 
00106 namespace {
00107 template<class T>
00108 void cset_member_delete( void * v ) { delete reinterpret_cast<T*>( v ); }
00109 }
00110 
00111 template<class T>
00112 inline
00113 const T * CSet::get() const
00114 { return (const T*) p_get( typeid(T) ); }
00115 
00116 template<class T>
00117 inline
00118 const T * CSet::insert_with_delete( const T * arg_value)
00119 {
00120   Manager m ;
00121   m.first = & typeid(T);
00122   m.second = & cset_member_delete<T> ;
00123 
00124   return (const T *) p_insert( m , arg_value );
00125 }
00126 
00127 template<class T>
00128 inline
00129 const T * CSet::insert_no_delete( const T * arg_value)
00130 {
00131   Manager m ;
00132   m.first = & typeid(T);
00133   m.second = 0 ;
00134 
00135   return (const T *) p_insert( m , arg_value );
00136 }
00137 
00138 template<class T>
00139 inline
00140 bool CSet::remove( const T * arg_value )
00141 { return p_remove( typeid(T) , arg_value ); }
00142 
00143 } // namespace stk
00144 
00145 #endif /* DOXYGEN_COMPILE */
00146 
00147 #endif // stk_util_util_CSet_hpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines