MeVisLab Toolbox Reference
mlRefCountedBase.h
Go to the documentation of this file.
1 /*************************************************************************************
2 **
3 ** Copyright 2011, MeVis Medical Solutions AG
4 **
5 ** The user may use this file in accordance with the license agreement provided with
6 ** the Software or, alternatively, in accordance with the terms contained in a
7 ** written agreement between the user and MeVis Medical Solutions AG.
8 **
9 ** For further information use the contact form at https://www.mevislab.de/contact
10 **
11 **************************************************************************************/
12 
13 #ifndef ML_REF_COUNTED_BASE_H
14 #define ML_REF_COUNTED_BASE_H
15 
17 
18 #include "mlEventSource.h"
19 
20 #include <ThirdPartyWarningsDisable.h>
21 #include <boost/intrusive_ptr.hpp>
22 #include <ThirdPartyWarningsRestore.h>
23 #include <atomic>
24 
25 ML_START_NAMESPACE
26 
27 //----------------------------------------------------------------------
31 {
32 public:
35 
38 
40  virtual void incRefCount() const;
41 
43  virtual void decRefCount() const;
44 
46  bool isRefCountedBase() const override { return true; }
47 
53  static void updateReferenceCountingIfSupported(Base* oldValue, Base* newValue, bool& isRefCountedBase);
54 
58 
59 protected:
61  ~RefCountedBase() override;
62 
64  mutable std::atomic_int_least32_t _refCount;
65 };
66 
67 inline void intrusive_ptr_add_ref(const ML_UTILS_NAMESPACE::RefCountedBase* p)
68 {
69  p->incRefCount();
70 }
71 
72 inline void intrusive_ptr_release(const ML_UTILS_NAMESPACE::RefCountedBase* p)
73 {
74  p->decRefCount();
75 }
76 
77 ML_UTILS_END_NAMESPACE
78 
80 #define ML_REFCOUNTED_PTR(CLASSNAME) \
81  class CLASSNAME; \
82  typedef ::boost::intrusive_ptr<CLASSNAME> CLASSNAME##Ptr; \
83  typedef ::boost::intrusive_ptr<const CLASSNAME> CLASSNAME##ConstPtr;
84 
85 ML_START_NAMESPACE
86  ML_REFCOUNTED_PTR(RefCountedBase)
87 ML_END_NAMESPACE
88 
89 #endif // __mlRefCountedBase_H
90 
91 
Class representing general ML objects that support import/export via strings (setPersistentState() an...
Definition: mlBase.h:59
EventSourceBase class adds event listener handling to Base.
Definition: mlEventSource.h:31
RefCountedBase class adds intrusive reference counting support to the Base class.
virtual void incRefCount() const
Increases the reference count.
~RefCountedBase() override
Macro for the declaration of the runtime type system methods, defined in mlRuntimeSubClass....
static void updateReferenceCountingIfSupported(Base *oldValue, Base *newValue, bool &isRefCountedBase)
Helper method that handles incrementing/decrementing of the reference count if base instances support...
virtual void decRefCount() const
Decreases the reference count (object will be deleted if the reference count is decremented to 0).
RefCountedBase(const RefCountedBase &)
Explicit copy constructor.
bool isRefCountedBase() const override
Returns whether the instance is derived from RefCountedBase.
RefCountedBase()
Constructor.
std::atomic_int_least32_t _refCount
Reference count.
#define ML_ABSTRACT_CLASS_HEADER(className)
Similar to ML_ABSTRACT_CLASS_HEADER_EXPORTED with a non-existing export symbol.
void intrusive_ptr_add_ref(const IntrusivePtrBase< Derived > *ptr)
Provide overloads for reference counting functions used by boost::intrusive_ptr.
void intrusive_ptr_release(const IntrusivePtrBase< Derived > *ptr)
#define ML_REFCOUNTED_PTR(CLASSNAME)
Macro that defines convenience Ptr/ConstPtr typedefs to be used instead of intrusive_ptr templates.
#define ML_UTILS_EXPORT
Definition: mlUtilities.h:18