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