• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
42 #ifndef __OPENCV_CORE_PTR_INL_HPP__
43 #define __OPENCV_CORE_PTR_INL_HPP__
44 
45 #include <algorithm>
46 
47 //! @cond IGNORED
48 
49 namespace cv {
50 
51 template<typename Y>
operator ()(Y * p) const52 void DefaultDeleter<Y>::operator () (Y* p) const
53 {
54     delete p;
55 }
56 
57 namespace detail
58 {
59 
60 struct PtrOwner
61 {
PtrOwnercv::detail::PtrOwner62     PtrOwner() : refCount(1)
63     {}
64 
incRefcv::detail::PtrOwner65     void incRef()
66     {
67         CV_XADD(&refCount, 1);
68     }
69 
decRefcv::detail::PtrOwner70     void decRef()
71     {
72         if (CV_XADD(&refCount, -1) == 1) deleteSelf();
73     }
74 
75 protected:
76     /* This doesn't really need to be virtual, since PtrOwner is never deleted
77        directly, but it doesn't hurt and it helps avoid warnings. */
~PtrOwnercv::detail::PtrOwner78     virtual ~PtrOwner()
79     {}
80 
81     virtual void deleteSelf() = 0;
82 
83 private:
84     unsigned int refCount;
85 
86     // noncopyable
87     PtrOwner(const PtrOwner&);
88     PtrOwner& operator = (const PtrOwner&);
89 };
90 
91 template<typename Y, typename D>
92 struct PtrOwnerImpl : PtrOwner
93 {
PtrOwnerImplcv::detail::PtrOwnerImpl94     PtrOwnerImpl(Y* p, D d) : owned(p), deleter(d)
95     {}
96 
deleteSelfcv::detail::PtrOwnerImpl97     void deleteSelf()
98     {
99         deleter(owned);
100         delete this;
101     }
102 
103 private:
104     Y* owned;
105     D deleter;
106 };
107 
108 
109 }
110 
111 template<typename T>
Ptr()112 Ptr<T>::Ptr() : owner(NULL), stored(NULL)
113 {}
114 
115 template<typename T>
116 template<typename Y>
Ptr(Y * p)117 Ptr<T>::Ptr(Y* p)
118   : owner(p
119       ? new detail::PtrOwnerImpl<Y, DefaultDeleter<Y> >(p, DefaultDeleter<Y>())
120       : NULL),
121     stored(p)
122 {}
123 
124 template<typename T>
125 template<typename Y, typename D>
Ptr(Y * p,D d)126 Ptr<T>::Ptr(Y* p, D d)
127   : owner(p
128       ? new detail::PtrOwnerImpl<Y, D>(p, d)
129       : NULL),
130     stored(p)
131 {}
132 
133 template<typename T>
Ptr(const Ptr & o)134 Ptr<T>::Ptr(const Ptr& o) : owner(o.owner), stored(o.stored)
135 {
136     if (owner) owner->incRef();
137 }
138 
139 template<typename T>
140 template<typename Y>
Ptr(const Ptr<Y> & o)141 Ptr<T>::Ptr(const Ptr<Y>& o) : owner(o.owner), stored(o.stored)
142 {
143     if (owner) owner->incRef();
144 }
145 
146 template<typename T>
147 template<typename Y>
Ptr(const Ptr<Y> & o,T * p)148 Ptr<T>::Ptr(const Ptr<Y>& o, T* p) : owner(o.owner), stored(p)
149 {
150     if (owner) owner->incRef();
151 }
152 
153 template<typename T>
~Ptr()154 Ptr<T>::~Ptr()
155 {
156     release();
157 }
158 
159 template<typename T>
operator =(const Ptr<T> & o)160 Ptr<T>& Ptr<T>::operator = (const Ptr<T>& o)
161 {
162     Ptr(o).swap(*this);
163     return *this;
164 }
165 
166 template<typename T>
167 template<typename Y>
operator =(const Ptr<Y> & o)168 Ptr<T>& Ptr<T>::operator = (const Ptr<Y>& o)
169 {
170     Ptr(o).swap(*this);
171     return *this;
172 }
173 
174 template<typename T>
release()175 void Ptr<T>::release()
176 {
177     if (owner) owner->decRef();
178     owner = NULL;
179     stored = NULL;
180 }
181 
182 template<typename T>
183 template<typename Y>
reset(Y * p)184 void Ptr<T>::reset(Y* p)
185 {
186     Ptr(p).swap(*this);
187 }
188 
189 template<typename T>
190 template<typename Y, typename D>
reset(Y * p,D d)191 void Ptr<T>::reset(Y* p, D d)
192 {
193     Ptr(p, d).swap(*this);
194 }
195 
196 template<typename T>
swap(Ptr<T> & o)197 void Ptr<T>::swap(Ptr<T>& o)
198 {
199     std::swap(owner, o.owner);
200     std::swap(stored, o.stored);
201 }
202 
203 template<typename T>
get() const204 T* Ptr<T>::get() const
205 {
206     return stored;
207 }
208 
209 template<typename T>
operator *() const210 typename detail::RefOrVoid<T>::type Ptr<T>::operator * () const
211 {
212     return *stored;
213 }
214 
215 template<typename T>
operator ->() const216 T* Ptr<T>::operator -> () const
217 {
218     return stored;
219 }
220 
221 template<typename T>
operator T*() const222 Ptr<T>::operator T* () const
223 {
224     return stored;
225 }
226 
227 
228 template<typename T>
empty() const229 bool Ptr<T>::empty() const
230 {
231     return !stored;
232 }
233 
234 template<typename T>
235 template<typename Y>
staticCast() const236 Ptr<Y> Ptr<T>::staticCast() const
237 {
238     return Ptr<Y>(*this, static_cast<Y*>(stored));
239 }
240 
241 template<typename T>
242 template<typename Y>
constCast() const243 Ptr<Y> Ptr<T>::constCast() const
244 {
245     return Ptr<Y>(*this, const_cast<Y*>(stored));
246 }
247 
248 template<typename T>
249 template<typename Y>
dynamicCast() const250 Ptr<Y> Ptr<T>::dynamicCast() const
251 {
252     return Ptr<Y>(*this, dynamic_cast<Y*>(stored));
253 }
254 
255 template<typename T>
swap(Ptr<T> & ptr1,Ptr<T> & ptr2)256 void swap(Ptr<T>& ptr1, Ptr<T>& ptr2){
257     ptr1.swap(ptr2);
258 }
259 
260 template<typename T>
operator ==(const Ptr<T> & ptr1,const Ptr<T> & ptr2)261 bool operator == (const Ptr<T>& ptr1, const Ptr<T>& ptr2)
262 {
263     return ptr1.get() == ptr2.get();
264 }
265 
266 template<typename T>
operator !=(const Ptr<T> & ptr1,const Ptr<T> & ptr2)267 bool operator != (const Ptr<T>& ptr1, const Ptr<T>& ptr2)
268 {
269     return ptr1.get() != ptr2.get();
270 }
271 
272 template<typename T>
makePtr()273 Ptr<T> makePtr()
274 {
275     return Ptr<T>(new T());
276 }
277 
278 template<typename T, typename A1>
makePtr(const A1 & a1)279 Ptr<T> makePtr(const A1& a1)
280 {
281     return Ptr<T>(new T(a1));
282 }
283 
284 template<typename T, typename A1, typename A2>
makePtr(const A1 & a1,const A2 & a2)285 Ptr<T> makePtr(const A1& a1, const A2& a2)
286 {
287     return Ptr<T>(new T(a1, a2));
288 }
289 
290 template<typename T, typename A1, typename A2, typename A3>
makePtr(const A1 & a1,const A2 & a2,const A3 & a3)291 Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3)
292 {
293     return Ptr<T>(new T(a1, a2, a3));
294 }
295 
296 template<typename T, typename A1, typename A2, typename A3, typename A4>
makePtr(const A1 & a1,const A2 & a2,const A3 & a3,const A4 & a4)297 Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3, const A4& a4)
298 {
299     return Ptr<T>(new T(a1, a2, a3, a4));
300 }
301 
302 template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5>
makePtr(const A1 & a1,const A2 & a2,const A3 & a3,const A4 & a4,const A5 & a5)303 Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5)
304 {
305     return Ptr<T>(new T(a1, a2, a3, a4, a5));
306 }
307 
308 template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
makePtr(const A1 & a1,const A2 & a2,const A3 & a3,const A4 & a4,const A5 & a5,const A6 & a6)309 Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6)
310 {
311     return Ptr<T>(new T(a1, a2, a3, a4, a5, a6));
312 }
313 
314 template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7>
makePtr(const A1 & a1,const A2 & a2,const A3 & a3,const A4 & a4,const A5 & a5,const A6 & a6,const A7 & a7)315 Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7)
316 {
317     return Ptr<T>(new T(a1, a2, a3, a4, a5, a6, a7));
318 }
319 
320 template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8>
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)321 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)
322 {
323     return Ptr<T>(new T(a1, a2, a3, a4, a5, a6, a7, a8));
324 }
325 
326 template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9>
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)327 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)
328 {
329     return Ptr<T>(new T(a1, a2, a3, a4, a5, a6, a7, a8, a9));
330 }
331 
332 template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9, typename A10>
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)333 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)
334 {
335     return Ptr<T>(new T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10));
336 }
337 
338 } // namespace cv
339 
340 //! @endcond
341 
342 #endif // __OPENCV_CORE_PTR_INL_HPP__
343