MeVisLab Toolbox Reference
mlIntrusivePtrBase.h
Go to the documentation of this file.
1 /*************************************************************************************
2 **
3 ** Copyright 2010, 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_INTRUSIVE_PTR_BASE_H
14 #define ML_INTRUSIVE_PTR_BASE_H
15 
16 
18 
19 #include "mlVesselGraphSystem.h"
20 #include "VesselGraphTypes.h"
22 #include <boost/thread/mutex.hpp>
24 
25 #ifdef WIN32
26 #pragma warning( push )
27 #pragma warning( disable : 4396 )
28 #endif
29 
31 
32 // Some forward declarations, which are required to be able to declare the required friend functions for boost::intrusive_ptr.
33 
34 // Forward declaration of our templated (see below) base class.
35 template<typename Derived>
36 class IntrusivePtrBase;
37 
38 // Forward declaration of the functions used by boost::intrusive_ptr
39 template<typename Derived>
41 template<typename Derived>
43 
47 template<typename Derived>
49 protected:
50  IntrusivePtrBase(): _referenceCount(0) {};
51 
52 private:
54  friend void intrusive_ptr_add_ref<>(const IntrusivePtrBase<Derived>* ptr);
55  friend void intrusive_ptr_release<>(const IntrusivePtrBase<Derived>* ptr);
56 
57  mutable long _referenceCount;
58  mutable boost::mutex _referenceCountMutex;
59 };
60 
62 template<typename Derived>
63 inline void intrusive_ptr_add_ref(const IntrusivePtrBase<Derived>* ptr) { ptr->_referenceCountMutex.lock(); ptr->_referenceCount++; ptr->_referenceCountMutex.unlock(); }
64 template<typename Derived>
65 inline void intrusive_ptr_release(const IntrusivePtrBase<Derived>* ptr) { ptr->_referenceCountMutex.lock(); ptr->_referenceCount--; ptr->_referenceCountMutex.unlock(); if (ptr->_referenceCount==0) delete static_cast<const Derived*>(ptr); }
66 
68 
69 #ifdef WIN32
70 #pragma warning( pop )
71 #endif
72 
73 #endif // __mlIntrusivePtrBase_H_
A base-class enabling the use of boost::intrusive_ptr in derived classes.
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 VESSELGRAPH_END_NAMESPACE
#define VESSELGRAPH_BEGIN_NAMESPACE