garg.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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_GARG_HPP
  7. #define OPENCV_GAPI_GARG_HPP
  8. #include <vector>
  9. #include <type_traits>
  10. #include <opencv2/gapi/opencv_includes.hpp>
  11. #include "opencv2/gapi/own/mat.hpp"
  12. #include "opencv2/gapi/util/any.hpp"
  13. #include "opencv2/gapi/util/variant.hpp"
  14. #include "opencv2/gapi/gmat.hpp"
  15. #include "opencv2/gapi/gscalar.hpp"
  16. #include "opencv2/gapi/garray.hpp"
  17. #include "opencv2/gapi/gtype_traits.hpp"
  18. #include "opencv2/gapi/gmetaarg.hpp"
  19. #include "opencv2/gapi/own/scalar.hpp"
  20. namespace cv {
  21. class GArg;
  22. namespace detail {
  23. template<typename T>
  24. using is_garg = std::is_same<GArg, typename std::decay<T>::type>;
  25. }
  26. // Parameter holder class for a node
  27. // Depending on platform capabilities, can either support arbitrary types
  28. // (as `boost::any`) or a limited number of types (as `boot::variant`).
  29. // FIXME: put into "details" as a user shouldn't use it in his code
  30. class GAPI_EXPORTS GArg
  31. {
  32. public:
  33. GArg() {}
  34. template<typename T, typename std::enable_if<!detail::is_garg<T>::value, int>::type = 0>
  35. explicit GArg(const T &t)
  36. : kind(detail::GTypeTraits<T>::kind)
  37. , value(detail::wrap_gapi_helper<T>::wrap(t))
  38. {
  39. }
  40. template<typename T, typename std::enable_if<!detail::is_garg<T>::value, int>::type = 0>
  41. explicit GArg(T &&t)
  42. : kind(detail::GTypeTraits<typename std::decay<T>::type>::kind)
  43. , value(detail::wrap_gapi_helper<T>::wrap(t))
  44. {
  45. }
  46. template<typename T> inline T& get()
  47. {
  48. return util::any_cast<typename std::remove_reference<T>::type>(value);
  49. }
  50. template<typename T> inline const T& get() const
  51. {
  52. return util::any_cast<typename std::remove_reference<T>::type>(value);
  53. }
  54. template<typename T> inline T& unsafe_get()
  55. {
  56. return util::unsafe_any_cast<typename std::remove_reference<T>::type>(value);
  57. }
  58. template<typename T> inline const T& unsafe_get() const
  59. {
  60. return util::unsafe_any_cast<typename std::remove_reference<T>::type>(value);
  61. }
  62. detail::ArgKind kind = detail::ArgKind::OPAQUE;
  63. protected:
  64. util::any value;
  65. };
  66. using GArgs = std::vector<GArg>;
  67. // FIXME: Express as M<GProtoArg...>::type
  68. // FIXME: Move to a separate file!
  69. using GRunArg = util::variant<
  70. #if !defined(GAPI_STANDALONE)
  71. cv::Mat,
  72. cv::Scalar,
  73. cv::UMat,
  74. #endif // !defined(GAPI_STANDALONE)
  75. cv::gapi::own::Mat,
  76. cv::gapi::own::Scalar,
  77. cv::detail::VectorRef
  78. >;
  79. using GRunArgs = std::vector<GRunArg>;
  80. using GRunArgP = util::variant<
  81. #if !defined(GAPI_STANDALONE)
  82. cv::Mat*,
  83. cv::Scalar*,
  84. cv::UMat*,
  85. #endif // !defined(GAPI_STANDALONE)
  86. cv::gapi::own::Mat*,
  87. cv::gapi::own::Scalar*,
  88. cv::detail::VectorRef
  89. >;
  90. using GRunArgsP = std::vector<GRunArgP>;
  91. template<typename... Ts> inline GRunArgs gin(const Ts&... args)
  92. {
  93. return GRunArgs{ GRunArg(detail::wrap_host_helper<Ts>::wrap_in(args))... };
  94. }
  95. template<typename... Ts> inline GRunArgsP gout(Ts&... args)
  96. {
  97. return GRunArgsP{ GRunArgP(detail::wrap_host_helper<Ts>::wrap_out(args))... };
  98. }
  99. } // namespace cv
  100. #endif // OPENCV_GAPI_GARG_HPP