DABC (Data Acquisition Backbone Core)  2.9.9
Reference.h
Go to the documentation of this file.
1 // $Id: Reference.h 4508 2020-05-26 07:55:59Z linev $
2 
3 /************************************************************
4  * The Data Acquisition Backbone Core (DABC) *
5  ************************************************************
6  * Copyright (C) 2009 - *
7  * GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
8  * Planckstr. 1, 64291 Darmstadt, Germany *
9  * Contact: http://dabc.gsi.de *
10  ************************************************************
11  * This software can be used under the GPL license *
12  * agreements as stated in LICENSE.txt file *
13  * which is part of the distribution. *
14  ************************************************************/
15 
16 #ifndef DABC_Reference
17 #define DABC_Reference
18 
19 #include <string>
20 
21 namespace dabc {
22 
23  class Command;
24  class ReferencesVector;
25  class Object;
26  class Mutex;
27 
73  class Reference {
74 
75  friend class Object;
76  friend class Command;
77 
78  protected:
79 
81 
82  Mutex* ObjectMutex() const;
83 
85  void Assign(const Reference & src);
86 
90 
92  template<class T>
93  bool verify_object(Object* src, T* &tgt) { return (tgt=dynamic_cast<T*>(src))!=0; }
94 
95  public:
96 
98  Reference(Object* obj = nullptr);
99 
101  Reference(const Reference& src) throw();
102 
104  virtual ~Reference();
105 
108  void SetAutoDestroy(bool on = true);
109 
112  void SetObject(Object* obj, bool withmutex = true);
113 
115  unsigned NumReferences() const;
116 
118  void Release() throw();
119 
123  Reference Take();
124 
126  void Destroy() throw();
127 
129  inline Object* GetObject() const { return fObj; }
130 
132  Object* GetParent() const;
133 
136 
138  const char* GetName() const;
139 
141  const char* ClassName() const;
142 
145  bool IsName(const char* name) const;
146 
148  inline Object* operator()() const { return fObj; }
149 
151  inline bool null() const { return GetObject() == nullptr; }
152 
154  inline bool operator!() const { return null(); }
155 
157  bool AddChild(Object* obj);
158 
161  unsigned NumChilds() const;
162 
164  Reference GetChild(unsigned n) const;
165 
167  bool GetAllChildRef(ReferencesVector* vect) const;
168 
171  Reference FindChild(const char* name) const;
172 
174  bool RemoveChild(const char* name, bool cleanup = true);
175 
178  bool RemoveChilds(bool cleanup = true);
179 
181  Reference& operator=(const Reference& src) throw();
182 
184  Reference& operator=(Object* obj) throw();
185 
187  Reference& operator<<(Reference& src) throw();
188 
190  inline bool operator==(const Reference& src) const { return GetObject() == src(); }
191 
193  inline bool operator!=(const Reference& src) const { return GetObject() != src(); }
194 
196  inline bool operator==(Object* obj) const { return GetObject() == obj; }
197 
199  inline bool operator!=(Object* obj) const { return GetObject() != obj; }
200 
202  void Print(int lvl=0, const char* from = 0) const;
203 
208  Reference GetFolder(const std::string &name, bool force = false) throw();
209 
212  std::string ItemName(bool compact = true) const;
213 
217  std::string RelativeName(const dabc::Reference& topitem);
218  };
219 }
220 
221 
222 #define DABC_REFERENCE(RefClass, ParentClass, T) \
223  public: \
224  \
225  RefClass(T* obj = 0) throw() : ParentClass(obj) {} \
226  \
227  RefClass(const RefClass& src) throw() : ParentClass(src) {} \
228  \
229  RefClass(const Reference& src) throw() : ParentClass() \
230  { T* res(0); if (verify_object(src(),res)) { Assign(src); } } \
231  \
232  inline T* GetObject() const { return (T*) ParentClass::GetObject(); } \
233  \
234  inline T* operator()() const { return (T*) ParentClass::GetObject(); } \
235  \
236  RefClass& operator=(const RefClass& src) throw() { ParentClass::operator=(src); return *this; } \
237  \
238  RefClass& operator=(const Reference& src) throw() \
239  { \
240  Release(); T* res(0); \
241  if (verify_object(src(),res)) Assign(src); \
242  return *this; \
243  } \
244  \
245  RefClass& operator=(dabc::Object* obj) throw() \
246  { \
247  Release(); T* res(0); \
248  if (verify_object(obj,res)) { RefClass ref((T*)obj); *this << ref; } \
249  return *this; \
250  } \
251  \
252  RefClass& operator<<(Reference& src) throw() \
253  { \
254  T* res(0); \
255  if (verify_object(src(),res)) dabc::Reference::operator<<(src); \
256  else { Release(); src.Release(); } \
257  return *this; \
258  }
259 
260 
261 #endif
262 
Represents command with its arguments.
Definition: Command.h:99
posix pthread mutex
Definition: threads.h:61
Base class for most of the DABC classes.
Definition: Object.h:116
Reference on the arbitrary object
Definition: Reference.h:73
void SetObject(Object *obj, bool withmutex=true)
Direct set of object to reference.
Definition: Reference.cxx:37
bool AddChild(Object *obj)
Add child to list of object children.
Definition: Reference.cxx:188
void Release()
Releases reference on the object.
Definition: Reference.cxx:138
bool operator!=(const Reference &src) const
Compare operator - return true if references refer to different object.
Definition: Reference.h:193
Object * operator()() const
Return pointer on the object.
Definition: Reference.h:148
const char * ClassName() const
Return class name of referenced object, if object not assigned, returns "---".
Definition: Reference.cxx:172
const char * GetName() const
Return name of referenced object, if object not assigned, returns "---".
Definition: Reference.cxx:167
bool operator!=(Object *obj) const
Compare operator - return true if reference refer to different objects.
Definition: Reference.h:199
Reference GetParentRef() const
Returns reference on parent object.
Definition: Reference.h:135
Object * GetObject() const
Return pointer on the object.
Definition: Reference.h:129
virtual ~Reference()
Destructor, releases reference on the object.
Definition: Reference.cxx:133
Reference Take()
Copy reference to output object.
Definition: Reference.cxx:102
bool GetAllChildRef(ReferencesVector *vect) const
Return references for all childs.
Definition: Reference.cxx:204
Reference FindChild(const char *name) const
Searches for child in referenced object.
Definition: Reference.cxx:210
bool RemoveChilds(bool cleanup=true)
Remove all childs in referenced object If cleanup true (default) and object is owner,...
Definition: Reference.cxx:223
void Print(int lvl=0, const char *from=0) const
Show on debug output content of reference.
Definition: Reference.cxx:229
bool IsName(const char *name) const
Returns true if object name is the same as specified one.
Definition: Reference.cxx:177
unsigned NumReferences() const
Returns number of references on the object.
Definition: Reference.cxx:112
bool operator!() const
Returns true if reference contains nullptr.
Definition: Reference.h:154
bool operator==(Object *obj) const
Compare operator - return true if reference refer to same object.
Definition: Reference.h:196
Reference(Object *obj=nullptr)
Constructor, creates reference on the object.
Definition: Reference.cxx:25
bool operator==(const Reference &src) const
Compare operator - return true if references refer to same object.
Definition: Reference.h:190
Reference GetFolder(const std::string &name, bool force=false)
Return folder of specified name, no special symbols are allowed.
Definition: Reference.cxx:234
bool RemoveChild(const char *name, bool cleanup=true)
Remove child with given name and return reference on that child.
Definition: Reference.cxx:215
Reference & operator=(const Reference &src)
Assignment operator - copy reference.
Definition: Reference.cxx:64
Object * GetParent() const
Returns pointer on parent object.
Definition: Reference.cxx:162
unsigned NumChilds() const
Return number of childs in referenced object.
Definition: Reference.cxx:194
std::string RelativeName(const dabc::Reference &topitem)
Produce name, which can be used to find item, calling topitem.FindChild().
Definition: Reference.cxx:248
Object * fObj
pointer on the object
Definition: Reference.h:80
std::string ItemName(bool compact=true) const
Produce string, which can be used as name argument in dabc::mgr.FindItem(name) call.
Definition: Reference.cxx:241
void Assign(const Reference &src)
Method used in copy constructor and assigned operations.
Definition: Reference.cxx:50
bool AcquireRefWithoutMutex(Reference &ref)
Special method, which allows to generate new reference when object mutex is locked.
Definition: Reference.cxx:123
bool verify_object(Object *src, T *&tgt)
Method used in reference constructor/assignments to verify is object is suitable.
Definition: Reference.h:93
void Destroy()
Release reference and starts destroyment of referenced object.
Definition: Reference.cxx:148
Mutex * ObjectMutex() const
Definition: Reference.cxx:183
Reference GetChild(unsigned n) const
Return reference on child n.
Definition: Reference.cxx:199
void SetAutoDestroy(bool on=true)
Set autodestroy flag for the object Once enabled, object will be destroyed when last reference will b...
Definition: Reference.cxx:118
Reference & operator<<(Reference &src)
Move operator - reference moved from source to target.
Definition: Reference.cxx:88
Vector of dabc::Reference objects.
Event manipulation API.
Definition: api.h:23