CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RecordHelper.h
Go to the documentation of this file.
1 #ifndef CondTools_L1Trigger_RecordHelper_h
2 #define CondTools_L1Trigger_RecordHelper_h
3 // -*- C++ -*-
4 //
5 // Package: L1Trigger
6 // Class : RecordHelper
7 //
16 #include <boost/type_traits.hpp>
17 
18 #include "RelationalAccess/ICursor.h"
19 #include "CoralBase/AttributeList.h"
20 #include "CoralBase/AttributeSpecification.h"
21 #include "CoralBase/Attribute.h"
22 
23 
25 std::string upcaseString(std::string aString);
26 
28 template <class TOutput> class FieldHandlerBase {
29 public:
30  typedef coral::AttributeList AttributeList;
32  FieldHandlerBase(const std::string& name) : name_(name) { }
33 
35  const std::string& getName() { return name_ ; }
36 
39  virtual const std::string getColumnName() { return upcaseString(name_); }
40 
43  virtual void extractValue(const AttributeList& src, TOutput& dest ) = 0;
44 
46  virtual ~FieldHandlerBase() { }
47 private:
48 
49  /* The (case sensitive!) name of the field. */
50  std::string name_;
51 };
52 
53 
59 template <class TOutput, class TCField, class TDBField> class FieldHandler : public FieldHandlerBase<TOutput> {
60  public:
61  typedef coral::AttributeList AttributeList;
62  typedef void (TOutput::*TSetMethod)(const TCField);
63 
64  FieldHandler(const std::string& fieldName,
65  TSetMethod setter) : FieldHandlerBase<TOutput>(fieldName), setter_(setter)
66  { }
67 
69  virtual void extractValue(const AttributeList& src, TOutput& dest) {
70 #ifdef RECORDHELPER_DEBUG
71  std::cout << "Parsing field " << this->getName() << " with type " << typeid(TCField).name() ;
72 #endif
74  const TDBFieldT & value = src[this->getColumnName()].template data< TDBFieldT >();
75  ((dest).*setter_)(TCField(value));
76 
77 #ifdef RECORDHELPER_DEBUG
78  std::cout << "=" << TCField(value) << std::endl ;
79 #endif
80  }
81 
82  protected:
86 };
87 
93 template <class TOutput, char FalseCharacter> class ASCIIBoolFieldHandler : public FieldHandler<TOutput, bool, char>
94 {
95  public:
96  typedef coral::AttributeList AttributeList;
97  ASCIIBoolFieldHandler(const std::string& fieldName,
99  : FieldHandler<TOutput, bool, char>(fieldName, setter)
100  { }
101 
103  virtual void extractValue(const AttributeList& src, TOutput& dest)
104  {
105  char value = src[this->getColumnName()].template data<char>();
106 #ifdef RECORDHELPER_DEBUG
107  std::cout << " .. and " << this->getColumnName() << " is (in integers) " << (int) value << std::endl;
108 #endif
109  ((dest).*(this->setter_))(value != FalseCharacter);
110  }
111 };
112 
113 
121 class TStandardGroup;
122 
124 template <typename TOutput> struct Group {
125  typedef TStandardGroup Type;
126 };
127 
129 #define RH_ASSIGN_GROUP(TOutput, TGroup) template <> struct Group<TOutput> { \
130  typedef TGroup Type; \
131 };
132 
140 /* Generically: We don't mess with types and expect the field types to
141  exactly match the database types (sadly, CORAL is rather anal about
142  such things). */
143 template <typename TOutput,
144  typename TGroup,
145  typename TCType>
148 };
149 
150 
151 template <class TOutput> class RecordHelper {
152  public:
153  typedef coral::AttributeList AttributeList;
155  typedef std::vector<FieldHandlerBase<TOutput>*> FieldVector;
156 
157  template <typename TField> void addField(const std::string& fieldName,
158  void (TOutput::*setter)(const TField)) {
159 #ifdef RECORDHELPER_DEBUG
160  std::cout << "Adding field " << fieldName << ", type = " << typeid(TField).name() << std::endl;
161 #endif
162  this->fields_.push_back(new typename GroupFieldHandler<TOutput, typename Group<TOutput>::Type , TField>::Type(fieldName, setter));
163 
164  }
165 
168  virtual void extractRecord(const AttributeList& source, TOutput& dest) {
169  for(typename FieldVector::const_iterator it = fields_.begin();
170  it != fields_.end() ; ++it) {
171  (*it)->extractValue(source, dest);
172  }
173  }
174 
176  virtual std::vector<std::string> getColumnList() {
177  std::vector<std::string> colList;
178  for(typename FieldVector::const_iterator it = fields_.begin();
179  it != fields_.end() ; ++it) {
180  colList.push_back((*it)->getColumnName());
181  }
182 
183  return colList;
184  }
185 
187  virtual ~RecordHelper() {
188  for(typename FieldVector::iterator it = fields_.begin();
189  it < fields_.end() ; ++it) {
190  delete *it;
191  }
192  }
193 protected:
196 };
197 
200 #define ADD_FIELD(HELPER, OUTPUT_NAME, FIELD_NAME) \
201  HELPER.addField(#FIELD_NAME, &OUTPUT_NAME::set##FIELD_NAME);
202 
203 
204 #endif
type
Definition: HCALResponse.h:22
virtual void extractRecord(const AttributeList &source, TOutput &dest)
Definition: RecordHelper.h:168
std::vector< FieldHandlerBase< TOutput > * > FieldVector
Definition: RecordHelper.h:155
void addField(const std::string &fieldName, void(TOutput::*setter)(const TField))
Definition: RecordHelper.h:157
void(TOutput::* TSetMethod)(const TCField)
Definition: RecordHelper.h:62
virtual void extractValue(const AttributeList &src, TOutput &dest)=0
virtual ~RecordHelper()
Definition: RecordHelper.h:187
std::string upcaseString(std::string aString)
Definition: RecordHelper.cc:8
FieldHandler(const std::string &fieldName, TSetMethod setter)
Definition: RecordHelper.h:64
FieldHandlerBase(const std::string &name)
Definition: RecordHelper.h:32
virtual void extractValue(const AttributeList &src, TOutput &dest)
Definition: RecordHelper.h:103
const std::string & getName()
Definition: RecordHelper.h:35
virtual const std::string getColumnName()
Definition: RecordHelper.h:39
FieldVector fields_
Definition: RecordHelper.h:195
std::string name_
Definition: RecordHelper.h:50
virtual ~FieldHandlerBase()
Definition: RecordHelper.h:46
TSetMethod setter_
Definition: RecordHelper.h:85
coral::AttributeList AttributeList
Definition: RecordHelper.h:30
FieldHandler< TOutput, TCType, TCType > Type
Definition: RecordHelper.h:147
coral::AttributeList AttributeList
Definition: RecordHelper.h:61
ASCIIBoolFieldHandler(const std::string &fieldName, typename FieldHandler< TOutput, bool, char >::TSetMethod setter)
Definition: RecordHelper.h:97
coral::AttributeList AttributeList
Definition: RecordHelper.h:96
virtual std::vector< std::string > getColumnList()
Definition: RecordHelper.h:176
tuple cout
Definition: gather_cfg.py:121
virtual void extractValue(const AttributeList &src, TOutput &dest)
Definition: RecordHelper.h:69
coral::AttributeList AttributeList
Definition: RecordHelper.h:153
TStandardGroup Type
Definition: RecordHelper.h:125