File indexing completed on 2024-04-06 11:56:17
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 #ifndef GENERS_ABSREADER_HH_
0026 #define GENERS_ABSREADER_HH_
0027
0028 #include "Alignment/Geners/interface/IOException.hh"
0029 #include <map>
0030 #include <sstream>
0031 #include <string>
0032
0033 #include "Alignment/Geners/interface/ClassId.hh"
0034
0035 namespace gs {
0036 template <class Base>
0037 struct AbsReader {
0038 virtual ~AbsReader() {}
0039
0040 virtual Base *read(const ClassId &id, std::istream &in) const = 0;
0041 };
0042
0043 template <class Base, class Derived>
0044 struct ConcreteReader : public AbsReader<Base> {
0045 ~ConcreteReader() override {}
0046
0047 inline Derived *read(const ClassId &id, std::istream &in) const override {
0048
0049
0050 return Derived::read(id, in);
0051 }
0052 };
0053
0054 template <class Base>
0055 class DefaultReader : public std::map<std::string, AbsReader<Base> *> {
0056 public:
0057 typedef Base value_type;
0058
0059 inline DefaultReader() : std::map<std::string, AbsReader<Base> *>() {}
0060
0061 virtual ~DefaultReader() {
0062 for (typename std::map<std::string, AbsReader<Base> *>::iterator it = this->begin(); it != this->end(); ++it)
0063 delete it->second;
0064 }
0065
0066 inline Base *read(const ClassId &id, std::istream &in) const {
0067 typename std::map<std::string, AbsReader<Base> *>::const_iterator it = this->find(id.name());
0068 if (it == this->end()) {
0069 std::ostringstream os;
0070 os << "In gs::DefaultReader::read: class \"" << id.name() << "\" is not mapped to a concrete reader";
0071 throw gs::IOInvalidArgument(os.str());
0072 }
0073 return it->second->read(id, in);
0074 }
0075
0076 DefaultReader(const DefaultReader &) = delete;
0077 DefaultReader &operator=(const DefaultReader &) = delete;
0078 };
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090 template <class Reader>
0091 class StaticReader {
0092 public:
0093 typedef typename Reader::Base::value_type InheritanceBase;
0094
0095 static const Reader &instance() {
0096 static const Reader obj;
0097 return obj;
0098 }
0099
0100 template <class Derived>
0101 static void registerClass() {
0102 Reader &rd = const_cast<Reader &>(instance());
0103 const ClassId &id(ClassId::makeId<Derived>());
0104 delete rd[id.name()];
0105 rd[id.name()] = new ConcreteReader<InheritanceBase, Derived>();
0106 }
0107
0108
0109 StaticReader() = delete;
0110 };
0111 }
0112
0113 #endif