File indexing completed on 2023-03-17 11:14:13
0001 #ifndef CondTools_L1Trigger_RecordHelper_h
0002 #define CondTools_L1Trigger_RecordHelper_h
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #include <boost/type_traits.hpp>
0017
0018 #include "RelationalAccess/ICursor.h"
0019 #include "CoralBase/AttributeList.h"
0020 #include "CoralBase/AttributeSpecification.h"
0021 #include "CoralBase/Attribute.h"
0022
0023
0024 std::string upcaseString(std::string aString);
0025
0026
0027 template <class TOutput>
0028 class FieldHandlerBase {
0029 public:
0030 typedef coral::AttributeList AttributeList;
0031
0032 FieldHandlerBase(const std::string& name) : name_(name) {}
0033
0034
0035 const std::string& getName() { return name_; }
0036
0037
0038
0039 virtual const std::string getColumnName() { return upcaseString(name_); }
0040
0041
0042
0043 virtual void extractValue(const AttributeList& src, TOutput& dest) = 0;
0044
0045
0046 virtual ~FieldHandlerBase() {}
0047
0048 private:
0049
0050 std::string name_;
0051 };
0052
0053
0054
0055
0056
0057
0058 template <class TOutput, class TCField, class TDBField>
0059 class FieldHandler : public FieldHandlerBase<TOutput> {
0060 public:
0061 typedef coral::AttributeList AttributeList;
0062 typedef void (TOutput::*TSetMethod)(const TCField);
0063
0064 FieldHandler(const std::string& fieldName, TSetMethod setter)
0065 : FieldHandlerBase<TOutput>(fieldName), setter_(setter) {}
0066
0067
0068 void extractValue(const AttributeList& src, TOutput& dest) override {
0069 #ifdef RECORDHELPER_DEBUG
0070 std::cout << "Parsing field " << this->getName() << " with type " << typeid(TCField).name();
0071 #endif
0072 typedef typename boost::remove_cv<typename boost::remove_reference<TDBField>::type>::type TDBFieldT;
0073 const TDBFieldT& value = src[this->getColumnName()].template data<TDBFieldT>();
0074 ((dest).*setter_)(TCField(value));
0075
0076 #ifdef RECORDHELPER_DEBUG
0077 std::cout << "=" << TCField(value) << std::endl;
0078 #endif
0079 }
0080
0081 protected:
0082
0083
0084 TSetMethod setter_;
0085 };
0086
0087
0088
0089
0090
0091
0092 template <class TOutput, char FalseCharacter>
0093 class ASCIIBoolFieldHandler : public FieldHandler<TOutput, bool, char> {
0094 public:
0095 typedef coral::AttributeList AttributeList;
0096 ASCIIBoolFieldHandler(const std::string& fieldName, typename FieldHandler<TOutput, bool, char>::TSetMethod setter)
0097 : FieldHandler<TOutput, bool, char>(fieldName, setter) {}
0098
0099
0100 void extractValue(const AttributeList& src, TOutput& dest) override {
0101 char value = src[this->getColumnName()].template data<char>();
0102 #ifdef RECORDHELPER_DEBUG
0103 std::cout << " .. and " << this->getColumnName() << " is (in integers) " << (int)value << std::endl;
0104 #endif
0105 ((dest).*(this->setter_))(value != FalseCharacter);
0106 }
0107 };
0108
0109
0110
0111
0112
0113
0114
0115 class TStandardGroup;
0116
0117
0118 template <typename TOutput>
0119 struct Group {
0120 typedef TStandardGroup Type;
0121 };
0122
0123
0124 #define RH_ASSIGN_GROUP(TOutput, TGroup) \
0125 template <> \
0126 struct Group<TOutput> { \
0127 typedef TGroup Type; \
0128 };
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139 template <typename TOutput, typename TGroup, typename TCType>
0140 struct GroupFieldHandler {
0141 typedef FieldHandler<TOutput, TCType, TCType> Type;
0142 };
0143
0144 template <class TOutput>
0145 class RecordHelper {
0146 public:
0147 typedef coral::AttributeList AttributeList;
0148
0149 typedef std::vector<FieldHandlerBase<TOutput>*> FieldVector;
0150
0151 template <typename TField>
0152 void addField(const std::string& fieldName, void (TOutput::*setter)(const TField)) {
0153 #ifdef RECORDHELPER_DEBUG
0154 std::cout << "Adding field " << fieldName << ", type = " << typeid(TField).name() << std::endl;
0155 #endif
0156 this->fields_.push_back(
0157 new typename GroupFieldHandler<TOutput, typename Group<TOutput>::Type, TField>::Type(fieldName, setter));
0158 }
0159
0160
0161
0162 virtual void extractRecord(const AttributeList& source, TOutput& dest) {
0163 for (typename FieldVector::const_iterator it = fields_.begin(); it != fields_.end(); ++it) {
0164 (*it)->extractValue(source, dest);
0165 }
0166 }
0167
0168
0169 virtual std::vector<std::string> getColumnList() {
0170 std::vector<std::string> colList;
0171 for (typename FieldVector::const_iterator it = fields_.begin(); it != fields_.end(); ++it) {
0172 colList.push_back((*it)->getColumnName());
0173 }
0174
0175 return colList;
0176 }
0177
0178
0179 virtual ~RecordHelper() {
0180 for (typename FieldVector::iterator it = fields_.begin(); it < fields_.end(); ++it) {
0181 delete *it;
0182 }
0183 }
0184
0185 protected:
0186
0187 FieldVector fields_;
0188 };
0189
0190
0191
0192 #define ADD_FIELD(HELPER, OUTPUT_NAME, FIELD_NAME) HELPER.addField(#FIELD_NAME, &OUTPUT_NAME::set##FIELD_NAME);
0193
0194 #endif