any.hpp 4.3 KB

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