DABC (Data Acquisition Backbone Core)  2.9.9
Reference.cxx
Go to the documentation of this file.
1 // $Id: Reference.cxx 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 #include "dabc/Reference.h"
17 
18 #include <cstdio>
19 #include <cstring>
20 
21 #include "dabc/Object.h"
22 #include "dabc/Exception.h"
23 #include "dabc/logging.h"
24 
26  fObj(0)
27 {
28  // this is empty reference
29  if (obj==0) return;
30 
31  if (obj->IncReference())
32  fObj = obj;
33  else
34  throw dabc::Exception(ex_Object, dabc::format("Cannot assign reference to object %p", obj), obj->GetName() );
35 }
36 
37 void dabc::Reference::SetObject(Object* obj, bool withmutex)
38 {
39  Release();
40 
41  // this is empty reference
42  if (obj==0) return;
43 
44  if (obj->IncReference(withmutex))
45  fObj = obj;
46  else
47  throw dabc::Exception(ex_Object, dabc::format("Cannot assign reference to object %p"), DNAME(obj) );
48 }
49 
51 {
52  if (src.fObj->IncReference())
53  fObj = src.fObj;
54  else
55  throw dabc::Exception(ex_Object, dabc::format("Cannot assign reference to object %p", src.fObj), src.GetName());
56 }
57 
59  fObj(0)
60 {
61  if (!src.null()) Assign(src);
62 }
63 
65 {
66 // Print(0, "From operator=()");
67 
68  Release();
69 
70  if (!src.null()) Assign(src);
71 
72  return *this;
73 }
74 
76 {
77  Release();
78 
79  if (obj!=0) {
80  Reference ref(obj);
81  *this << ref;
82  }
83 
84  return *this;
85 }
86 
87 
89 {
90  Release();
91 
92  // we are trying to avoid situation, that both references have same pointer in a time
93  Object* temp = src.fObj;
94 
95  src.fObj = 0;
96 
97  fObj = temp;
98 
99  return *this;
100 }
101 
103 {
104  dabc::Reference res;
105  res.fObj = fObj;
106  fObj = 0;
107 
108  return res;
109 }
110 
111 
113 {
114  return GetObject() ? GetObject()->NumReferences() : 0;
115 }
116 
117 
119 {
120  if (GetObject()) GetObject()->SetAutoDestroy(on);
121 }
122 
124 {
125  if (null() || !ref.null()) return false;
126  if (!fObj->IncReference(false))
127  throw dabc::Exception(ex_Object, dabc::format("Cannot assign reference to object %p without mutex", fObj), DNAME(fObj));
128 
129  ref.fObj = fObj;
130  return true;
131 }
132 
134 {
135  Release();
136 }
137 
139 {
140  if (fObj==0) return;
141  if (fObj->DecReference(false))
142  // special case - object is not referenced and want to be destroyed - let help him
143  delete fObj;
144  fObj = 0;
145 }
146 
147 
149 {
150  if (fObj==0) return;
151 
152  // we ask if object can be destroyed when reference is decreased
153  if (fObj->DecReference(true))
154  // if yes, state of the object will be blocked - now new references and one can delete object
155  // but it does not means that object disappear immediately - it may be cleanup in its own thread
156  delete fObj;
157 
158  fObj = 0;
159 }
160 
161 
163 {
164  return GetObject() ? GetObject()->GetParent() : 0;
165 }
166 
167 const char* dabc::Reference::GetName() const
168 {
169  return GetObject() ? GetObject()->GetName() : "---";
170 }
171 
172 const char* dabc::Reference::ClassName() const
173 {
174  return GetObject() ? GetObject()->ClassName() : "---";
175 }
176 
177 bool dabc::Reference::IsName(const char* name) const
178 {
179  if ((name==0) || (*name==0) || (GetObject()==0)) return false;
180  return GetObject()->IsName(name);
181 }
182 
184 {
185  return GetObject() ? GetObject()->ObjectMutex() : 0;
186 }
187 
189 {
190  return GetObject() ? GetObject()->AddChild(obj, true) : false;
191 }
192 
193 
195 {
196  return GetObject() ? GetObject()->NumChilds() : 0;
197 }
198 
200 {
201  return GetObject() ? GetObject()->GetChildRef(n) : Reference();
202 }
203 
205 {
206  if ((GetObject()==0) || (vect==0)) return false;
207  return GetObject()->GetAllChildRef(vect);
208 }
209 
211 {
212  return GetObject() ? GetObject()->FindChildRef(name) : Reference();
213 }
214 
215 bool dabc::Reference::RemoveChild(const char* name, bool cleanup)
216 {
217  if (!GetObject()) return false;
218 
219  return GetObject()->RemoveChild(GetObject()->FindChild(name), cleanup);
220 }
221 
222 
224 {
225  return GetObject() ? GetObject()->RemoveChilds(cleanup) : true;
226 }
227 
228 
229 void dabc::Reference::Print(int lvl, const char* from) const
230 {
231  dabc::lgr()->Debug(lvl,"filename",1,"funcname", dabc::format("%s REF:%p obj:%p", (from ? from : ""), this, fObj).c_str());
232 }
233 
234 dabc::Reference dabc::Reference::GetFolder(const std::string &name, bool force) throw()
235 {
236  if (GetObject()==0) return Reference();
237 
238  return GetObject()->GetFolder(name, force);
239 }
240 
241 std::string dabc::Reference::ItemName(bool compact) const
242 {
243  if (GetObject()==0) return std::string();
244 
245  return GetObject()->ItemName(compact);
246 }
247 
249 {
250  if (null() || topitem.null() || (topitem==*this)) return "";
251 
252  std::string res;
253 
254  GetObject()->FillFullName(res, topitem(), true);
255 
256  return res;
257 }
DABC exception.
Definition: Exception.h:57
static void Debug(int level, const char *filename, unsigned linenumber, const char *funcname, const char *message)
Definition: logging.h:106
posix pthread mutex
Definition: threads.h:61
Base class for most of the DABC classes.
Definition: Object.h:116
bool IncReference(bool withmutex=true)
Increments reference counter, return false if it cannot be done.
Definition: Object.cxx:278
Object * GetParent() const
Returns pointer on parent object, thread safe
Definition: Object.h:286
const char * GetName() const
Returns name of the object, thread safe
Definition: Object.h:295
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
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
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
Reference(Object *obj=nullptr)
Constructor, creates reference on the object.
Definition: Reference.cxx:25
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
bool null() const
Returns true if reference contains nullptr.
Definition: Reference.h:151
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
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.
#define DNAME(arg)
Definition: logging.h:193
std::string format(const char *fmt,...)
Definition: string.cxx:49
@ ex_Object
Definition: Exception.h:38
Logger * lgr()
Definition: logging.cxx:483