MeVisLabToolboxReference
MeVisLab/Standard/Sources/ML/MLVesselGraph/Properties/mlIntrusivePtrBase.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //----------------------------------------------------------------------------------
00004 
00009 //----------------------------------------------------------------------------------
00010 
00011 #ifndef __mlIntrusivePtrBase_H_
00012 #define __mlIntrusivePtrBase_H_
00013 
00014 #include "mlVesselGraphSystem.h"
00015 #include <mlTypeDefs.h>
00016 #include "mlSystemWarningsDisable.h"
00017 #include <boost/thread/mutex.hpp>
00018 #include "mlSystemWarningsRestore.h"
00019 
00020 #ifdef WIN32
00021 #pragma warning( push )
00022 #pragma warning( disable : 4396 )
00023 #endif
00024 
00025 ML_START_NAMESPACE
00026 
00027 // Some forward declarations, which are required to be able to declare the required friend functions for boost::intrusive_ptr.
00028 
00029 // Forward declaration of our templated (see below) base class.
00030 template<typename Derived>
00031 class IntrusivePtrBase;
00032 
00033 // Forward declaration of the functions used by boost::intrusive_ptr
00034 template<typename Derived>
00035 void intrusive_ptr_add_ref(const IntrusivePtrBase<Derived>* ptr);
00036 template<typename Derived>
00037 void intrusive_ptr_release(const IntrusivePtrBase<Derived>* ptr);
00038 
00042 template<typename Derived>
00043 class IntrusivePtrBase {
00044 protected:
00045   IntrusivePtrBase(): _referenceCount(0) {};
00046 
00047 private:
00049   friend void intrusive_ptr_add_ref<>(const IntrusivePtrBase<Derived>* ptr);
00050   friend void intrusive_ptr_release<>(const IntrusivePtrBase<Derived>* ptr);
00051 
00052   mutable long _referenceCount;
00053   mutable boost::mutex _referenceCountMutex;
00054 };
00055 
00057 template<typename Derived>
00058 inline void intrusive_ptr_add_ref(const IntrusivePtrBase<Derived>* ptr) { ptr->_referenceCountMutex.lock(); ptr->_referenceCount++; ptr->_referenceCountMutex.unlock(); }
00059 template<typename Derived>
00060 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); }
00061 
00062 ML_END_NAMESPACE
00063 
00064 #ifdef WIN32
00065 #pragma warning( pop )
00066 #endif
00067 
00068 #endif // __mlIntrusivePtrBase_H_