Renesas / opencv-lib

Dependents:   RZ_A2M_Mbed_samples

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ptr.inl.hpp Source File

ptr.inl.hpp

00001 /*M///////////////////////////////////////////////////////////////////////////////////////
00002 //
00003 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
00004 //
00005 //  By downloading, copying, installing or using the software you agree to this license.
00006 //  If you do not agree to this license, do not download, install,
00007 //  copy or use the software.
00008 //
00009 //
00010 //                           License Agreement
00011 //                For Open Source Computer Vision Library
00012 //
00013 // Copyright (C) 2013, NVIDIA Corporation, all rights reserved.
00014 // Third party copyrights are property of their respective owners.
00015 //
00016 // Redistribution and use in source and binary forms, with or without modification,
00017 // are permitted provided that the following conditions are met:
00018 //
00019 //   * Redistribution's of source code must retain the above copyright notice,
00020 //     this list of conditions and the following disclaimer.
00021 //
00022 //   * Redistribution's in binary form must reproduce the above copyright notice,
00023 //     this list of conditions and the following disclaimer in the documentation
00024 //     and/or other materials provided with the distribution.
00025 //
00026 //   * The name of the copyright holders may not be used to endorse or promote products
00027 //     derived from this software without specific prior written permission.
00028 //
00029 // This software is provided by the copyright holders and contributors "as is" and
00030 // any express or implied warranties, including, but not limited to, the implied
00031 // warranties of merchantability and fitness for a particular purpose are disclaimed.
00032 // In no event shall the copyright holders or contributors be liable for any direct,
00033 // indirect, incidental, special, exemplary, or consequential damages
00034 // (including, but not limited to, procurement of substitute goods or services;
00035 // loss of use, data, or profits; or business interruption) however caused
00036 // and on any theory of liability, whether in contract, strict liability,
00037 // or tort (including negligence or otherwise) arising in any way out of
00038 // the use of this software, even if advised of the possibility of such damage.
00039 //
00040 //M*/
00041 
00042 #ifndef OPENCV_CORE_PTR_INL_HPP
00043 #define OPENCV_CORE_PTR_INL_HPP
00044 
00045 #include <algorithm>
00046 
00047 //! @cond IGNORED
00048 
00049 namespace cv {
00050 
00051 template<typename Y>
00052 void DefaultDeleter<Y>::operator () (Y* p) const
00053 {
00054     delete p;
00055 }
00056 
00057 namespace detail
00058 {
00059 
00060 struct PtrOwner
00061 {
00062     PtrOwner() : refCount(1)
00063     {}
00064 
00065     void incRef()
00066     {
00067         CV_XADD(&refCount, 1);
00068     }
00069 
00070     void decRef()
00071     {
00072         if (CV_XADD(&refCount, -1) == 1) deleteSelf();
00073     }
00074 
00075 protected:
00076     /* This doesn't really need to be virtual, since PtrOwner is never deleted
00077        directly, but it doesn't hurt and it helps avoid warnings. */
00078     virtual ~PtrOwner()
00079     {}
00080 
00081     virtual void deleteSelf() = 0;
00082 
00083 private:
00084     unsigned int refCount;
00085 
00086     // noncopyable
00087     PtrOwner(const PtrOwner&);
00088     PtrOwner& operator = (const PtrOwner&);
00089 };
00090 
00091 template<typename Y, typename D>
00092 struct PtrOwnerImpl : PtrOwner
00093 {
00094     PtrOwnerImpl(Y* p, D d) : owned(p), deleter(d)
00095     {}
00096 
00097     void deleteSelf()
00098     {
00099         deleter(owned);
00100         delete this;
00101     }
00102 
00103 private:
00104     Y* owned;
00105     D deleter;
00106 };
00107 
00108 
00109 }
00110 
00111 template<typename T>
00112 Ptr<T>::Ptr() : owner(NULL), stored(NULL)
00113 {}
00114 
00115 template<typename T>
00116 template<typename Y>
00117 Ptr<T>::Ptr(Y* p)
00118   : owner(p
00119       ? new detail::PtrOwnerImpl<Y, DefaultDeleter<Y> >(p, DefaultDeleter<Y>())
00120       : NULL),
00121     stored(p)
00122 {}
00123 
00124 template<typename T>
00125 template<typename Y, typename D>
00126 Ptr<T>::Ptr(Y* p, D d)
00127   : owner(p
00128       ? new detail::PtrOwnerImpl<Y, D>(p, d)
00129       : NULL),
00130     stored(p)
00131 {}
00132 
00133 template<typename T>
00134 Ptr<T>::Ptr(const Ptr& o) : owner(o.owner), stored(o.stored)
00135 {
00136     if (owner) owner->incRef();
00137 }
00138 
00139 template<typename T>
00140 template<typename Y>
00141 Ptr<T>::Ptr(const Ptr<Y>& o) : owner(o.owner), stored(o.stored)
00142 {
00143     if (owner) owner->incRef();
00144 }
00145 
00146 template<typename T>
00147 template<typename Y>
00148 Ptr<T>::Ptr(const Ptr<Y>& o, T* p) : owner(o.owner), stored(p)
00149 {
00150     if (owner) owner->incRef();
00151 }
00152 
00153 template<typename T>
00154 Ptr<T>::~Ptr()
00155 {
00156     release();
00157 }
00158 
00159 template<typename T>
00160 Ptr<T>& Ptr<T>::operator = (const Ptr<T>& o)
00161 {
00162     Ptr(o).swap(*this);
00163     return *this;
00164 }
00165 
00166 template<typename T>
00167 template<typename Y>
00168 Ptr<T>& Ptr<T>::operator = (const Ptr<Y>& o)
00169 {
00170     Ptr(o).swap(*this);
00171     return *this;
00172 }
00173 
00174 template<typename T>
00175 void Ptr<T>::release()
00176 {
00177     if (owner) owner->decRef();
00178     owner = NULL;
00179     stored = NULL;
00180 }
00181 
00182 template<typename T>
00183 template<typename Y>
00184 void Ptr<T>::reset(Y* p)
00185 {
00186     Ptr(p).swap(*this);
00187 }
00188 
00189 template<typename T>
00190 template<typename Y, typename D>
00191 void Ptr<T>::reset(Y* p, D d)
00192 {
00193     Ptr(p, d).swap(*this);
00194 }
00195 
00196 template<typename T>
00197 void Ptr<T>::swap(Ptr<T>& o)
00198 {
00199     std::swap(owner, o.owner);
00200     std::swap(stored, o.stored);
00201 }
00202 
00203 template<typename T>
00204 T* Ptr<T>::get() const
00205 {
00206     return stored;
00207 }
00208 
00209 template<typename T>
00210 typename detail::RefOrVoid<T>::type Ptr<T>::operator * () const
00211 {
00212     return *stored;
00213 }
00214 
00215 template<typename T>
00216 T* Ptr<T>::operator -> () const
00217 {
00218     return stored;
00219 }
00220 
00221 template<typename T>
00222 Ptr<T>::operator T* () const
00223 {
00224     return stored;
00225 }
00226 
00227 
00228 template<typename T>
00229 bool Ptr<T>::empty() const
00230 {
00231     return !stored;
00232 }
00233 
00234 template<typename T>
00235 template<typename Y>
00236 Ptr<Y> Ptr<T>::staticCast() const
00237 {
00238     return Ptr<Y>(*this, static_cast<Y*>(stored));
00239 }
00240 
00241 template<typename T>
00242 template<typename Y>
00243 Ptr<Y> Ptr<T>::constCast() const
00244 {
00245     return Ptr<Y>(*this, const_cast<Y*>(stored));
00246 }
00247 
00248 template<typename T>
00249 template<typename Y>
00250 Ptr<Y> Ptr<T>::dynamicCast() const
00251 {
00252     return Ptr<Y>(*this, dynamic_cast<Y*>(stored));
00253 }
00254 
00255 #ifdef CV_CXX_MOVE_SEMANTICS
00256 
00257 template<typename T>
00258 Ptr<T>::Ptr(Ptr&& o) : owner(o.owner), stored(o.stored)
00259 {
00260     o.owner = NULL;
00261     o.stored = NULL;
00262 }
00263 
00264 template<typename T>
00265 Ptr<T>& Ptr<T>::operator = (Ptr<T>&& o)
00266 {
00267     if (this == &o)
00268         return *this;
00269 
00270     release();
00271     owner = o.owner;
00272     stored = o.stored;
00273     o.owner = NULL;
00274     o.stored = NULL;
00275     return *this;
00276 }
00277 
00278 #endif
00279 
00280 
00281 template<typename T>
00282 void swap(Ptr<T>& ptr1, Ptr<T>& ptr2){
00283     ptr1.swap(ptr2);
00284 }
00285 
00286 template<typename T>
00287 bool operator == (const Ptr<T>& ptr1, const Ptr<T>& ptr2)
00288 {
00289     return ptr1.get() == ptr2.get();
00290 }
00291 
00292 template<typename T>
00293 bool operator != (const Ptr<T>& ptr1, const Ptr<T>& ptr2)
00294 {
00295     return ptr1.get() != ptr2.get();
00296 }
00297 
00298 template<typename T>
00299 Ptr<T> makePtr()
00300 {
00301     return Ptr<T>(new T());
00302 }
00303 
00304 template<typename T, typename A1>
00305 Ptr<T> makePtr(const A1& a1)
00306 {
00307     return Ptr<T>(new T(a1));
00308 }
00309 
00310 template<typename T, typename A1, typename A2>
00311 Ptr<T> makePtr(const A1& a1, const A2& a2)
00312 {
00313     return Ptr<T>(new T(a1, a2));
00314 }
00315 
00316 template<typename T, typename A1, typename A2, typename A3>
00317 Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3)
00318 {
00319     return Ptr<T>(new T(a1, a2, a3));
00320 }
00321 
00322 template<typename T, typename A1, typename A2, typename A3, typename A4>
00323 Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3, const A4& a4)
00324 {
00325     return Ptr<T>(new T(a1, a2, a3, a4));
00326 }
00327 
00328 template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5>
00329 Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5)
00330 {
00331     return Ptr<T>(new T(a1, a2, a3, a4, a5));
00332 }
00333 
00334 template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
00335 Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6)
00336 {
00337     return Ptr<T>(new T(a1, a2, a3, a4, a5, a6));
00338 }
00339 
00340 template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7>
00341 Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7)
00342 {
00343     return Ptr<T>(new T(a1, a2, a3, a4, a5, a6, a7));
00344 }
00345 
00346 template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8>
00347 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)
00348 {
00349     return Ptr<T>(new T(a1, a2, a3, a4, a5, a6, a7, a8));
00350 }
00351 
00352 template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9>
00353 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)
00354 {
00355     return Ptr<T>(new T(a1, a2, a3, a4, a5, a6, a7, a8, a9));
00356 }
00357 
00358 template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9, typename A10>
00359 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)
00360 {
00361     return Ptr<T>(new T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10));
00362 }
00363 
00364 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>
00365 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)
00366 {
00367     return Ptr<T>(new T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11));
00368 }
00369 
00370 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>
00371 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)
00372 {
00373     return Ptr<T>(new T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12));
00374 }
00375 } // namespace cv
00376 
00377 //! @endcond
00378 
00379 #endif // OPENCV_CORE_PTR_INL_HPP