any.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. //
  5. // Copyright (C) 2018 Intel Corporation
  6. #ifndef OPENCV_GAPI_UTIL_ANY_HPP
  7. #define OPENCV_GAPI_UTIL_ANY_HPP
  8. #include <memory>
  9. #include <type_traits>
  10. #include <typeinfo>
  11. #include <utility>
  12. #include <opencv2/gapi/util/throw.hpp>
  13. #if defined(_MSC_VER)
  14. // disable MSVC warning on "multiple copy constructors specified"
  15. # pragma warning(disable: 4521)
  16. #endif
  17. namespace cv
  18. {
  19. namespace internal
  20. {
  21. template <class T, class Source>
  22. T down_cast(Source operand)
  23. {
  24. #if defined(__GXX_RTTI) || defined(_CPPRTTI)
  25. return dynamic_cast<T>(operand);
  26. #else
  27. #ifdef __GNUC__
  28. #warning used static cast instead of dynamic because RTTI is disabled
  29. #else
  30. #pragma message("WARNING: used static cast instead of dynamic because RTTI is disabled")
  31. #endif
  32. return static_cast<T>(operand);
  33. #endif
  34. }
  35. }
  36. namespace util
  37. {
  38. class bad_any_cast : public std::bad_cast
  39. {
  40. public:
  41. virtual const char* what() const noexcept override
  42. {
  43. return "Bad any cast";
  44. }
  45. };
  46. //modeled against C++17 std::any
  47. class any
  48. {
  49. private:
  50. struct holder;
  51. using holder_ptr = std::unique_ptr<holder>;
  52. struct holder
  53. {
  54. virtual holder_ptr clone() = 0;
  55. virtual ~holder() = default;
  56. };
  57. template <typename value_t>
  58. struct holder_impl : holder
  59. {
  60. value_t v;
  61. template<typename arg_t>
  62. holder_impl(arg_t&& a) : v(std::forward<arg_t>(a)) {}
  63. holder_ptr clone() override { return holder_ptr(new holder_impl (v));}
  64. };
  65. holder_ptr hldr;
  66. public:
  67. template<class value_t>
  68. any(value_t&& arg) : hldr(new holder_impl<typename std::decay<value_t>::type>( std::forward<value_t>(arg))) {}
  69. any(any const& src) : hldr( src.hldr ? src.hldr->clone() : nullptr) {}
  70. //simple hack in order not to write enable_if<not any> for the template constructor
  71. any(any & src) : any (const_cast<any const&>(src)) {}
  72. any() = default;
  73. any(any&& ) = default;
  74. any& operator=(any&&) = default;
  75. any& operator=(any const& src)
  76. {
  77. any copy(src);
  78. swap(*this, copy);
  79. return *this;
  80. }
  81. template<class value_t>
  82. friend value_t* any_cast(any* operand);
  83. template<class value_t>
  84. friend const value_t* any_cast(const any* operand);
  85. template<class value_t>
  86. friend value_t& unsafe_any_cast(any& operand);
  87. template<class value_t>
  88. friend const value_t& unsafe_any_cast(const any& operand);
  89. friend void swap(any & lhs, any& rhs)
  90. {
  91. swap(lhs.hldr, rhs.hldr);
  92. }
  93. };
  94. template<class value_t>
  95. value_t* any_cast(any* operand)
  96. {
  97. auto casted = internal::down_cast<any::holder_impl<typename std::decay<value_t>::type> *>(operand->hldr.get());
  98. if (casted){
  99. return & (casted->v);
  100. }
  101. return nullptr;
  102. }
  103. template<class value_t>
  104. const value_t* any_cast(const any* operand)
  105. {
  106. auto casted = internal::down_cast<any::holder_impl<typename std::decay<value_t>::type> *>(operand->hldr.get());
  107. if (casted){
  108. return & (casted->v);
  109. }
  110. return nullptr;
  111. }
  112. template<class value_t>
  113. value_t& any_cast(any& operand)
  114. {
  115. auto ptr = any_cast<value_t>(&operand);
  116. if (ptr)
  117. {
  118. return *ptr;
  119. }
  120. throw_error(bad_any_cast());
  121. }
  122. template<class value_t>
  123. const value_t& any_cast(const any& operand)
  124. {
  125. auto ptr = any_cast<value_t>(&operand);
  126. if (ptr)
  127. {
  128. return *ptr;
  129. }
  130. throw_error(bad_any_cast());
  131. }
  132. template<class value_t>
  133. inline value_t& unsafe_any_cast(any& operand)
  134. {
  135. #ifdef DEBUG
  136. return any_cast<value_t>(operand);
  137. #else
  138. return static_cast<any::holder_impl<typename std::decay<value_t>::type> *>(operand.hldr.get())->v;
  139. #endif
  140. }
  141. template<class value_t>
  142. inline const value_t& unsafe_any_cast(const any& operand)
  143. {
  144. #ifdef DEBUG
  145. return any_cast<value_t>(operand);
  146. #else
  147. return static_cast<any::holder_impl<typename std::decay<value_t>::type> *>(operand.hldr.get())->v;
  148. #endif
  149. }
  150. } // namespace util
  151. } // namespace cv
  152. #if defined(_MSC_VER)
  153. // Enable "multiple copy constructors specified" back
  154. # pragma warning(default: 4521)
  155. #endif
  156. #endif // OPENCV_GAPI_UTIL_ANY_HPP