ptr.inl.hpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2013, NVIDIA Corporation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the copyright holders or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #ifndef OPENCV_CORE_PTR_INL_HPP
  42. #define OPENCV_CORE_PTR_INL_HPP
  43. #include <algorithm>
  44. //! @cond IGNORED
  45. namespace cv {
  46. template<typename Y>
  47. void DefaultDeleter<Y>::operator () (Y* p) const
  48. {
  49. delete p;
  50. }
  51. namespace detail
  52. {
  53. struct PtrOwner
  54. {
  55. PtrOwner() : refCount(1)
  56. {}
  57. void incRef()
  58. {
  59. CV_XADD(&refCount, 1);
  60. }
  61. void decRef()
  62. {
  63. if (CV_XADD(&refCount, -1) == 1) deleteSelf();
  64. }
  65. protected:
  66. /* This doesn't really need to be virtual, since PtrOwner is never deleted
  67. directly, but it doesn't hurt and it helps avoid warnings. */
  68. virtual ~PtrOwner()
  69. {}
  70. virtual void deleteSelf() = 0;
  71. private:
  72. unsigned int refCount;
  73. // noncopyable
  74. PtrOwner(const PtrOwner&);
  75. PtrOwner& operator = (const PtrOwner&);
  76. };
  77. template<typename Y, typename D>
  78. struct PtrOwnerImpl : PtrOwner
  79. {
  80. PtrOwnerImpl(Y* p, D d) : owned(p), deleter(d)
  81. {}
  82. void deleteSelf()
  83. {
  84. deleter(owned);
  85. delete this;
  86. }
  87. private:
  88. Y* owned;
  89. D deleter;
  90. };
  91. }
  92. template<typename T>
  93. Ptr<T>::Ptr() : owner(NULL), stored(NULL)
  94. {}
  95. template<typename T>
  96. template<typename Y>
  97. Ptr<T>::Ptr(Y* p)
  98. : owner(p
  99. ? new detail::PtrOwnerImpl<Y, DefaultDeleter<Y> >(p, DefaultDeleter<Y>())
  100. : NULL),
  101. stored(p)
  102. {}
  103. template<typename T>
  104. template<typename Y, typename D>
  105. Ptr<T>::Ptr(Y* p, D d)
  106. : owner(p
  107. ? new detail::PtrOwnerImpl<Y, D>(p, d)
  108. : NULL),
  109. stored(p)
  110. {}
  111. template<typename T>
  112. Ptr<T>::Ptr(const Ptr& o) : owner(o.owner), stored(o.stored)
  113. {
  114. if (owner) owner->incRef();
  115. }
  116. template<typename T>
  117. template<typename Y>
  118. Ptr<T>::Ptr(const Ptr<Y>& o) : owner(o.owner), stored(o.stored)
  119. {
  120. if (owner) owner->incRef();
  121. }
  122. template<typename T>
  123. template<typename Y>
  124. Ptr<T>::Ptr(const Ptr<Y>& o, T* p) : owner(o.owner), stored(p)
  125. {
  126. if (owner) owner->incRef();
  127. }
  128. template<typename T>
  129. Ptr<T>::~Ptr()
  130. {
  131. release();
  132. }
  133. template<typename T>
  134. Ptr<T>& Ptr<T>::operator = (const Ptr<T>& o)
  135. {
  136. Ptr(o).swap(*this);
  137. return *this;
  138. }
  139. template<typename T>
  140. template<typename Y>
  141. Ptr<T>& Ptr<T>::operator = (const Ptr<Y>& o)
  142. {
  143. Ptr(o).swap(*this);
  144. return *this;
  145. }
  146. template<typename T>
  147. void Ptr<T>::release()
  148. {
  149. if (owner) owner->decRef();
  150. owner = NULL;
  151. stored = NULL;
  152. }
  153. template<typename T>
  154. template<typename Y>
  155. void Ptr<T>::reset(Y* p)
  156. {
  157. Ptr(p).swap(*this);
  158. }
  159. template<typename T>
  160. template<typename Y, typename D>
  161. void Ptr<T>::reset(Y* p, D d)
  162. {
  163. Ptr(p, d).swap(*this);
  164. }
  165. template<typename T>
  166. void Ptr<T>::swap(Ptr<T>& o)
  167. {
  168. std::swap(owner, o.owner);
  169. std::swap(stored, o.stored);
  170. }
  171. template<typename T>
  172. T* Ptr<T>::get() const
  173. {
  174. return stored;
  175. }
  176. template<typename T>
  177. typename detail::RefOrVoid<T>::type Ptr<T>::operator * () const
  178. {
  179. return *stored;
  180. }
  181. template<typename T>
  182. T* Ptr<T>::operator -> () const
  183. {
  184. return stored;
  185. }
  186. template<typename T>
  187. Ptr<T>::operator T* () const
  188. {
  189. return stored;
  190. }
  191. template<typename T>
  192. bool Ptr<T>::empty() const
  193. {
  194. return !stored;
  195. }
  196. template<typename T>
  197. template<typename Y>
  198. Ptr<Y> Ptr<T>::staticCast() const
  199. {
  200. return Ptr<Y>(*this, static_cast<Y*>(stored));
  201. }
  202. template<typename T>
  203. template<typename Y>
  204. Ptr<Y> Ptr<T>::constCast() const
  205. {
  206. return Ptr<Y>(*this, const_cast<Y*>(stored));
  207. }
  208. template<typename T>
  209. template<typename Y>
  210. Ptr<Y> Ptr<T>::dynamicCast() const
  211. {
  212. return Ptr<Y>(*this, dynamic_cast<Y*>(stored));
  213. }
  214. #ifdef CV_CXX_MOVE_SEMANTICS
  215. template<typename T>
  216. Ptr<T>::Ptr(Ptr&& o) : owner(o.owner), stored(o.stored)
  217. {
  218. o.owner = NULL;
  219. o.stored = NULL;
  220. }
  221. template<typename T>
  222. Ptr<T>& Ptr<T>::operator = (Ptr<T>&& o)
  223. {
  224. if (this == &o)
  225. return *this;
  226. release();
  227. owner = o.owner;
  228. stored = o.stored;
  229. o.owner = NULL;
  230. o.stored = NULL;
  231. return *this;
  232. }
  233. #endif
  234. template<typename T>
  235. void swap(Ptr<T>& ptr1, Ptr<T>& ptr2){
  236. ptr1.swap(ptr2);
  237. }
  238. template<typename T>
  239. bool operator == (const Ptr<T>& ptr1, const Ptr<T>& ptr2)
  240. {
  241. return ptr1.get() == ptr2.get();
  242. }
  243. template<typename T>
  244. bool operator != (const Ptr<T>& ptr1, const Ptr<T>& ptr2)
  245. {
  246. return ptr1.get() != ptr2.get();
  247. }
  248. template<typename T>
  249. Ptr<T> makePtr()
  250. {
  251. return Ptr<T>(new T());
  252. }
  253. template<typename T, typename A1>
  254. Ptr<T> makePtr(const A1& a1)
  255. {
  256. return Ptr<T>(new T(a1));
  257. }
  258. template<typename T, typename A1, typename A2>
  259. Ptr<T> makePtr(const A1& a1, const A2& a2)
  260. {
  261. return Ptr<T>(new T(a1, a2));
  262. }
  263. template<typename T, typename A1, typename A2, typename A3>
  264. Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3)
  265. {
  266. return Ptr<T>(new T(a1, a2, a3));
  267. }
  268. template<typename T, typename A1, typename A2, typename A3, typename A4>
  269. Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3, const A4& a4)
  270. {
  271. return Ptr<T>(new T(a1, a2, a3, a4));
  272. }
  273. template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5>
  274. Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5)
  275. {
  276. return Ptr<T>(new T(a1, a2, a3, a4, a5));
  277. }
  278. template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
  279. Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6)
  280. {
  281. return Ptr<T>(new T(a1, a2, a3, a4, a5, a6));
  282. }
  283. template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7>
  284. Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7)
  285. {
  286. return Ptr<T>(new T(a1, a2, a3, a4, a5, a6, a7));
  287. }
  288. template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8>
  289. Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8)
  290. {
  291. return Ptr<T>(new T(a1, a2, a3, a4, a5, a6, a7, a8));
  292. }
  293. template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9>
  294. Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9)
  295. {
  296. return Ptr<T>(new T(a1, a2, a3, a4, a5, a6, a7, a8, a9));
  297. }
  298. template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9, typename A10>
  299. Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10)
  300. {
  301. return Ptr<T>(new T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10));
  302. }
  303. template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9, typename A10, typename A11>
  304. Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10, const A11& a11)
  305. {
  306. return Ptr<T>(new T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11));
  307. }
  308. template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9, typename A10, typename A11, typename A12>
  309. Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10, const A11& a11, const A12& a12)
  310. {
  311. return Ptr<T>(new T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12));
  312. }
  313. } // namespace cv
  314. //! @endcond
  315. #endif // OPENCV_CORE_PTR_INL_HPP