Diana Software
QGlobalHandle.hh
Go to the documentation of this file.
1 #ifndef _Q_GLOBALHANDLE_HH_
2 #define _Q_GLOBALHANDLE_HH_
30 #include "QGlobalReader.hh"
31 #include "QGlobalWriter.hh"
32 #include "QGlobalLabel.hh"
33 #include "QDemangle.hh"
34 #include <sstream>
35 
37 
38 class QGlobalDataManager;
39 
40 template<class Q> class GlobalHandle
41 {
42  public:
47  GlobalHandle(const std::string& name, const std::string& defaultOwner = "")
48  {
49  fLabel.fName = name;
50  fLabel.fOwner = "";
51  fVersion = "";
52  fObject = 0;
53  fMyObject = 0;
54  fDefaultOwner = defaultOwner;
56  fDescription = "";
57  }
58 
63  {
64  fLabel = other.fLabel;
65  fVersion = other.fVersion;
66  fObject = 0;
67  fMyObject = 0;
68  if(other.fMyObject) {
69  Set(other.Get());
70  } else {
71  fObject = other.fObject;
72  }
75  fDescription = other.fDescription;
76  }
78  virtual ~GlobalHandle()
79  {
80  if(fMyObject) delete fMyObject;
81  };
82 
84  const QGlobalLabel& GetLabel() const { return fLabel; }
86  const std::string& GetName() const { return fLabel.fName; }
88  const std::string& GetOwner() const { return fLabel.fOwner; }
90  const std::string& GetDefaultOwner() const { return fDefaultOwner; }
92  int GetChannel() const { return fLabel.fChannel; }
94  int GetChannel2() const { return fLabel.fChannel2; }
96  int GetBeginRun() const { return fLabel.fBeginRun; }
98  int GetEndRun() const { return fLabel.fEndRun; }
100  int GetRun() const { return fLabel.fBeginRun; }
102  int GetBeginDataset() const { return fLabel.fBeginDataset; }
104  int GetEndDataset() const { return fLabel.fEndDataset; }
106  int GetDataset() const { return fLabel.fBeginDataset; }
108  void GetValidityInterval(int& beginRun, int& endRun) const
109  {
110  beginRun = fLabel.fBeginRun;
111  endRun = fLabel.fEndRun;
112  }
114  void SetChannel(const int chan) { fLabel.fChannel = chan; }
116  void SetChannel2(const int chan) { fLabel.fChannel2 = chan; }
118  void SetRunInterval(const int start, const int end) { fLabel.fBeginRun = start; fLabel.fEndRun = end; }
120  void SetRun(const int run) { fLabel.fBeginRun = fLabel.fEndRun = run; }
121 
123  void SetDatasetInterval(const int start, const int end) { fLabel.fBeginDataset = start; fLabel.fEndDataset = end; }
125  void SetDataset(const int ds) { fLabel.fBeginDataset = fLabel.fEndDataset = ds; }
126 
128  void AddKey(const std::string& key, const int digits)
129  {
130  QGlobalLabel::ExtraKey& thisKey = fLabel.fExtraKeys[key];
131  thisKey.Clear();
132  thisKey.fDigits = digits;
133  }
135  void SetKey(const std::string& key, const int value)
136  {
137  if( fLabel.fExtraKeys.find(key) == fLabel.fExtraKeys.end()) {
138  std::stringstream msg;
139  msg<<"QGlobalHandle of type: "<<Demangle(fObject)
140  <<" has no extra key: \""
141  <<key
142  <<"\". Add it via GloabalHandle::AddKey() in your GlobalHandle ctor)";
143  DianaThrow( QError(QERR_GLOBAL_HANDLE,__FILE__,__LINE__,msg.str()) );
144 
145  }
146  fLabel.fExtraKeys[key].fValue = value;
147  }
149  int GetKey(const std::string& key) const
150  {
151  if( fLabel.fExtraKeys.find(key) == fLabel.fExtraKeys.end()) {
152  std::stringstream msg;
153  msg<<"QGlobalHandle of type: "<<Demangle(fObject)
154  <<" has no extra key: \""
155  <<key
156  <<"\". Add it via GloabalHandle::AddKey() in your GlobalHandle ctor)";
157  DianaThrow( QError(QERR_GLOBAL_HANDLE,__FILE__,__LINE__,msg.str()) );
158 
159  }
160  return fLabel.fExtraKeys.find(key)->second.fValue;
161  }
162 
163  bool HaveKey(const std::string& key) const
164  {
165  return (fLabel.fExtraKeys.find(key) != fLabel.fExtraKeys.end());
166  }
167 
168 
170  void Set(const Q& obj)
171  {
172  CreateObject();
173  fObject->Reset();
174  obj.Q::Fill(fObject);
175  // Force validity
176  fObject->Validate();
177  }
179  const Q& Get() const
180  {
181  if(!IsValid()) {
182  std::stringstream msg;
183  msg<<"Global Object (Name: \""<<this->GetName()
184  <<"\" , Owner: \""<<this->GetOwner()<<"\", label: \""<<fLabel.GetStringLabel()<<"\") is not valid";
187  }
188  DianaThrow( QError(QERR_GLOBAL_HANDLE,__FILE__,__LINE__,msg.str()));
189  }
190  return *fObject;
191  }
193  bool IsValid() const {return fObject && fObject->IsValid(); }
194 
196  const QError& GetError() const { return fGlobalDataManagerError; }
197 
199  void SetDescription(const std::string& descr) { fDescription = descr; }
200 
202  virtual QError FillFromFile(const std::string& fname)
203  {
206  const Q* tobj = 0;
207  std::string key = fLabel.GetStringLabel();
208  if(reader) {
209  tobj = reader->Get<Q>(fLabel,err);
210  } else {
211  std::string str = "Error while opening file: \"";
212  str += fname + "\": ";
213  str += err.GetDescription();
214  err.SetDescription(str);
215  return err;
216  }
217 
218  if(err==QERR_SUCCESS) {
219  Set(*tobj);
220  if(tobj->IsValid()) fObject->Validate();
221  else fObject->InValidate();
222  } else {
223  std::string str = "Error while reading object: \"";
224  str += fLabel.GetStringLabel() + "\" ";
225  str += "From file \"";
226  str += fname;
227  str += "\": ";
228  str += err.GetDescription();
229  err.SetDescription(str);
230  return err;
231  }
232 
233  return err;
234  }
235 
237  virtual QError StoreOnFile(const std::string& fname) const
238  {
240  if(!IsValid()) {
242  std::stringstream strstr;
243 
244  strstr<<"StoreOnFile(): global Object (Name: \""<<this->GetName()
245  <<"\" , Owner: \""<<this->GetOwner()<<"\") is not valid, so it is not being written";
246  err.SetDescription(strstr.str());
247  return err;
248  }
250  if(writer) err = writer->Set(fLabel.GetStringLabel(), *fObject,fDescription);
251  else {
252  std::string str = "Error while opening file: \"";
253  str += fname + "\": ";
254  str += err.GetDescription();
255  err.SetDescription(str);
256  return err;
257  }
258  if( err != QERR_SUCCESS) {
259  std::string str = "Error while writing object: \"";
260  str += fLabel.GetStringLabel() + "\" ";
261  str += "on file \"";
262  str += fname;
263  str += "\": ";
264  str += err.GetDescription();
265  err.SetDescription(str);
266  }
267  return err;
268 
269  }
270 
272  virtual QError FillFromDB()
273  {
275  std::stringstream strstr;
276  strstr<<"FillFromDB() is not implemented for object of type: "
277  <<Demangle(fObject)<<"(Name: \""<<this->GetName()
278  <<"\" , Owner: \""<<this->GetOwner()<<"\")."
279  <<" Write a specific GlobalHandle!";
280  err.SetDescription(strstr.str());
281  return err;
282  }
283 
285  virtual QError StoreOnDB() const
286  {
288  std::stringstream strstr;
289  strstr<<"StoreOnDB() is not implemented for object of type: "
290  <<Demangle(fObject)<<"(Name: \""<<this->GetName()
291  <<"\" , Owner: \""<<this->GetOwner()<<"\")."
292  <<" Write a specific GlobalHandle!";
293  err.SetDescription(strstr.str());
294  return err;
295  }
296 
298  virtual QError StoreOnDB()
299  {
300  return const_cast<const GlobalHandle<Q>* >(this)->StoreOnDB();
301  }
302 
304  std::string GetVersion() const { return fVersion; }
305 
306 
307 
308  protected:
310  Q** GetAddress() { return &fObject; }
311 
313  Q** const GetAddress() const { return &fObject; }
314 
317  {
318  if(fObject) {
319  std::stringstream msg;
320  msg<<"QGlobalHandle of type: "<<Demangle(fObject)
321  <<" already owns a QObject"
322  <<" ( Name: "<<GetName()
323  <<" Owned by: "<<GetOwner()
324  <<" )";
325 
326  DianaThrow( QError(QERR_GLOBAL_HANDLE,__FILE__,__LINE__,msg.str()) );
327  }
328 
329  fMyObject = new Q;
330  fObject = fMyObject;
331  }
332 
333  private:
335  void SetOwner(const std::string& owner) { fLabel.fOwner = owner; }
339  const GlobalHandle<Q>& operator=(const GlobalHandle<Q>& other) { return *this; }
340 
342  void SetVersion(const std::string& tag) { fVersion = tag; }
347 
348 
352  std::string fVersion;
354  std::string fDefaultOwner;
358  std::string fDescription;
359 
360  friend class QGlobalDataManager;
361 };
362 
364 
365 
366 #endif
err
Definition: CheckOF.C:114
std::string Demangle(const C &obj, int firstChar=0)
Definition: QDemangle.hh:12
#define DianaThrow(obj)
Definition: QDianaDebug.hh:26
#define Q_END_NAMESPACE
Definition: QDiana.hh:22
#define Q_BEGIN_NAMESPACE
Definition: QDiana.hh:20
@ QERR_GLOBAL_HANDLE
Definition: QError.hh:45
@ QERR_NOT_IMPLEMENTED
Definition: QError.hh:109
@ QERR_SUCCESS
Definition: QError.hh:27
template class to handle diana global QObject with QGlobalDataManager
int GetDataset() const
Get dataset of validity.
const QError & GetError() const
Get I/O error.
GlobalHandle()
default ctor. Needed to compile on gcc3
Q * fMyObject
Object created by this class.
GlobalHandle(const std::string &name, const std::string &defaultOwner="")
ctor
void SetRunInterval(const int start, const int end)
Set Validity Interval.
QGlobalLabel fLabel
unique identifier of the object
int GetBeginRun() const
Get first run of validity.
void SetDescription(const std::string &descr)
set object description (experimental)
const std::string & GetName() const
Get object name.
virtual QError FillFromFile(const std::string &fname)
fill QObject from file fname
int GetKey(const std::string &key) const
Get extra integer key.
std::string fDescription
Object description.
int GetEndRun() const
Get last run of validity.
std::string fVersion
Version of the QObject to be read / written.
void SetDatasetInterval(const int start, const int end)
Set Dataset Validity Interval.
virtual ~GlobalHandle()
dtor
virtual QError StoreOnDB()
store object on DB, non const version
bool IsValid() const
check QObject validity
virtual QError StoreOnDB() const
store object on DB
int GetEndDataset() const
Get last dataset of validity.
void AddKey(const std::string &key, const int digits)
Add an extra integer key to the label.
Q **const GetAddress() const
get object address (const)
virtual QError StoreOnFile(const std::string &fname) const
store object on file fname
int GetRun() const
Get run of validity.
void SetKey(const std::string &key, const int value)
Set extra integer key to value.
const QGlobalLabel & GetLabel() const
Get label.
const GlobalHandle< Q > & operator=(const GlobalHandle< Q > &other)
operator= must not be used, use copy ctor
int GetBeginDataset() const
Get first dataset of validity.
Q ** GetAddress()
get object address
Q * fObject
Object.
void CreateObject()
instantiate object of type Q
void SetChannel(const int chan)
Set Channel.
int GetChannel() const
Get Channel associated to the object.
std::string fDefaultOwner
Default owner of the QObject.
void SetOwner(const std::string &owner)
set object owner
std::string GetVersion() const
Get version tag of the object, if any.
const std::string & GetOwner() const
Get object owner.
void GetValidityInterval(int &beginRun, int &endRun) const
Get run validity interval.
int GetChannel2() const
Get Second Channel associated to the object.
GlobalHandle(const GlobalHandle< Q > &other)
copy ctor
QError fGlobalDataManagerError
I/O error from the QGlobalDataManager.
const std::string & GetDefaultOwner() const
Get default owner (may be set by inheriting classes)
void SetRun(const int run)
Set BeginRun = EndRun = run.
void Set(const Q &obj)
set the QObject
virtual QError FillFromDB()
fill object from DB
const Q & Get() const
get the QObject
void SetVersion(const std::string &tag)
set version tag of the object to be read (used by QGlobalDataManager)
bool HaveKey(const std::string &key) const
void SetDataset(const int ds)
Set dataset of validity.
void SetChannel2(const int chan)
Set Channel2.
error class with error type and description
Definition: QError.hh:115
void SetDescription(const std::string &descr)
set error description
Definition: QError.hh:147
const std::string & GetDescription() const
get error description
Definition: QError.cc:190
Object to manage I/O (DB, file, or memory) of diana global quantities.
Label for global QObject's.
Definition: QGlobalLabel.hh:19
std::map< std::string, ExtraKey > fExtraKeys
Definition: QGlobalLabel.hh:73
std::string fName
Object name.
Definition: QGlobalLabel.hh:44
std::string fOwner
Object owner.
Definition: QGlobalLabel.hh:42
std::string GetStringLabel() const
convert label to string
int fBeginRun
begin validity
Definition: QGlobalLabel.hh:46
QGlobalReader * GetReader(const std::string &filename, QError &err)
get reader
static QGlobalReaderDispatcher & GetInstance()
Abstract class for global readers.
const Q * Get(const std::string &name, QError &err) const
Get QObject, owned by this reader.
QGlobalWriter * GetWriter(const std::string &filename, QError &err)
get writer
static QGlobalWriterDispatcher & GetInstance()
Abstract class for global writers.
QError Set(const std::string &name, const Q &obj, const std::string &descr="")
Set QObject, owned by the caller, - leave it undeclared if you can't implement it.