DABC (Data Acquisition Backbone Core)  2.9.9
Object.h
Go to the documentation of this file.
1 // $Id: Object.h 4472 2020-04-15 13:33:18Z 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_Object
17 #define DABC_Object
18 
19 #ifndef DABC_defines
20 #include "dabc/defines.h"
21 #endif
22 
23 #ifndef DABC_Reference
24 #include "dabc/Reference.h"
25 #endif
26 
27 #ifndef DABC_ConfigIO
28 #include "dabc/ConfigIO.h"
29 #endif
30 
31 namespace dabc {
32 
33  class Manager;
34  class Thread;
35  class Mutex;
36  class Configuration;
37  class ReferencesVector;
38  class HierarchyContainer;
39 
40  extern const char* xmlDeviceNode;
41  extern const char* xmlThreadNode;
42  extern const char* xmlMemoryPoolNode;
43  extern const char* xmlModuleNode;
44  extern const char* xmlConnectionNode;
45 
46  extern const char* xmlQueueAttr;
47  extern const char* xmlBindAttr;
48  extern const char* xmlSignalAttr;
49  extern const char* xmlRateAttr;
50  extern const char* xmlLoopAttr;
51  extern const char* xmlInputQueueSize;
52  extern const char* xmlOutputQueueSize;
53  extern const char* xmlInlineDataSize;
54 
55  extern const char* xmlPoolName;
56  extern const char* xmlWorkPool;
57  extern const char* xmlFixedLayout;
58  extern const char* xmlCleanupTimeout;
59  extern const char* xmlBufferSize;
60  extern const char* xmlNumBuffers;
61  extern const char* xmlNumSegments;
62  extern const char* xmlAlignment;
63  extern const char* xmlShowInfo;
64 
65  extern const char* xmlNumInputs;
66  extern const char* xmlNumOutputs;
67  extern const char* xmlInputPrefix;
68  extern const char* xmlInputMask;
69  extern const char* xmlOutputPrefix;
70  extern const char* xmlOutputMask;
71 
72  extern const char* xmlUseAcknowledge;
73  extern const char* xmlFlushTimeout;
74  extern const char* xmlConnTimeout;
75 
76  // list of parameters, taken from URL
77  // this parameters can be (should be) used in many places, which
78  // allows to specify them through standardized URL syntax
79  extern const char* xmlProtocol;
80  extern const char* xmlHostName;
81  extern const char* xmlFileName; // used as file name option in separate xml node
82  extern const char* xmlFileNumber; // used as file number option in separate xml node
83  extern const char* xmlFileSizeLimit; // used as separate xml node
84  extern const char* xml_maxsize; // used as option in file url
85  extern const char* xml_number; // used as option in file url
86  extern const char* xml_flush; // used as flush attribute in url
87 
88  extern const char* xmlMcastAddr;
89  extern const char* xmlMcastPort;
90  extern const char* xmlMcastRecv;
91 
92  extern const char* typeThread;
93  extern const char* typeDevice;
94  extern const char* typeSocketDevice;
95  extern const char* typeSocketThread;
96  extern const char* typeApplication;
97 
116  class Object {
117  friend class Manager;
118  friend class Reference;
119 
120  private:
121 
123  enum EState {
125  stNormal = 1,
129  stDestructor = 5
130  };
131 
132  static unsigned gNumInstances;
133  static unsigned gNumCreated;
134 
136  void Constructor();
137 
139  void Destructor();
140 
143  bool IncReference(bool withmutex = true);
144 
146  bool DecReference(bool ask_to_destroy, bool do_decrement = true, bool from_thread = false);
147 
149  inline EState GetState() const { return (EState) (fObjectFlags & flStateMask); }
150 
152  inline void SetState(EState st) { fObjectFlags = (fObjectFlags & ~flStateMask) | (unsigned) st ; }
153 
154  static Reference SearchForChild(Reference& ref, const char* name, bool firsttime, bool force) throw();
155 
158  void SetCleanupBit();
159 
160  protected:
161 
162  enum EFlags {
163  flStateMask = 0x000f,
164  flIsOwner = 0x0010,
165  flCleanup = 0x0020,
166  flHasThread = 0x0040,
167  flAutoDestroy = 0x0080,
168  flLogging = 0x0100,
169  flNoMutex = 0x0200,
170  flHidden = 0x0400,
171  flChildsHidden = 0x0800,
172  flTopXmlLevel = 0x1000
173  };
174 
175  unsigned fObjectFlags;
177  std::string fObjectName;
182 
184  inline bool GetFlag(unsigned fl) const { return (fObjectFlags & fl) != 0; }
185 
187  inline void SetFlag(unsigned fl, bool on = true) { fObjectFlags = (on ? (fObjectFlags | fl) : (fObjectFlags & ~fl)); }
188 
190  inline Mutex* ObjectMutex() const { return fObjectMutex; }
191 
193  unsigned NumReferences();
194 
198  virtual bool DestroyByOwnThread() { return false; }
199 
202 
208  virtual void ObjectCleanup();
209 
214  virtual bool _DoDeleteItself() { return false; }
215 
217  void SetOwner(bool on = true);
218 
221  void SetAutoDestroy(bool on = true);
222 
226  virtual void ObjectDestroyed(Object*) {}
227 
231  bool IsNormalState();
232 
235  void FillFullName(std::string &fullname, Object* upto, bool exclude_top_parent = false) const;
236 
238  bool _IsNormalState();
239 
241  virtual Object *CreateInstance(const std::string &name) { return new Object(nullptr, name); }
242 
244  virtual void _ChildsChanged() {}
245 
248  friend class Object;
249 
250  private:
252  std::string name;
253 
255  ConstructorPair(const ConstructorPair& src) : parent(src.parent), name(src.name) {}
256  };
257 
264  static ConstructorPair MakePair(Reference prnt, const std::string &fullname, bool withmanager = true);
265 
268  static ConstructorPair MakePair(Object* prnt, const std::string &fullname, bool withmanager = true);
269 
272  static ConstructorPair MakePair(const std::string &fullname, bool withmanager = true);
273 
274  Object(const ConstructorPair& pair, unsigned flags = flIsOwner);
275 
276  public:
277 
278  Object(const std::string &name, unsigned flags = flIsOwner);
279 
280  Object(Reference parent, const std::string &name, unsigned flags = flIsOwner);
281 
282  // FIXME: one should find a way to catch a call to the destructor
283  virtual ~Object();
284 
286  Object* GetParent() const { return fObjectParent(); }
287 
290 
292  bool IsParent(Object* obj) const;
293 
295  const char* GetName() const { return fObjectName.c_str(); }
296 
298  bool IsName(const char* str) const { return fObjectName.compare(str)==0; }
299 
301  bool IsName(const std::string &str) const { return fObjectName.compare(str)==0; }
302 
310  bool IsName(const char* str, int len) const;
311 
313  bool IsNameMatch(const std::string &mask) const;
314 
316  bool IsOwner() const;
317 
319  bool IsLogging() const;
320 
322  void SetLogging(bool on = true);
323 
325  bool IsHidden() const;
326 
328  bool IsChildsHidden() const;
329 
331  bool IsTopXmlLevel() const;
332 
333  // List of children is __thread safe__ BUT may change in any time in between two calls
334  // To perform some complex actions, references on child objects should be used
335 
336 
337  // TODO: should it be public?
338  // TODO: do we need another method
339 
345  bool AddChild(Object* child, bool withmutex = true) throw();
346 
353  bool AddChildAt(Object* child, unsigned pos, bool withmutex = true);
354 
357  bool RemoveChild(Object* child, bool cleanup = true) throw();
358 
361  bool RemoveChildAt(unsigned n, bool cleanup = true) throw();
362 
365  bool RemoveChilds(bool cleanup = true);
366 
368  unsigned NumChilds() const;
369 
371  Object* GetChild(unsigned n) const;
372 
374  Reference GetChildRef(unsigned n) const;
375 
376  bool GetAllChildRef(ReferencesVector* vect) const;
377 
379  Object* FindChild(const char* name) const;
380 
381  //TODO: one need method reverse to FindChild - MakePath()
382  // one should be able to produce string which can be used to find specified object
383 
385  Reference FindChildRef(const char* name, bool force = false) const throw();
386 
392  Reference GetFolder(const std::string &name, bool force = false) throw();
393 
395  virtual void Print(int lvl = 0);
396 
403  std::string ItemName(bool compact = true) const;
404 
405  // ALL following methods about object destroyment and cleanup
406 
414  static void Destroy(Object* obj) throw();
415 
419  virtual const char* ClassName() const { return "Object"; }
420 
424  virtual bool Find(ConfigIO &cfg);
425 
428  virtual void BuildFieldsMap(RecordFieldsMap* cont) {}
429 
430  // operations with object name (and info) are __not thread safe__
431  // therefore, in the case when object name must be changed,
432  // locking should be applied by any other means
433 
435  static unsigned NumInstances() { return gNumInstances; }
436 
443  static void InspectGarbageCollector();
444 
448  static bool NameMatch(const std::string &name, const std::string &mask);
449 
453  static bool NameMatchM(const std::string &name, const std::string &mask);
454 
455 #ifdef DABC_EXTRA_CHECKS
456  static void DebugObject(const char* classname = 0, Object* instance = 0, int kind = 0);
457 #endif
458 
459  protected:
461  void SetName(const char* name);
462 
464  void SetNameDirect(const char* name);
465 
467  void DeleteThis();
468  };
469 };
470 
471 #endif
Interface class between xml configuration and dabc objects.
Definition: ConfigIO.h:38
Manager of everything in DABC
Definition: Manager.h:291
posix pthread mutex
Definition: threads.h:61
Base class for most of the DABC classes.
Definition: Object.h:116
Reference fObjectParent
reference on the parent object
Definition: Object.h:176
bool RemoveChild(Object *child, bool cleanup=true)
Detach child from parent object If cleanup==true and parent is owner of child, child will be destroye...
Definition: Object.cxx:604
bool IsName(const char *str) const
Checks if object name is same as provided string, thread safe
Definition: Object.h:298
void Destructor()
Destroys all internal data, reentrant.
Definition: Object.cxx:225
bool IsOwner() const
Returns true if object is owner of its children, thread safe
Definition: Object.cxx:167
bool IsTopXmlLevel() const
Return true if object should be searched in the top level of the xml file, thread safe
Definition: Object.cxx:185
ReferencesVector * fObjectChilds
list of the child objects
Definition: Object.h:180
virtual Object * CreateInstance(const std::string &name)
Method used to create new item to be placed as child of the object.
Definition: Object.h:241
bool IncReference(bool withmutex=true)
Increments reference counter, return false if it cannot be done.
Definition: Object.cxx:278
void FillFullName(std::string &fullname, Object *upto, bool exclude_top_parent=false) const
Method used to produce full item name,.
Definition: Object.cxx:1061
void SetFlag(unsigned fl, bool on=true)
Change value of selected flag, not thread safe
Definition: Object.h:187
unsigned fObjectFlags
flag, protected by the mutex
Definition: Object.h:175
std::string ItemName(bool compact=true) const
Produce string, which can be used as name argument in dabc::mgr.FindItem(name) call.
Definition: Object.cxx:1076
Object * GetParent() const
Returns pointer on parent object, thread safe
Definition: Object.h:286
bool DecReference(bool ask_to_destroy, bool do_decrement=true, bool from_thread=false)
Decrements reference counter, return true if object must be destroyed.
Definition: Object.cxx:317
unsigned NumReferences()
Return number of references on the object.
Definition: Object.cxx:557
virtual void Print(int lvl=0)
Print object content on debug output.
Definition: Object.cxx:1092
void Constructor()
Initializes all variables of the object.
Definition: Object.cxx:210
std::string fObjectName
object name
Definition: Object.h:177
virtual ~Object()
Definition: Object.cxx:147
virtual bool DestroyByOwnThread()
Internal DABC method, used to activate object cleanup via object thread Returns: false - object canno...
Definition: Object.h:198
bool GetFlag(unsigned fl) const
Return value of selected flag, not thread safe
Definition: Object.h:184
bool RemoveChildAt(unsigned n, bool cleanup=true)
Detach child object from parent at specified position If cleanup==true and object is owner of child,...
Definition: Object.cxx:665
virtual void _ChildsChanged()
Method called when new childs are add or old are removed.
Definition: Object.h:244
Reference GetChildRef(unsigned n) const
returns reference on child object
Definition: Object.cxx:684
static bool NameMatchM(const std::string &name, const std::string &mask)
Check if name matches to specified mask.
Definition: Object.cxx:1028
Mutex * fObjectMutex
mutex protects all private property of the object
Definition: Object.h:178
void SetNameDirect(const char *name)
Changes object name disregard of existing references.
Definition: Object.cxx:884
static unsigned gNumCreated
number of created instances, will used for object id
Definition: Object.h:133
void SetState(EState st)
Set object state value.
Definition: Object.h:152
Object(const ConstructorPair &pair, unsigned flags=flIsOwner)
Definition: Object.cxx:132
void SetLogging(bool on=true)
Sets logging flag, thread safe
Definition: Object.cxx:197
const char * GetName() const
Returns name of the object, thread safe
Definition: Object.h:295
void SetAutoDestroy(bool on=true)
Set autodestroy flag for the object Once enabled, object will be destroyed when last reference will b...
Definition: Object.cxx:160
Mutex * ObjectMutex() const
Returns mutex, used for protection of Object data members.
Definition: Object.h:190
static void InspectGarbageCollector()
\ brief Methods to inspect how many objects pointers are remained
Definition: Object.cxx:1099
virtual void BuildFieldsMap(RecordFieldsMap *cont)
Fill fields map, which is relevant for the object Objects hierarchy produced from dabc::Manager.
Definition: Object.h:428
int fObjectRefCnt
accounts how many references existing on the object, thread safe
Definition: Object.h:179
static ConstructorPair MakePair(Reference prnt, const std::string &fullname, bool withmanager=true)
Internal DABC method, used to produce pair - object parent and object name, which is typically should...
Definition: Object.cxx:934
virtual void ObjectCleanup()
User method to cleanup object content before it will be destroyed Main motivation is to release any r...
Definition: Object.cxx:532
bool DestroyCalledFromOwnThread()
Internal DABC method, should be called by thread which was requested to destroy object.
Definition: Object.cxx:504
bool IsNormalState()
Return true if object is in normal state.
Definition: Object.cxx:309
@ flChildsHidden
hide all childs from hierarchy scan
Definition: Object.h:171
@ flTopXmlLevel
object (or folder) can be found on top xml level in the Context
Definition: Object.h:172
@ flStateMask
use 4 bits for state
Definition: Object.h:163
@ flNoMutex
object will be created without mutex, only can be used in constructor
Definition: Object.h:169
@ flHasThread
flag indicates that object has thread and should be cleaned up via thread
Definition: Object.h:166
@ flAutoDestroy
object will be automatically destroyed when no references exists, normally set in constructor,...
Definition: Object.h:167
@ flIsOwner
flag indicates default ownership for child objects
Definition: Object.h:164
@ flHidden
hide object from hierarchy scan
Definition: Object.h:170
@ flLogging
object is marked to provide logging information, for debug purposes only
Definition: Object.h:168
@ flCleanup
flag indicates that one should cleanup pointer from depended objects
Definition: Object.h:165
bool IsParent(Object *obj) const
Checks if specified argument is in the list of object parents.
Definition: Object.cxx:985
Reference GetFolder(const std::string &name, bool force=false)
Return folder of specified name, no special symbols allowed.
Definition: Object.cxx:814
virtual bool _DoDeleteItself()
This method is called at the moment when DecReference think that object can be destroyed and wants to...
Definition: Object.h:214
static bool NameMatch(const std::string &name, const std::string &mask)
Check if name matches to specified mask.
Definition: Object.cxx:1004
bool AddChild(Object *child, bool withmutex=true)
Add object to list of child objects, thread safe
Definition: Object.cxx:565
Reference FindChildRef(const char *name, bool force=false) const
returns reference on child object with given name
Definition: Object.cxx:735
bool _IsNormalState()
Same as IsNormalState() but without mutex lock - user should lock mutex himself.
Definition: Object.cxx:303
static Reference SearchForChild(Reference &ref, const char *name, bool firsttime, bool force)
Definition: Object.cxx:742
bool RemoveChilds(bool cleanup=true)
Remove all childs.
Definition: Object.cxx:821
virtual const char * ClassName() const
Returns class name of the object instance.
Definition: Object.h:419
static void Destroy(Object *obj)
User method for object destroyment.
Definition: Object.cxx:921
static unsigned NumInstances()
Static variable counts total number of objects instances.
Definition: Object.h:435
bool IsNameMatch(const std::string &mask) const
Check if object name match to the mask.
Definition: Object.cxx:999
bool AddChildAt(Object *child, unsigned pos, bool withmutex=true)
Add object to list of child objects at specified position.
Definition: Object.cxx:570
void SetOwner(bool on=true)
Specifies if object will be owner of its new childs.
Definition: Object.cxx:154
EState GetState() const
Returns object state value.
Definition: Object.h:149
bool IsChildsHidden() const
Return true if object wants to hide childs from hierarchy scan, thread safe
Definition: Object.cxx:179
static unsigned gNumInstances
actual number of existing instances
Definition: Object.h:132
bool GetAllChildRef(ReferencesVector *vect) const
Definition: Object.cxx:701
bool IsName(const std::string &str) const
Checks if object name is same as provided string, thread safe
Definition: Object.h:301
virtual bool Find(ConfigIO &cfg)
Method to locate object in xml file.
Definition: Object.cxx:929
virtual void ObjectDestroyed(Object *)
Method called by the manager when registered dependent object is destroyed Should be used in user cla...
Definition: Object.h:226
bool IsLogging() const
Return true if object selected for logging, thread safe
Definition: Object.cxx:173
bool IsHidden() const
Return true if object wants to be hidden from hierarchy scan, thread safe
Definition: Object.cxx:191
void SetCleanupBit()
Method set cleanup bit that object will be cleaned up in all registered objects Used only by manager ...
Definition: Object.cxx:297
void DeleteThis()
Method should be used by the object to delete itself.
Definition: Object.cxx:509
void SetName(const char *name)
Changes object name.
Definition: Object.cxx:871
EState
These are object live stages.
Definition: Object.h:123
@ stWaitForThread
object must be cleaned by the thread
Definition: Object.h:126
@ stConstructor
state during constructor
Definition: Object.h:124
@ stDestructor
state during destructor
Definition: Object.h:129
@ stWaitForDestructor
one waits unit refcounter decreases
Definition: Object.h:128
@ stDoingDestroy
we are inside destroy method
Definition: Object.h:127
@ stNormal
state during object normal functioning
Definition: Object.h:125
Reference GetParentRef() const
Definition: Object.h:289
int fObjectBlock
counter for blocking calls, as long as non-zero, non of child can be removed
Definition: Object.h:181
unsigned NumChilds() const
returns number of child objects
Definition: Object.cxx:670
Object * FindChild(const char *name) const
returns pointer on child object with given name
Definition: Object.cxx:730
Object * GetChild(unsigned n) const
returns pointer on child object
Definition: Object.cxx:677
Reference on the arbitrary object
Definition: Reference.h:73
Vector of dabc::Reference objects.
Event manipulation API.
Definition: api.h:23
const char * xmlWorkPool
Definition: Object.cxx:46
const char * xmlBufferSize
Definition: Object.cxx:49
const char * xmlNumOutputs
Definition: Object.cxx:55
const char * xmlOutputQueueSize
Definition: Object.cxx:42
const char * typeSocketThread
Definition: Object.cxx:80
const char * xml_maxsize
Definition: Object.cxx:73
const char * xmlFileName
Definition: Object.cxx:70
const char * xmlFixedLayout
Definition: Object.cxx:47
const char * xmlNumSegments
const char * typeSocketDevice
Definition: Object.cxx:79
const char * xmlInputMask
Definition: Object.cxx:57
const char * xmlOutputMask
Definition: Object.cxx:59
const char * xmlRateAttr
Definition: Object.cxx:39
const char * xmlMcastAddr
Definition: Object.cxx:64
const char * xml_flush
Definition: Object.cxx:75
const char * xmlConnectionNode
Definition: Object.cxx:34
const char * xmlAlignment
Definition: Object.cxx:51
const char * xmlSignalAttr
Definition: Object.cxx:38
const char * xmlUseAcknowledge
Definition: Object.cxx:60
const char * xmlFlushTimeout
Definition: Object.cxx:61
const char * xmlOutputPrefix
Definition: Object.cxx:58
const char * xmlInputQueueSize
Definition: Object.cxx:41
const char * xmlLoopAttr
Definition: Object.cxx:40
const char * xmlBindAttr
Definition: Object.cxx:37
const char * xmlProtocol
Definition: Object.cxx:68
const char * xmlCleanupTimeout
Definition: Object.cxx:48
const char * xmlInlineDataSize
Definition: Object.cxx:43
const char * xml_number
Definition: Object.cxx:74
const char * xmlMemoryPoolNode
Definition: Object.cxx:32
const char * xmlNumBuffers
Definition: Object.cxx:50
const char * xmlShowInfo
Definition: Object.cxx:52
const char * xmlMcastPort
Definition: Object.cxx:65
const char * typeThread
Definition: Object.cxx:77
const char * xmlModuleNode
Definition: Object.cxx:33
const char * xmlPoolName
Definition: Object.cxx:45
const char * xmlConnTimeout
Definition: Object.cxx:62
const char * xmlFileNumber
Definition: Object.cxx:71
const char * xmlFileSizeLimit
Definition: Object.cxx:72
const char * xmlInputPrefix
Definition: Object.cxx:56
const char * xmlHostName
Definition: Object.cxx:69
const char * xmlMcastRecv
Definition: Object.cxx:66
const char * xmlQueueAttr
Definition: Object.cxx:36
const char * xmlDeviceNode
Definition: Object.cxx:30
const char * xmlThreadNode
Definition: Object.cxx:31
const char * xmlNumInputs
Definition: Object.cxx:54
const char * typeDevice
Definition: Object.cxx:78
const char * typeApplication
Definition: Object.cxx:81
Structure used to specify arguments for object constructor.
Definition: Object.h:247
ConstructorPair(const ConstructorPair &src)
Definition: Object.h:255