Diana Software
QEvent.hh
Go to the documentation of this file.
1 #ifndef _Q_EVENT_HH_
2 #define _Q_EVENT_HH_
3 
4 #include <string>
5 #include <sstream>
6 #include <map>
7 #include <vector>
8 #include <iostream>
9 #include "QObject.hh"
10 #include "QError.hh"
11 #include "QHandle.hh"
12 #include "QDiana.hh"
13 #include "QAliases.hh"
14 #include "QEventLabel.hh"
15 
44 
45 class QEvent
46 {
47  public:
48  typedef std::map<QEventLabel,QObject**> QObjectMap;
49 
51  QEvent();
52 
54  QEvent(const QEvent& ev);
55 
57  ~QEvent();
58 
60  unsigned int GetReadNumber() const {return fReadNumber;}
61 
74  template<typename Q> void Get(const char* owner,ReadHandle<Q>& handle) const
75  {
76  handle.Set(0,owner);
77  QObject** evData = GetObject(owner,handle.GetObjectName());
78  if(evData) handle.Set(evData,owner);
79  }
89  template<typename Q> void Get(ReadHandle<Q>& handle) const
90  {
91  const char* owner = fModuleName->c_str();
92  handle.Set(0,owner);
93  QObject** evData = GetObject(owner,handle.GetObjectName());
94  if(evData) handle.Set(evData,owner);
95  }
96 
106  template<typename Q> const Q& Get(const char* owner, const char* name) const
107  {
108  ReadHandle<Q> handle(name);
109  Get(owner,handle);
110  return handle.Get();
111  }
112 
120  template<typename Q> const Q& Get(const char* name) const
121  {
122  ReadHandle<Q> handle(name);
123  Get(handle);
124  return handle.Get();
125  }
126 
135  template<typename Q> const Q& GetByLabel(const QEventLabel& label) const
136  {
137  ReadHandle<Q> handle(label.GetName().c_str());
138  handle.Set(0,label.GetOwner().c_str());
139  QObject** evData = this->GetObjectByLabel(label);
140  if(evData) handle.Set(evData,label.GetOwner().c_str());
141  return handle.Get();
142  }
143 
153  template<typename Q> void Get(WriteHandle<Q>& handle)
154  {
155  if(!fModuleName) {
156  std::stringstream msg;
157  msg<<"Module name not set while getting in write mode a "<<handle.GetObjectName();
158  throw QError(QERR_EVENT_GENERIC,__FILE__,__LINE__,msg.str());
159  }
160  Get(fModuleName->c_str(),handle);
161  }
169  template<typename Q> Q& Get(const char* name)
170  {
171  WriteHandle<Q> handle(name);
172  Get(handle);
173  return handle.Get();
174  }
175 
193  template<typename Q> void Add(WriteHandle<Q>& handle)
194  {
195  if(!fModuleName) {
196  std::stringstream msg;
197  msg<<"Module name not set while adding a "<<handle.GetObjectName();
198  throw QError(QERR_EVENT_GENERIC,__FILE__,__LINE__,msg.str());
199  }
200  Add(fModuleName->c_str(),handle);
201  handle.Get().SetIsJustAdded();
202  }
203 
222  template<typename Q> Q& Add(const std::string& name)
223  {
224  WriteHandle<Q> handle(name.c_str());
225  Add(handle);
226  return handle.Get();
227  }
228 
232  template<typename Q> void Require(const std::string& owner, const std::string& name) const
233  {
234  QEventLabel label(owner,name);
235  return RequireByLabel<Q>(label);
236  }
237 
242  template<typename Q> void RequireByLabel(const QEventLabel& label) const
243  {
244  ReadHandle<Q> handle(label.name.c_str());
245  Get(label.owner.c_str(),handle);
246  if(!handle.Exists()) {
247  std::stringstream msg;
248  msg<<"Required object: "<<label.GetStringLabel()<<" of type: "<<handle.GetObjectType()<<" not found in the event";
249  throw QError(QERR_EVENT_GENERIC,__FILE__,__LINE__,msg.str());
250  } else {
251  QObject** obj = GetObjectByLabel(label);
252  (*obj)->SetIsRequired();
253  }
254  }
261  void SetAlias(const std::string& name, const std::string& pathInObject, const std::string& alias)
262  {
263  QAliases& aliases = GetAliases();
264  aliases.Add(QEventLabel(*GetOwner(),name),pathInObject,alias);
265  }
266 
271  void Dump(std::ostream& o) const;
272 
274  const QAliases& GetAliases() const { return fAliases; }
275 
277  const std::string* GetOwner() const { return fModuleName; }
279  template<typename Q> bool Contains(const char *owner,const char *name) const
280  {
281  QEventLabel label(owner,name);
282  QObjectMap::const_iterator it = fObjectMap.find(label);
283  if(it!=fObjectMap.end()){
284  if( (*(it->second))->IsA() == Q::Class() )
285  return true;
286  }
287  return false;
288  }
290  template<typename Q> bool Contains(const char *label) const
291  {
292  QObjectMap::const_iterator it = fObjectMap.find(label);
293  if(it!=fObjectMap.end()){
294  if( (*(it->second))->IsA() == Q::Class() )
295  return true;
296  }
297  return false;
298  }
299 
300  protected:
301 
302 
303 
309  template<typename Q> void Get(const char* owner, WriteHandle<Q>& handle)
310  {
311  handle.Set(0,owner);
312  QObject** evData = GetObject(owner,handle.GetObjectName());
313  if(evData) handle.Set(evData,owner);
314  }
315 
321  template<typename Q> void Add(const char* owner, WriteHandle<Q>& handle)
322  {
323  const char* name = handle.GetObjectName();
324  if(!handle.GetAddress()) {
325  // if found set the existing object otherwise create a new one.
326  QObject** address = GetObject(owner,name);
327  if(address && fOverWrite)
328  handle.Set(address,owner);
329  else
330  handle.CreateObject(owner);
331  }
332  Add(owner,name,handle.GetAddress());
333  }
334 
340  template<typename Q> Q& Add(const char* owner, const char* name)
341  {
342  WriteHandle<Q> handle(name);
343  Add(owner, handle);
344  return handle.Get();
345  }
346 
348  void Clear();
349 
351  void Consolidate();
352 
354  void SetOwner(const std::string* owner) const { fModuleName = owner; }
355 
357  QAliases& GetAliases() { return fAliases; }
358 
360  std::vector<QEventLabel> GetLabels() const;
361 
363  void SetReadNumber(const unsigned int readNumber) {fReadNumber = readNumber; }
364 
366  void SetOverWrite(const bool ow = true) { fOverWrite = ow; }
372  const QEvent& operator=(const QEvent& ev);
373 
374  QObject** GetObject(const char* owner, const char* name) const;
375  QObject** GetObjectByLabel(const QEventLabel& label) const;
376 
377  private:
378  void GetLabel(const char* owner, const char* name, QEventLabel& label) const;
379 
380  void Add(const char* owner, const char* name, QObject**);
381 
382 
384  std::map<std::string, std::string> fDefaultMap;
385 
386  const std::string mutable* fModuleName;
387 
389  unsigned int fReadNumber;
390 
392 
394 
395  friend class QEventAssembler;
396  friend class QBaseModule;
397 };
398 
400 
401 #endif
#define Q_END_NAMESPACE
Definition: QDiana.hh:22
#define Q_BEGIN_NAMESPACE
Definition: QDiana.hh:20
@ QERR_EVENT_GENERIC
Definition: QError.hh:42
void Add(const QEventLabel &label, const std::string &path, const std::string &alias)
Definition: QAliases.cc:50
Base class for modules.
Definition: QBaseModule.hh:57
error class with error type and description
Definition: QError.hh:115
Visitor class of QEvent that provides full handling of QEvent.
label for QObject in the QEvent
Definition: QEventLabel.hh:23
std::string GetStringLabel() const
get string in the format "owner@name"
Definition: QEventLabel.cc:48
const std::string & GetOwner() const
get owner
Definition: QEventLabel.hh:55
const std::string & GetName() const
get name
Definition: QEventLabel.hh:57
std::string owner
owner of the QObject
Definition: QEventLabel.hh:60
std::string name
name of the QObject
Definition: QEventLabel.hh:62
diana event
Definition: QEvent.hh:46
std::map< std::string, std::string > fDefaultMap
Definition: QEvent.hh:384
const Q & GetByLabel(const QEventLabel &label) const
Get a QObject in read mode by label.
Definition: QEvent.hh:135
const Q & Get(const char *owner, const char *name) const
Get a QObject in read mode.
Definition: QEvent.hh:106
void SetReadNumber(const unsigned int readNumber)
set the event number as read by the current reader.
Definition: QEvent.hh:363
void RequireByLabel(const QEventLabel &label) const
notify the QEvent that we need a QObject, if not found an exception is thrown
Definition: QEvent.hh:242
QObject ** GetObject(const char *owner, const char *name) const
Definition: QEvent.cc:167
void Clear()
reset members to default values
Definition: QEvent.cc:74
bool fOverWrite
Definition: QEvent.hh:391
QAliases fAliases
Definition: QEvent.hh:393
bool Contains(const char *label) const
Check to see if the event contains a particular object.
Definition: QEvent.hh:290
Q & Add(const char *owner, const char *name)
Add a QObject to the event. This method has to be called before the event loop.
Definition: QEvent.hh:340
unsigned int fReadNumber
event number
Definition: QEvent.hh:389
QObject ** GetObjectByLabel(const QEventLabel &label) const
Definition: QEvent.cc:175
void Get(const char *owner, WriteHandle< Q > &handle)
Get a QObject handle in write mode.
Definition: QEvent.hh:309
void Dump(std::ostream &o) const
Print the event and its member.
Definition: QEvent.cc:98
void Add(const char *owner, WriteHandle< Q > &handle)
Add a QObject to the event. This method has to be called before the event loop.
Definition: QEvent.hh:321
void Require(const std::string &owner, const std::string &name) const
notify the QEvent that we need a QObject, if not found an exception is thrown
Definition: QEvent.hh:232
void Consolidate()
keep only Required and JustAdded QObjects
Definition: QEvent.cc:84
QEvent()
constructor
Definition: QEvent.cc:8
const std::string mutable * fModuleName
Definition: QEvent.hh:386
void Get(const char *owner, ReadHandle< Q > &handle) const
Get a QObject Handle in read mode.
Definition: QEvent.hh:74
~QEvent()
destructor
Definition: QEvent.cc:17
const QEvent & operator=(const QEvent &ev)
operator= new QObject's are created if needed and are owned by this. This this method is protected,...
Definition: QEvent.cc:43
Q & Get(const char *name)
Get a QObject in write mode.
Definition: QEvent.hh:169
void Get(ReadHandle< Q > &handle) const
Get a QObject Handle in read mode, owned by the current module.
Definition: QEvent.hh:89
void SetOverWrite(const bool ow=true)
set whether an object can be overwritten
Definition: QEvent.hh:366
const std::string * GetOwner() const
get the name of the module that is currently acting on the event
Definition: QEvent.hh:277
QObjectMap fObjectMap
Definition: QEvent.hh:383
void GetLabel(const char *owner, const char *name, QEventLabel &label) const
Definition: QEvent.cc:184
std::vector< QEventLabel > GetLabels() const
Get all object labels.
Definition: QEvent.cc:156
void SetOwner(const std::string *owner) const
set the name of the module that is currently acting on the event
Definition: QEvent.hh:354
Q & Add(const std::string &name)
Add a QObject to the event.
Definition: QEvent.hh:222
std::map< QEventLabel, QObject ** > QObjectMap
Definition: QEvent.hh:48
unsigned int GetReadNumber() const
return the event number as read by the current reader.
Definition: QEvent.hh:60
QAliases & GetAliases()
get aliases
Definition: QEvent.hh:357
void Get(WriteHandle< Q > &handle)
Get a QObject Handle in write mode.
Definition: QEvent.hh:153
const Q & Get(const char *name) const
Get a QObject in read mode, owned by the current module.
Definition: QEvent.hh:120
void Add(WriteHandle< Q > &handle)
Add a QObject to the event.
Definition: QEvent.hh:193
bool Contains(const char *owner, const char *name) const
Check to see if the event contains a particular object.
Definition: QEvent.hh:279
const QAliases & GetAliases() const
get aliases (const version)
Definition: QEvent.hh:274
void SetAlias(const std::string &name, const std::string &pathInObject, const std::string &alias)
set alias for objects or variables in objects owned by currentmodule.
Definition: QEvent.hh:261
std::string GetObjectType() const
get object type
Definition: QHandle.hh:43
void Set(QObject **address, const char *owner)
set object address (owned by the caller) and owner
Definition: QHandle.hh:46
const char * GetObjectName() const
get object name
Definition: QHandle.hh:37
virtual bool Exists() const
Check object validity.
Definition: QHandle.hh:31
base class for objects handled by QEvent and QGlobalDataManager.
Definition: QObject.hh:76
read handle to access QEvent QObject's.
Definition: QHandle.hh:126
const T & Get() const
Get Object.
Definition: QHandle.hh:133
write handle to access and add QEvent QObject's.
Definition: QHandle.hh:155
T & Get()
get object
Definition: QHandle.hh:161
void CreateObject(const char *owner)
create object
Definition: QHandle.hh:177
QObject ** GetAddress()
get object address
Definition: QHandle.hh:189