SharedPointer_impl.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*=============================================================================
  2. Copyright (C) 2012 - 2017 Allied Vision Technologies. All Rights Reserved.
  3. Redistribution of this file, in original or modified form, without
  4. prior written consent of Allied Vision Technologies is prohibited.
  5. -------------------------------------------------------------------------------
  6. File: SharedPointer_impl.h
  7. Description: Implementation of an example shared pointer class for the Vimba
  8. CPP API.
  9. (This include file contains example code only.)
  10. -------------------------------------------------------------------------------
  11. THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
  12. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF TITLE,
  13. NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  14. DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  15. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  16. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  17. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  18. AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  19. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  20. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. =============================================================================*/
  22. #ifndef AVT_VMBAPI_SHAREDPOINTER_IMPL_H
  23. #define AVT_VMBAPI_SHAREDPOINTER_IMPL_H
  24. #include <VimbaCPP/Include/SharedPointer.h>
  25. #include <stdexcept>
  26. namespace AVT {
  27. namespace VmbAPI {
  28. template <class T>
  29. ref_count<T>::ref_count(const ref_count &rRefCount)
  30. {
  31. }
  32. template <class T>
  33. ref_count<T>& ref_count<T>::operator = (const ref_count &rRefCount)
  34. {
  35. //This is a dummy method to satisfy the compiler
  36. return *this;
  37. }
  38. template <class T>
  39. ref_count<T>::ref_count(T *pObject)
  40. : m_pObject(pObject)
  41. , m_nCount(1)
  42. {
  43. }
  44. template <class T>
  45. ref_count<T>::~ref_count()
  46. {
  47. if(NULL != m_pObject)
  48. {
  49. delete m_pObject;
  50. }
  51. m_Mutex.Unlock();
  52. }
  53. template <class T>
  54. void ref_count<T>::inc()
  55. {
  56. m_Mutex.Lock();
  57. m_nCount++;
  58. m_Mutex.Unlock();
  59. }
  60. template <class T>
  61. void ref_count<T>::dec()
  62. {
  63. m_Mutex.Lock();
  64. if( m_nCount == 0 )
  65. {
  66. throw std::logic_error("shared pointer, used incorectly");
  67. }
  68. if(m_nCount > 1)
  69. {
  70. m_nCount--;
  71. m_Mutex.Unlock();
  72. }
  73. else
  74. {
  75. // m_Mutex will be unlocked in d'tor
  76. delete this;
  77. }
  78. }
  79. template <class T>
  80. long ref_count<T>::use_count() const
  81. {
  82. return m_nCount;
  83. }
  84. template <class T>
  85. template <class T2>
  86. void shared_ptr<T>::swap(T2 &rValue1, T2 &rValue2)
  87. {
  88. T2 buffer = rValue1;
  89. rValue1 = rValue2;
  90. rValue2 = buffer;
  91. }
  92. template <class T>
  93. shared_ptr<T>::shared_ptr()
  94. : m_pRefCount(NULL)
  95. , m_pObject(NULL)
  96. {
  97. }
  98. template <class T>
  99. template <class T2>
  100. shared_ptr<T>::shared_ptr(T2 *pObject)
  101. : m_pRefCount(NULL)
  102. , m_pObject(NULL)
  103. {
  104. m_pRefCount = new ref_count<T2>(pObject);
  105. if(NULL == m_pRefCount)
  106. {
  107. delete pObject;
  108. throw std::bad_alloc();
  109. }
  110. m_pObject = pObject;
  111. }
  112. template <class T>
  113. template <class T2>
  114. shared_ptr<T>::shared_ptr(const shared_ptr<T2> &rSharedPointer)
  115. : m_pRefCount(NULL)
  116. , m_pObject(NULL)
  117. {
  118. if(NULL != rSharedPointer.m_pRefCount)
  119. {
  120. rSharedPointer.m_pRefCount->inc();
  121. m_pRefCount = rSharedPointer.m_pRefCount;
  122. m_pObject = rSharedPointer.m_pObject;
  123. }
  124. }
  125. template <class T>
  126. template <class T2>
  127. shared_ptr<T>::shared_ptr(const shared_ptr<T2> &rSharedPointer, dynamic_cast_tag)
  128. : m_pRefCount(NULL)
  129. , m_pObject(NULL)
  130. {
  131. if(NULL != rSharedPointer.m_pRefCount)
  132. {
  133. T *pObject = dynamic_cast<T*>(rSharedPointer.m_pObject);
  134. if(NULL != pObject)
  135. {
  136. rSharedPointer.m_pRefCount->inc();
  137. m_pRefCount = rSharedPointer.m_pRefCount;
  138. m_pObject = pObject;
  139. }
  140. }
  141. }
  142. template <class T>
  143. shared_ptr<T>::shared_ptr(const shared_ptr &rSharedPointer)
  144. : m_pRefCount(NULL)
  145. , m_pObject(NULL)
  146. {
  147. if(NULL != rSharedPointer.m_pRefCount)
  148. {
  149. rSharedPointer.m_pRefCount->inc();
  150. m_pRefCount = rSharedPointer.m_pRefCount;
  151. m_pObject = rSharedPointer.m_pObject;
  152. }
  153. }
  154. template <class T>
  155. shared_ptr<T>::~shared_ptr()
  156. {
  157. if(NULL != m_pRefCount)
  158. {
  159. m_pRefCount->dec();
  160. m_pRefCount = NULL;
  161. m_pObject = NULL;
  162. }
  163. }
  164. template <class T>
  165. template <class T2>
  166. shared_ptr<T>& shared_ptr<T>::operator = (const shared_ptr<T2> &rSharedPointer)
  167. {
  168. shared_ptr(rSharedPointer).swap(*this);
  169. return *this;
  170. }
  171. template <class T>
  172. shared_ptr<T>& shared_ptr<T>::operator = (const shared_ptr &rSharedPointer)
  173. {
  174. shared_ptr(rSharedPointer).swap(*this);
  175. return *this;
  176. }
  177. template <class T>
  178. void shared_ptr<T>::reset()
  179. {
  180. shared_ptr().swap(*this);
  181. }
  182. template <class T>
  183. template <class T2>
  184. void shared_ptr<T>::reset(T2 *pObject)
  185. {
  186. shared_ptr(pObject).swap(*this);
  187. }
  188. template <class T>
  189. T* shared_ptr<T>::get() const
  190. {
  191. return m_pObject;
  192. }
  193. template <class T>
  194. T& shared_ptr<T>::operator * () const
  195. {
  196. return *m_pObject;
  197. }
  198. template <class T>
  199. T* shared_ptr<T>::operator -> () const
  200. {
  201. return m_pObject;
  202. }
  203. template <class T>
  204. long shared_ptr<T>::use_count() const
  205. {
  206. if(NULL == m_pRefCount)
  207. {
  208. return 0;
  209. }
  210. return m_pRefCount->use_count();
  211. }
  212. template <class T>
  213. bool shared_ptr<T>::unique() const
  214. {
  215. return (use_count() == 1);
  216. }
  217. template <class T>
  218. void shared_ptr<T>::swap(shared_ptr &rSharedPointer)
  219. {
  220. swap(m_pObject, rSharedPointer.m_pObject);
  221. swap(m_pRefCount, rSharedPointer.m_pRefCount);
  222. }
  223. template<class T, class T2>
  224. shared_ptr<T> dynamic_pointer_cast(const shared_ptr<T2> &rSharedPointer)
  225. {
  226. return shared_ptr<T>(rSharedPointer, dynamic_cast_tag());
  227. }
  228. template <class T1, class T2>
  229. bool operator==(const shared_ptr<T1>& sp1, const shared_ptr<T2>& sp2)
  230. {
  231. return sp1.get() == sp2.get();
  232. }
  233. template <class T1, class T2>
  234. bool operator!=(const shared_ptr<T1>& sp1, const shared_ptr<T2>& sp2)
  235. {
  236. return sp1.get() != sp2.get();
  237. }
  238. }} //namespace AVT::VmbAPI
  239. #endif //AVT_VMBAPI_SHAREDPOINTER_IMPL_H