CMS 3D CMS Logo

/data/git/CMSSW_5_3_11_patch5/src/DataFormats/Common/interface/traits.h

Go to the documentation of this file.
00001 #ifndef DataFormats_Common_traits_h
00002 #define DataFormats_Common_traits_h
00003 
00004 /*----------------------------------------------------------------------
00005 
00006 Definition of traits templates used in the EDM.  
00007 
00008 $Id: traits.h,v 1.20 2008/05/16 01:07:23 wmtan Exp $
00009 
00010 ----------------------------------------------------------------------*/
00011 
00012 #include <deque>
00013 #include <limits>
00014 #include <list>
00015 #include <map>
00016 #include <set>
00017 #include <string>
00018 #include <utility>
00019 #include <vector>
00020 
00021 namespace edm
00022 {
00023   //------------------------------------------------------------
00024   //
00025   // The trait struct template key_traits<K> is used to carry
00026   // information relevant to the type K when used as a 'key' in
00027   // RefVector and its related classes and templates.
00028   //
00029   // The general case works only for integral types K; for more
00030   // 'esoteric' types, one must introduce an explicit specialization.
00031   // That specialization must initialize the static data member
00032   // 'value'.
00033 
00034   template <class K>
00035   struct key_traits
00036   {
00037     typedef K key_type;
00038     static const key_type value;
00039   };
00040 
00041   template <class K> 
00042   typename  key_traits<K>::key_type const
00043   key_traits<K>::value =
00044     std::numeric_limits<typename key_traits<K>::key_type>::max();
00045 
00046   // Partial specialization for std::pair
00047 
00048   template <class U, class V>
00049   struct key_traits<std::pair<U,V> >
00050   {
00051     typedef std::pair<U,V>  key_type;
00052     static const key_type value;
00053   };
00054 
00055   template <class U, class V>
00056   typename key_traits<std::pair<U,V> >::key_type const
00057   key_traits<std::pair<U,V> >::value = 
00058     std::make_pair(key_traits<U>::value, key_traits<V>::value);
00059 
00060   // If we ever need to support instantiations of std::basic_string
00061   // other than std::string, this is the place to do it.
00062 
00063   // For value, we make a 1-character long string that contains an
00064   // unprintable character; we are hoping nobody ever uses such a
00065   // string as a legal key.
00066   template <> 
00067   struct key_traits<std::string>
00068   {
00069     typedef std::string key_type;
00070     static const key_type value;
00071   };
00072 
00073 
00074   //------------------------------------------------------------
00075   //
00076   // DoNotSortUponInsertion is a base class. Derive your own class X
00077   // from DoNotSortUponInsertion when: 
00078   //
00079   // 1. You want to use DetSetVector<X> as an EDProduct, but
00080   //
00081   // 2. You do *not* want the Event::put member template to cause the
00082   // DetSet<X> instances within the DetSetVector<X> to be sorted.
00083   //
00084   // DoNotSortUponInsertion has no behavior; it is used at compile
00085   // time to influence the behavior of Event::put.
00086   //
00087   // Usage:
00088   //    class MyClass : public edm::DoNotSortUponInsertion { ... }
00089   //
00090   struct DoNotSortUponInsertion { };
00091 
00092   //------------------------------------------------------------
00093   //
00094   // DoNotRecordParents is a base class. Derive your own (EDProduct)
00095   // class X from DoNotRecordParents when your class already keeps all
00096   // data that are relevant to parentage internally, and the
00097   // information kept by the event model would thus be redundant.
00098   //
00099   // DoNotRecordParents has no behavior; it is used at compile time to
00100   // influence the behavior of Event::put.
00101   //
00102   // Usage:
00103   //    class MyClass : public edm::DoNotRecordParents { ... }
00104   struct DoNotRecordParents { };
00105 
00106   // Other is a base class. NEVER USE IT. It is for the
00107   // core of the event model only.
00108   struct Other { };
00109 
00110   //------------------------------------------------------------
00111   //
00112   // The trait struct template has_fillView<T> is used to
00113   // indicate whether or not the type T has a member function
00114   //
00115   //      void T::fillView(std::vector<void const*>&) const
00116   //
00117   // We assume the 'general case' for T is to not support fillView.
00118   // Classes which do support fillView must specialize this trait.
00119   //
00120   //------------------------------------------------------------
00121 
00122   template <class T>
00123   struct has_fillView
00124   {
00125     static bool const value = false;
00126   };
00127 
00128   template <class T, class A>
00129   struct has_fillView<std::vector<T,A> >
00130   {
00131     static bool const value = true;
00132   };
00133 
00134   template <class A>
00135   struct has_fillView<std::vector<bool,A> >
00136   {
00137     static bool const value = false;
00138   };
00139 
00140   template <class T, class A>
00141   struct has_fillView<std::list<T,A> >
00142   {
00143     static bool const value = true;
00144   };
00145 
00146   template <class T, class A>
00147   struct has_fillView<std::deque<T,A> >
00148   {
00149     static bool const value = true;
00150   };
00151 
00152   template <class T, class A>
00153   struct has_fillView<std::set<T,A> >
00154   {
00155     static bool const value = true;
00156   };
00157 
00158 
00159   //------------------------------------------------------------
00160   //
00161   // The trait struct template has_setPtr<T> is used to
00162   // indicate whether or not the type T has a member function
00163   //
00164   //      void T::setPtr(const std::type_info&, void const*&) const
00165   //
00166   // We assume the 'general case' for T is to not support setPtr.
00167   // Classes which do support setPtr must specialize this trait.
00168   //
00169   //------------------------------------------------------------
00170   
00171   template <class T>
00172     struct has_setPtr
00173   {
00174     static bool const value = false;
00175   };
00176   
00177   template <class T, class A>
00178     struct has_setPtr<std::vector<T,A> >
00179   {
00180     static bool const value = true;
00181   };
00182   
00183   template <class A>
00184     struct has_setPtr<std::vector<bool,A> >
00185   {
00186     static bool const value = false;
00187   };
00188   
00189   template <class T, class A>
00190     struct has_setPtr<std::list<T,A> >
00191   {
00192     static bool const value = true;
00193   };
00194   
00195   template <class T, class A>
00196     struct has_setPtr<std::deque<T,A> >
00197   {
00198     static bool const value = true;
00199   };
00200   
00201   template <class T, class A>
00202     struct has_setPtr<std::set<T,A> >
00203   {
00204     static bool const value = true;
00205   };
00206 }
00207 
00208 #endif