// This file is part of OpenCV project. // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. #ifndef OPENCV_CORE_CVSTD_WRAPPER_HPP #define OPENCV_CORE_CVSTD_WRAPPER_HPP #include "opencv2/core/cvdef.h" #include #include // std::shared_ptr #include // std::enable_if namespace cv { using std::nullptr_t; //! @addtogroup core_basic //! @{ #ifdef CV_DOXYGEN template using Ptr = std::shared_ptr<_Tp>; // In ideal world it should look like this, but we need some compatibility workarounds below template static inline Ptr<_Tp> makePtr(const A1&... a1) { return std::make_shared<_Tp>(a1...); } #else // cv::Ptr with compatibility workarounds // It should be defined for C-API types only. // C++ types should use regular "delete" operator. template struct DefaultDeleter; #if 0 { void operator()(Y* p) const; }; #endif namespace sfinae { template struct has_parenthesis_operator { private: template static CV_CONSTEXPR std::true_type check(typename std::is_same().operator()(std::declval()...))>::type, Ret>::type*); template static CV_CONSTEXPR std::false_type check(...); typedef decltype(check(0)) type; public: #if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900/*MSVS 2015*/) static CV_CONSTEXPR bool value = type::value; #else // support MSVS 2013 static const int value = type::value; #endif }; } // namespace sfinae template struct has_custom_delete : public std::false_type {}; // Force has_custom_delete to std::false_type when NVCC is compiling CUDA source files #ifndef __CUDACC__ template struct has_custom_delete, void, T*>::value >::type > : public std::true_type {}; #endif template struct Ptr : public std::shared_ptr { #if 0 using std::shared_ptr::shared_ptr; // GCC 5.x can't handle this #else inline Ptr() CV_NOEXCEPT : std::shared_ptr() {} inline Ptr(nullptr_t) CV_NOEXCEPT : std::shared_ptr(nullptr) {} template inline Ptr(Y* p, D d) : std::shared_ptr(p, d) {} template inline Ptr(nullptr_t, D d) : std::shared_ptr(nullptr, d) {} template inline Ptr(const Ptr& r, T* ptr) CV_NOEXCEPT : std::shared_ptr(r, ptr) {} inline Ptr(const Ptr& o) CV_NOEXCEPT : std::shared_ptr(o) {} inline Ptr(Ptr&& o) CV_NOEXCEPT : std::shared_ptr(std::move(o)) {} template inline Ptr(const Ptr& o) CV_NOEXCEPT : std::shared_ptr(o) {} template inline Ptr(Ptr&& o) CV_NOEXCEPT : std::shared_ptr(std::move(o)) {} #endif inline Ptr(const std::shared_ptr& o) CV_NOEXCEPT : std::shared_ptr(o) {} inline Ptr(std::shared_ptr&& o) CV_NOEXCEPT : std::shared_ptr(std::move(o)) {} // Overload with custom DefaultDeleter: Ptr(...) template inline Ptr(const std::true_type&, Y* ptr) : std::shared_ptr(ptr, DefaultDeleter()) {} // Overload without custom deleter: Ptr(...); template inline Ptr(const std::false_type&, Y* ptr) : std::shared_ptr(ptr) {} template inline Ptr(Y* ptr) : Ptr(has_custom_delete(), ptr) {} // Overload with custom DefaultDeleter: Ptr(...) template inline void reset(const std::true_type&, Y* ptr) { std::shared_ptr::reset(ptr, DefaultDeleter()); } // Overload without custom deleter: Ptr(...); template inline void reset(const std::false_type&, Y* ptr) { std::shared_ptr::reset(ptr); } template inline void reset(Y* ptr) { Ptr::reset(has_custom_delete(), ptr); } template void reset(Y* ptr, Deleter d) { std::shared_ptr::reset(ptr, d); } void reset() CV_NOEXCEPT { std::shared_ptr::reset(); } Ptr& operator=(const Ptr& o) { std::shared_ptr::operator =(o); return *this; } template inline Ptr& operator=(const Ptr& o) { std::shared_ptr::operator =(o); return *this; } T* operator->() const CV_NOEXCEPT { return std::shared_ptr::get();} typename std::add_lvalue_reference::type operator*() const CV_NOEXCEPT { return *std::shared_ptr::get(); } // OpenCV 3.x methods (not a part of standard C++ library) inline void release() { std::shared_ptr::reset(); } inline operator T* () const { return std::shared_ptr::get(); } inline bool empty() const { return std::shared_ptr::get() == nullptr; } template inline Ptr staticCast() const CV_NOEXCEPT { return std::static_pointer_cast(*this); } template inline Ptr constCast() const CV_NOEXCEPT { return std::const_pointer_cast(*this); } template inline Ptr dynamicCast() const CV_NOEXCEPT { return std::dynamic_pointer_cast(*this); } }; template static inline Ptr<_Tp> makePtr(const A1&... a1) { static_assert( !has_custom_delete<_Tp>::value, "Can't use this makePtr with custom DefaultDeleter"); return (Ptr<_Tp>)std::make_shared<_Tp>(a1...); } #endif // CV_DOXYGEN //! @} core_basic } // cv #endif //OPENCV_CORE_CVSTD_WRAPPER_HPP