Diana Software
QGlobalReader.hh
Go to the documentation of this file.
1 #ifndef _Q_GLOBAL_READER_HH_
2 #define _Q_GLOBAL_READER_HH_
3 
28 #include "QDiana.hh"
29 #include "QNamed.hh"
30 #include "QError.hh"
31 #include "QMessage.hh"
32 #include "QDemangle.hh"
33 #include <string>
34 #include <sstream>
35 #include "QGlobalRWFactory.hh"
36 #include "QGlobalLabel.hh"
37 #include "QMessage.hh"
38 #include "QObject.hh"
39 
40 
41 class TObject;
42 
43 
44 class QGlobalReader : public QNamed
45 {
46  public:
48  QGlobalReader(const std::string& name) : QNamed(name) {}
49 
51  virtual ~QGlobalReader() {}
52 
54  template<class Q> const Q* Get(const std::string& name, QError& err) const
55  {
56  const Q* qobj = 0;
57  const Diana::QObject* obj = GetQObject(name,Demangle(qobj),err);
58  qobj = dynamic_cast<const Q*>(obj);
59  if(obj && !qobj) {
61  std::stringstream msg;
62  msg<<"Requested type: "<<Demangle(qobj)<<" found type: "<<obj->ClassName();
63  err.SetDescription(msg.str());
64  }
65 
66  return qobj;
67 
68  }
70  template<class Q> const Q* Get(const Diana::QGlobalLabel& label, QError& err) const
71  {
72  const Q* qobj = 0;
73  const Diana::QObject* obj = GetQObject(label,Demangle(qobj), err);
74  qobj = dynamic_cast<const Q*>(obj);
75  if(obj && !qobj) {
77  std::stringstream msg;
78  msg<<"Requested type: "<<Demangle(qobj)<<" found type: "<<obj->ClassName();
79  err.SetDescription(msg.str());
80  }
81 
82  return qobj;
83  }
84 
85 
86 
87  protected:
88  virtual const Diana::QObject* GetQObject(const std::string&, const std::string&, QError& err) const
89  {
91  err.SetDescription(GetName(),0,"GetQObject not implemented");
92  return 0;
93  }
94 
95  virtual const Diana::QObject* GetQObject(const Diana::QGlobalLabel& label,const std::string& className, QError& err) const
96  {
97  return GetQObject(label.GetStringLabel(),className,err);
98  }
99 
101  virtual QError Open(const std::string& filename, const std::string& opt = "") = 0;
102 
104  virtual QError Close() = 0;
105 
107 
108 };
109 
116  public:
117  QGlobalReaderFactory(const std::string& name, const std::string& ext) {fName = name; fExt = ext;}
118  const std::string& GetName() const { return fName;}
119  const std::string& GetExt() const { return fExt;}
120  virtual ~QGlobalReaderFactory() {};
121  private:
122  virtual QGlobalReader* Create() = 0;
123  std::string fName;
124  std::string fExt;
125  friend class QGlobalRWFactory;
126 };
127 
128 
129 #define CREATE_GOLBAL_READER_IMPL(clazz,ext) \
130  class clazz ## Factory: public QGlobalReaderFactory{\
131  public:\
132  clazz ## Factory() : QGlobalReaderFactory(#clazz,ext) {}\
133  QGlobalReader *Create(){ return new clazz; }\
134  };
135 
136 #define REGISTER_GLOBAL_READER(clazz, ext) \
137  CREATE_GOLBAL_READER_IMPL(clazz, ext);\
138  extern "C" void registerInFactory ## clazz() {\
139  QGlobalReaderFactory* pt = new clazz ## Factory;\
140  QGlobalRWFactory::GetInstance().RegisterGlobalReader(pt);\
141  }
142 
143 #endif
err
Definition: CheckOF.C:114
std::string Demangle(const C &obj, int firstChar=0)
Definition: QDemangle.hh:12
@ QERR_TYPE_CONVERSION
Definition: QError.hh:36
@ QERR_NOT_IMPLEMENTED
Definition: QError.hh:109
error class with error type and description
Definition: QError.hh:115
This class take care of plugin registration and creation.
Class that returns a pointer to the reader matching the given input filename.
abstract factory for QGlobalReader.
QGlobalReaderFactory(const std::string &name, const std::string &ext)
virtual ~QGlobalReaderFactory()
virtual QGlobalReader * Create()=0
const std::string & GetExt() const
const std::string & GetName() const
Abstract class for global readers.
const Q * Get(const std::string &name, QError &err) const
Get QObject, owned by this reader.
virtual const Diana::QObject * GetQObject(const Diana::QGlobalLabel &label, const std::string &className, QError &err) const
const Q * Get(const Diana::QGlobalLabel &label, QError &err) const
Get QObject, owned by this reader.
virtual QError Close()=0
Close file, called by QGlobalReaderDispatcher.
virtual const Diana::QObject * GetQObject(const std::string &, const std::string &, QError &err) const
virtual ~QGlobalReader()
destructor
virtual QError Open(const std::string &filename, const std::string &opt="")=0
Open file, called by QGlobalReaderDispatcher.
QGlobalReader(const std::string &name)
constructor
base class for anything that has a name
Definition: QNamed.hh:14
const std::string & GetName() const
Definition: QNamed.hh:19