Diana Software
QHandle.hh
Go to the documentation of this file.
1 #ifndef _Q_HANDLE_HH_
2 #define _Q_HANDLE_HH_
3 
4 #include <sstream>
5 #include "QObject.hh"
6 #include "QError.hh"
7 #include "QDemangle.hh"
8 #include "QDiana.hh"
9 
25 
26 template<class T> class QHandle
27 {
28  public:
29 
31  virtual bool Exists() const {return (fObject ? true : false);}
32 
34  virtual bool IsValid() const {return (Exists() && fObject->IsValid());}
35 
37  const char* GetObjectName() const { return fObjectName; }
38 
40  const char* GetObjectOwner() const { return fOwner; }
41 
43  std::string GetObjectType() const { return Demangle(fObject); }
44 
46  void Set(QObject** address,const char* owner)
47  {
48  if(fObject) {
49  std::stringstream msg;
50  msg<<"QHandle of type: "<<GetObjectType()
51  <<" already owns a QObject";
52  if(fOwner)
53  msg<<" Owned by: "<<fOwner;
54 
55  throw QError(QERR_EVENT_HANDLE,__FILE__,__LINE__,msg.str());
56  }
57  fOwner = owner;
58  fAddress = address;
59  if(fAddress) fObject = dynamic_cast<T*>((QObject*)*fAddress);
60  }
61 
63  virtual ~QHandle() {};
64 
65  protected:
66 
68  QHandle(const char* objectName) : fObject(0), fAddress(0)
69  {
70  fObjectName = objectName;
71  fOwner = "";
72  }
73 
74  void ThrowError() const
75  {
76  std::stringstream msg;
77  msg<<"Object ( Name: "<<this->fObjectName
78  <<"; Owner: "<<this->fOwner
79  <<"; Type: "<<this->GetObjectType();
80  if(!Exists()) {
81  msg<<" ) does not exist";
82  } else {
83  msg<<"; Address: "<<fObject;
84  if(!IsValid()) {
85  msg<<" ) is not valid";
86  } else {
87  msg<<" ) is valid"; // so why an error? :)
88  }
89 
90  }
91  throw QError(QERR_EVENT_HANDLE,__FILE__,__LINE__,msg.str());
92  }
93 
94  protected:
96  {
97  return fAddress;
98  }
99 
100  const char* fObjectName;
101  const char* fOwner;
104 
105  QHandle(const QHandle&) {};
106  const QHandle& operator=(const QHandle&) { return *this; };
107 
108  private:
109 };
110 
125 template<typename T> class ReadHandle : public QHandle<T>
126 {
127  public:
129  ReadHandle(const char* name) : QHandle<T>(name) {}
130 
131 
133  const T& Get() const
134  {
135  if(!this->IsValid()) this->ThrowError();
136  return *(this->fObject);
137  }
138 
139  private:
140 };
141 
154 template<typename T> class WriteHandle : public QHandle<T>
155 {
156  public:
158  WriteHandle(const char* name) : QHandle<T>(name) {}
159 
161  T& Get()
162  {
163  if(!this->Exists()) {
164  std::stringstream msg;
165  msg<<"Object ( Name: "<<this->fObjectName
166  <<"; Owner : "<<this->fOwner
167  <<"; Type: "<<this->GetObjectType()
168  <<" ) does not exist (probably not added to the event?)";
169  throw QError(QERR_EVENT_HANDLE,__FILE__,__LINE__,msg.str());
170  }
171  // if somebody wants to write it, it is automatically made valid.
172  this->fObject->Validate();
173  return *(this->fObject);
174  }
175 
177  void CreateObject(const char* owner)
178  {
179  T** pobj = new (T*);
180  *pobj = new T;
181  T* obj = *pobj;
182  if(obj) {
183  obj->Reset();
184  this->Set((QObject**)pobj,owner);
185  }
186  }
187 
190  {
191  return this->BaseGetAddress();
192  }
193 
194 
195 };
196 
197 
199 
200 #endif
std::string Demangle(const C &obj, int firstChar=0)
Definition: QDemangle.hh:12
#define Q_END_NAMESPACE
Definition: QDiana.hh:22
#define Q_BEGIN_NAMESPACE
Definition: QDiana.hh:20
@ QERR_EVENT_HANDLE
Definition: QError.hh:43
error class with error type and description
Definition: QError.hh:115
abstract class for QEvent handlers
Definition: QHandle.hh:27
const char * fObjectName
Definition: QHandle.hh:100
std::string GetObjectType() const
get object type
Definition: QHandle.hh:43
const QHandle & operator=(const QHandle &)
Definition: QHandle.hh:106
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
T * fObject
Definition: QHandle.hh:102
const char * fOwner
Definition: QHandle.hh:101
QHandle(const char *objectName)
ctor
Definition: QHandle.hh:68
QHandle(const QHandle &)
Definition: QHandle.hh:105
QObject ** BaseGetAddress()
Definition: QHandle.hh:95
virtual ~QHandle()
destructor
Definition: QHandle.hh:63
QObject ** fAddress
Definition: QHandle.hh:103
virtual bool IsValid() const
Check object validity.
Definition: QHandle.hh:34
virtual bool Exists() const
Check object validity.
Definition: QHandle.hh:31
const char * GetObjectOwner() const
get object owner
Definition: QHandle.hh:40
void ThrowError() const
Definition: QHandle.hh:74
base class for objects handled by QEvent and QGlobalDataManager.
Definition: QObject.hh:76
read handle to access QEvent QObject's.
Definition: QHandle.hh:126
ReadHandle(const char *name)
ctor
Definition: QHandle.hh:129
const T & Get() const
Get Object.
Definition: QHandle.hh:133
write handle to access and add QEvent QObject's.
Definition: QHandle.hh:155
WriteHandle(const char *name)
ctor
Definition: QHandle.hh:158
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