CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/L1TriggerConfig/GMTConfigProducers/interface/RecordHelper.h

Go to the documentation of this file.
00001 #ifndef CondTools_L1Trigger_RecordHelper_h
00002 #define CondTools_L1Trigger_RecordHelper_h
00003 // -*- C++ -*-
00004 //
00005 // Package:     L1Trigger
00006 // Class  :     RecordHelper
00007 // 
00016 #include <boost/type_traits.hpp>
00017 
00018 #include "RelationalAccess/ICursor.h"
00019 #include "CoralBase/AttributeList.h"
00020 #include "CoralBase/AttributeSpecification.h"
00021 #include "CoralBase/Attribute.h"
00022 
00023 
00025 std::string upcaseString(std::string aString);
00026 
00028 template <class TOutput> class FieldHandlerBase {
00029 public:
00030   typedef coral::AttributeList AttributeList;
00032   FieldHandlerBase(const std::string& name) : name_(name) { }
00033 
00035   const std::string& getName() { return name_ ; }
00036 
00039   virtual const std::string getColumnName() { return upcaseString(name_); }
00040 
00043   virtual void  extractValue(const AttributeList& src, TOutput& dest ) = 0;
00044 
00046   virtual ~FieldHandlerBase() { } 
00047 private:
00048   
00049   /* The (case sensitive!) name of the field. */
00050   std::string name_;
00051 };
00052 
00053 
00059 template <class TOutput, class TCField, class TDBField> class FieldHandler : public FieldHandlerBase<TOutput> {
00060  public:
00061   typedef coral::AttributeList AttributeList;
00062   typedef void (TOutput::*TSetMethod)(const TCField);
00063 
00064   FieldHandler(const std::string& fieldName, 
00065                TSetMethod setter) : FieldHandlerBase<TOutput>(fieldName), setter_(setter) 
00066   { }
00067 
00069   virtual void extractValue(const AttributeList& src, TOutput& dest) { 
00070 #ifdef RECORDHELPER_DEBUG
00071     std::cout << "Parsing field " << this->getName() << " with type " << typeid(TCField).name() ;
00072 #endif
00073     typedef typename boost::remove_cv<typename boost::remove_reference<TDBField>::type>::type TDBFieldT;
00074     const TDBFieldT & value = src[this->getColumnName()].template data< TDBFieldT >();
00075     ((dest).*setter_)(TCField(value));
00076 
00077 #ifdef RECORDHELPER_DEBUG
00078     std::cout << "=" << TCField(value) << std::endl ; 
00079 #endif
00080   }
00081 
00082  protected:
00085   TSetMethod setter_;
00086 };
00087 
00093 template <class TOutput, char FalseCharacter> class ASCIIBoolFieldHandler : public FieldHandler<TOutput, bool, char> 
00094 {
00095  public:
00096   typedef coral::AttributeList AttributeList;
00097   ASCIIBoolFieldHandler(const std::string& fieldName,
00098                         typename FieldHandler<TOutput,bool,char>::TSetMethod setter) 
00099     : FieldHandler<TOutput, bool, char>(fieldName, setter)
00100     {   }
00101 
00103   virtual void extractValue(const AttributeList& src, TOutput& dest)
00104   {
00105     char value = src[this->getColumnName()].template data<char>();
00106 #ifdef RECORDHELPER_DEBUG
00107     std::cout << " .. and "  << this->getColumnName() << " is (in integers) " << (int) value << std::endl;
00108 #endif
00109     ((dest).*(this->setter_))(value != FalseCharacter);
00110   }
00111 };
00112 
00113 
00121 class TStandardGroup;
00122 
00124 template <typename TOutput> struct Group {
00125   typedef TStandardGroup Type;
00126 };
00127 
00129 #define RH_ASSIGN_GROUP(TOutput, TGroup) template <> struct Group<TOutput> { \
00130   typedef TGroup Type;                                                       \
00131 };
00132 
00140 /* Generically: We don't mess with types and expect the field types to
00141    exactly match the database types (sadly, CORAL is rather anal about
00142    such things). */
00143 template <typename TOutput, 
00144           typename TGroup, 
00145           typename TCType> 
00146 struct GroupFieldHandler { 
00147   typedef FieldHandler<TOutput, TCType, TCType> Type;
00148 };
00149 
00150 
00151 template <class TOutput> class RecordHelper {
00152  public: 
00153   typedef coral::AttributeList AttributeList;
00155   typedef std::vector<FieldHandlerBase<TOutput>*> FieldVector;
00156 
00157   template <typename TField> void addField(const std::string& fieldName,
00158                                            void (TOutput::*setter)(const TField)) { 
00159 #ifdef RECORDHELPER_DEBUG
00160     std::cout << "Adding field " << fieldName << ", type = " << typeid(TField).name() << std::endl;    
00161 #endif
00162     this->fields_.push_back(new typename GroupFieldHandler<TOutput, typename Group<TOutput>::Type , TField>::Type(fieldName, setter));
00163 
00164   }
00165   
00168   virtual void extractRecord(const AttributeList& source, TOutput& dest) { 
00169     for(typename FieldVector::const_iterator it = fields_.begin(); 
00170         it != fields_.end() ; ++it) { 
00171       (*it)->extractValue(source, dest);
00172     }
00173   }
00174 
00176   virtual std::vector<std::string> getColumnList() {
00177     std::vector<std::string> colList;
00178     for(typename FieldVector::const_iterator it = fields_.begin();
00179         it != fields_.end() ; ++it) {
00180       colList.push_back((*it)->getColumnName());
00181     }
00182     
00183     return colList;
00184   }
00185 
00187   virtual ~RecordHelper() { 
00188     for(typename FieldVector::iterator it = fields_.begin();
00189         it < fields_.end() ; ++it) { 
00190       delete *it;
00191     }
00192   }
00193 protected:
00195   FieldVector fields_;
00196 };
00197 
00200 #define ADD_FIELD(HELPER, OUTPUT_NAME, FIELD_NAME) \
00201     HELPER.addField(#FIELD_NAME, &OUTPUT_NAME::set##FIELD_NAME);
00202 
00203 
00204 #endif