Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:13:13

0001 #ifndef FWCore_Utilities_typelookup_h
0002 #define FWCore_Utilities_typelookup_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     Utilities
0006 // Class  :     typelookup
0007 //
0008 /**\class typelookup typelookup.h FWCore/Utilities/interface/typelookup.h
0009 
0010  Description: Allow looking up a c++ type_info via its name
0011 
0012  Usage:
0013     This group of functions allow one to associate a string to a std::type_info.  In particular one
0014  can lookup an edm::TypeIDBase given a string.
0015  
0016  The system works by using the macros TYPELOOKUP_METHODS and DEFINE_TYPELOOKUP_REGISTRATION.
0017  TYPELOOKUP_METHODS(Tname): explicitly instantiates the template functions className classTypeInfo using
0018    the Tname value as both the C++ class and transforming it into the string name.
0019  
0020  DEFINE_TYPELOOKUP_REGISTRATION: registers the string name with the C++ class type.
0021  
0022  TYPELOOKUP_DATA_REG : sets both TYPELOOKUP_METHODS and DEFINE_TYPELOOKUP_REGISTRATION
0023 
0024  
0025  Example: You have a new data class called 'DummyData'.  Then to register that class with the system you
0026  place the lines
0027  
0028  #include "<where ever my class decleration lives>/interface/DummyData.h"
0029  
0030  TYPELOOKUP_DATA_REG(DummyData);
0031  
0032  into the file <where ever my class decleration lives>/src/T_EventSetup_DummyData.cc
0033  
0034  The actual name of the file that uses the 'TYPELOOKUP_DATA_REG' macro is not important.  The only important point
0035  the file that uses the 'TYPELOOKUP_DATA_REG' macro must be in the same library as the data class it is registering.
0036  
0037 */
0038 //
0039 // Original Author:  Chris Jones
0040 //         Created:  Wed Jan 20 14:26:21 CST 2010
0041 //
0042 
0043 // system include files
0044 #include <typeinfo>
0045 #include <utility>
0046 
0047 // user include files
0048 
0049 namespace edm {
0050   class TypeIDBase;
0051 
0052   namespace typelookup {
0053     /**Returns a std::type_info and a long lived const char* containing the name 
0054        associated with the string iClassName. If the string is not associated with a known 
0055        type then returns two null pointers */
0056     std::pair<const char*, const std::type_info*> findType(const char* iClassName);
0057 
0058     /**Returns the registered string (usually the class name) for the type T
0059        */
0060     template <typename T>
0061     const char* className();
0062 
0063     /**Returns the std::type_info for the class T.  This is done just by calling
0064        typeid(T).  So why bother? The call to typeid(T) requires one to include the
0065        header file which defines class T while the call to classTypeInfo<T>() does not.
0066        */
0067     template <typename T>
0068     const std::type_info& classTypeInfo();
0069 
0070     /**Used to create file static variables which register the string iTypeName to the C++ class
0071        type associated to the std::type_info.
0072        */
0073     class NameRegistrar {
0074     public:
0075       NameRegistrar(const char* iTypeName, const std::type_info& iInfo);
0076     };
0077 
0078   }  // namespace typelookup
0079 }  // namespace edm
0080 
0081 #define TYPELOOKUP_METHODS(Tname)                    \
0082   namespace edm {                                    \
0083     namespace typelookup {                           \
0084       template <>                                    \
0085       const char* className<Tname>() {               \
0086         return #Tname;                               \
0087       }                                              \
0088       template <>                                    \
0089       const std::type_info& classTypeInfo<Tname>() { \
0090         return typeid(Tname);                        \
0091       }                                              \
0092     }                                                \
0093   }
0094 
0095 #define EDM_TYPELOOKUP_SYM(x, y) EDM_TYPELOOKUP_SYM2(x, y)
0096 #define EDM_TYPELOOKUP_SYM2(x, y) x##y
0097 
0098 #define DEFINE_TYPELOOKUP_REGISTRATION(type)                                            \
0099   static const edm::typelookup::NameRegistrar EDM_TYPELOOKUP_SYM(s_register, __LINE__)( \
0100       edm::typelookup::className<type>(), typeid(type))
0101 
0102 #define TYPELOOKUP_DATA_REG(_dataclass_) \
0103   TYPELOOKUP_METHODS(_dataclass_)        \
0104   DEFINE_TYPELOOKUP_REGISTRATION(_dataclass_)
0105 
0106 #endif