00001 /*============================================================================= 00002 Copyright (C) 2012 Allied Vision Technologies. All Rights Reserved. 00003 00004 Redistribution of this file, in original or modified form, without 00005 prior written consent of Allied Vision Technologies is prohibited. 00006 00007 ------------------------------------------------------------------------------- 00008 00009 File: SharedPointer.h 00010 00011 Description: Definition of an example shared pointer class that can be 00012 used with the Vimba CPP API. 00013 (This include file contains example code only.) 00014 00015 ------------------------------------------------------------------------------- 00016 00017 THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED 00018 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF TITLE, 00019 NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00020 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 00021 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00022 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00023 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 00024 AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 00025 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00026 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 00028 =============================================================================*/ 00029 00030 #ifndef AVT_VMBAPI_SHAREDPOINTER_H 00031 #define AVT_VMBAPI_SHAREDPOINTER_H 00032 00033 #include <VimbaCPP/Include/Mutex.h> 00034 00035 namespace AVT { 00036 namespace VmbAPI { 00037 00038 //Coding style of this file is different to mimic the shared_ptr classes of std and boost. 00039 00040 struct dynamic_cast_tag 00041 { 00042 }; 00043 00044 class ref_count_base 00045 { 00046 public: 00047 virtual ~ref_count_base() {;} 00048 00049 virtual void inc() = 0; 00050 virtual void dec() = 0; 00051 virtual long use_count() const = 0; 00052 }; 00053 00054 template <class T> 00055 class ref_count : public virtual AVT::VmbAPI::ref_count_base 00056 { 00057 private: 00058 T *m_pObject; 00059 long m_nCount; 00060 Mutex m_Mutex; 00061 00062 ref_count(const ref_count &rRefCount); 00063 ref_count& operator = (const ref_count &rRefCount); 00064 00065 public: 00066 explicit ref_count(T *pObject); 00067 virtual ~ref_count(); 00068 00069 virtual void inc(); 00070 virtual void dec(); 00071 virtual long use_count() const; 00072 }; 00073 00074 template <class T> 00075 class shared_ptr 00076 { 00077 private: 00078 typedef shared_ptr<T> this_type; 00079 00080 template<class T2> 00081 friend class shared_ptr; 00082 00083 AVT::VmbAPI::ref_count_base *m_pRefCount; 00084 T *m_pObject; 00085 00086 template <class T2> 00087 static void swap(T2 &rValue1, T2 &rValue2); 00088 00089 public: 00090 shared_ptr(); 00091 template <class T2> 00092 explicit shared_ptr(T2 *pObject); 00093 shared_ptr(const shared_ptr &rSharedPointer); 00094 template <class T2> 00095 shared_ptr(const shared_ptr<T2> &rSharedPointer); 00096 template <class T2> 00097 shared_ptr(const shared_ptr<T2> &rSharedPointer, dynamic_cast_tag); 00098 00099 virtual ~shared_ptr(); 00100 00101 shared_ptr& operator = (const shared_ptr &rSharedPointer); 00102 template <class T2> 00103 shared_ptr& operator = (const shared_ptr<T2> &rSharedPointer); 00104 00105 void reset(); 00106 template <class T2> 00107 void reset(T2 *pObject); 00108 00109 T* get() const; 00110 T& operator * () const; 00111 T* operator -> () const; 00112 long use_count() const; 00113 bool unique() const; 00114 00115 typedef T* this_type::*unspecified_bool_type; 00116 00117 operator unspecified_bool_type () const 00118 { 00119 if(m_pObject == 0) 00120 { 00121 return 0; 00122 } 00123 00124 return &this_type::m_pObject; 00125 } 00126 00127 void swap(shared_ptr &rSharedPointer); 00128 }; 00129 00130 template<class T, class T2> 00131 shared_ptr<T> dynamic_pointer_cast(const shared_ptr<T2> &rSharedPointer); 00132 00133 template<class T1, class T2> 00134 bool operator==(const shared_ptr<T1>& sp1, const shared_ptr<T2>& sp2); 00135 template<class T1, class T2> 00136 bool operator!=(const shared_ptr<T1>& sp1, const shared_ptr<T2>& sp2); 00137 00138 }} //namespace AVT::VmbAPI 00139 00140 #include <VimbaCPP/Include/SharedPointer_impl.h> 00141 00142 #endif //AVT_VMBAPI_SHAREDPOINTER_H