cvstd_wrapper.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #ifndef OPENCV_CORE_CVSTD_WRAPPER_HPP
  5. #define OPENCV_CORE_CVSTD_WRAPPER_HPP
  6. #include "opencv2/core/cvdef.h"
  7. #include <string>
  8. #include <memory> // std::shared_ptr
  9. #include <type_traits> // std::enable_if
  10. namespace cv {
  11. using std::nullptr_t;
  12. //! @addtogroup core_basic
  13. //! @{
  14. #ifdef CV_DOXYGEN
  15. template <typename _Tp> using Ptr = std::shared_ptr<_Tp>; // In ideal world it should look like this, but we need some compatibility workarounds below
  16. template<typename _Tp, typename ... A1> static inline
  17. Ptr<_Tp> makePtr(const A1&... a1) { return std::make_shared<_Tp>(a1...); }
  18. #else // cv::Ptr with compatibility workarounds
  19. // It should be defined for C-API types only.
  20. // C++ types should use regular "delete" operator.
  21. template<typename Y> struct DefaultDeleter;
  22. #if 0
  23. {
  24. void operator()(Y* p) const;
  25. };
  26. #endif
  27. namespace sfinae {
  28. template<typename C, typename Ret, typename... Args>
  29. struct has_parenthesis_operator
  30. {
  31. private:
  32. template<typename T>
  33. static CV_CONSTEXPR std::true_type check(typename std::is_same<typename std::decay<decltype(std::declval<T>().operator()(std::declval<Args>()...))>::type, Ret>::type*);
  34. template<typename> static CV_CONSTEXPR std::false_type check(...);
  35. typedef decltype(check<C>(0)) type;
  36. public:
  37. #if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900/*MSVS 2015*/)
  38. static CV_CONSTEXPR bool value = type::value;
  39. #else
  40. // support MSVS 2013
  41. static const int value = type::value;
  42. #endif
  43. };
  44. } // namespace sfinae
  45. template <typename T, typename = void>
  46. struct has_custom_delete
  47. : public std::false_type {};
  48. // Force has_custom_delete to std::false_type when NVCC is compiling CUDA source files
  49. #ifndef __CUDACC__
  50. template <typename T>
  51. struct has_custom_delete<T, typename std::enable_if< sfinae::has_parenthesis_operator<DefaultDeleter<T>, void, T*>::value >::type >
  52. : public std::true_type {};
  53. #endif
  54. template<typename T>
  55. struct Ptr : public std::shared_ptr<T>
  56. {
  57. #if 0
  58. using std::shared_ptr<T>::shared_ptr; // GCC 5.x can't handle this
  59. #else
  60. inline Ptr() CV_NOEXCEPT : std::shared_ptr<T>() {}
  61. inline Ptr(nullptr_t) CV_NOEXCEPT : std::shared_ptr<T>(nullptr) {}
  62. template<typename Y, typename D> inline Ptr(Y* p, D d) : std::shared_ptr<T>(p, d) {}
  63. template<typename D> inline Ptr(nullptr_t, D d) : std::shared_ptr<T>(nullptr, d) {}
  64. template<typename Y> inline Ptr(const Ptr<Y>& r, T* ptr) CV_NOEXCEPT : std::shared_ptr<T>(r, ptr) {}
  65. inline Ptr(const Ptr<T>& o) CV_NOEXCEPT : std::shared_ptr<T>(o) {}
  66. inline Ptr(Ptr<T>&& o) CV_NOEXCEPT : std::shared_ptr<T>(std::move(o)) {}
  67. template<typename Y> inline Ptr(const Ptr<Y>& o) CV_NOEXCEPT : std::shared_ptr<T>(o) {}
  68. template<typename Y> inline Ptr(Ptr<Y>&& o) CV_NOEXCEPT : std::shared_ptr<T>(std::move(o)) {}
  69. #endif
  70. inline Ptr(const std::shared_ptr<T>& o) CV_NOEXCEPT : std::shared_ptr<T>(o) {}
  71. inline Ptr(std::shared_ptr<T>&& o) CV_NOEXCEPT : std::shared_ptr<T>(std::move(o)) {}
  72. // Overload with custom DefaultDeleter: Ptr<IplImage>(...)
  73. template<typename Y>
  74. inline Ptr(const std::true_type&, Y* ptr) : std::shared_ptr<T>(ptr, DefaultDeleter<Y>()) {}
  75. // Overload without custom deleter: Ptr<std::string>(...);
  76. template<typename Y>
  77. inline Ptr(const std::false_type&, Y* ptr) : std::shared_ptr<T>(ptr) {}
  78. template<typename Y = T>
  79. inline Ptr(Y* ptr) : Ptr(has_custom_delete<Y>(), ptr) {}
  80. // Overload with custom DefaultDeleter: Ptr<IplImage>(...)
  81. template<typename Y>
  82. inline void reset(const std::true_type&, Y* ptr) { std::shared_ptr<T>::reset(ptr, DefaultDeleter<Y>()); }
  83. // Overload without custom deleter: Ptr<std::string>(...);
  84. template<typename Y>
  85. inline void reset(const std::false_type&, Y* ptr) { std::shared_ptr<T>::reset(ptr); }
  86. template<typename Y>
  87. inline void reset(Y* ptr) { Ptr<T>::reset(has_custom_delete<Y>(), ptr); }
  88. template<class Y, class Deleter>
  89. void reset(Y* ptr, Deleter d) { std::shared_ptr<T>::reset(ptr, d); }
  90. void reset() CV_NOEXCEPT { std::shared_ptr<T>::reset(); }
  91. Ptr& operator=(const Ptr& o) { std::shared_ptr<T>::operator =(o); return *this; }
  92. template<typename Y> inline Ptr& operator=(const Ptr<Y>& o) { std::shared_ptr<T>::operator =(o); return *this; }
  93. T* operator->() const CV_NOEXCEPT { return std::shared_ptr<T>::get();}
  94. typename std::add_lvalue_reference<T>::type operator*() const CV_NOEXCEPT { return *std::shared_ptr<T>::get(); }
  95. // OpenCV 3.x methods (not a part of standard C++ library)
  96. inline void release() { std::shared_ptr<T>::reset(); }
  97. inline operator T* () const { return std::shared_ptr<T>::get(); }
  98. inline bool empty() const { return std::shared_ptr<T>::get() == nullptr; }
  99. template<typename Y> inline
  100. Ptr<Y> staticCast() const CV_NOEXCEPT { return std::static_pointer_cast<Y>(*this); }
  101. template<typename Y> inline
  102. Ptr<Y> constCast() const CV_NOEXCEPT { return std::const_pointer_cast<Y>(*this); }
  103. template<typename Y> inline
  104. Ptr<Y> dynamicCast() const CV_NOEXCEPT { return std::dynamic_pointer_cast<Y>(*this); }
  105. };
  106. template<typename _Tp, typename ... A1> static inline
  107. Ptr<_Tp> makePtr(const A1&... a1)
  108. {
  109. static_assert( !has_custom_delete<_Tp>::value, "Can't use this makePtr with custom DefaultDeleter");
  110. return (Ptr<_Tp>)std::make_shared<_Tp>(a1...);
  111. }
  112. #endif // CV_DOXYGEN
  113. //! @} core_basic
  114. } // cv
  115. #endif //OPENCV_CORE_CVSTD_WRAPPER_HPP