gproto.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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_GPROTO_HPP
  7. #define OPENCV_GAPI_GPROTO_HPP
  8. #include <type_traits>
  9. #include <vector>
  10. #include <ostream>
  11. #include <opencv2/gapi/util/variant.hpp>
  12. #include <opencv2/gapi/gmat.hpp>
  13. #include <opencv2/gapi/gscalar.hpp>
  14. #include <opencv2/gapi/garray.hpp>
  15. #include <opencv2/gapi/gopaque.hpp>
  16. #include <opencv2/gapi/garg.hpp>
  17. #include <opencv2/gapi/gmetaarg.hpp>
  18. namespace cv {
  19. // FIXME: user shouldn't deal with it - put to detail?
  20. // GProtoArg is an union type over G-types which can serve as
  21. // GComputation's in/output slots. In other words, GProtoArg
  22. // wraps any type which can serve as G-API exchange type.
  23. //
  24. // In Runtime, GProtoArgs are substituted with appropriate GRunArgs.
  25. //
  26. // GProtoArg objects are constructed in-place when user describes
  27. // (captures) computations, user doesn't interact with these types
  28. // directly.
  29. using GProtoArg = util::variant
  30. < GMat
  31. , GMatP
  32. , GFrame
  33. , GScalar
  34. , detail::GArrayU // instead of GArray<T>
  35. , detail::GOpaqueU // instead of GOpaque<T>
  36. >;
  37. using GProtoArgs = std::vector<GProtoArg>;
  38. namespace detail
  39. {
  40. template<typename... Ts> inline GProtoArgs packArgs(Ts... args)
  41. {
  42. return GProtoArgs{ GProtoArg(wrap_gapi_helper<Ts>::wrap(args))... };
  43. }
  44. }
  45. template<class Tag>
  46. struct GIOProtoArgs
  47. {
  48. public:
  49. // NB: Used by python wrapper
  50. GIOProtoArgs() = default;
  51. explicit GIOProtoArgs(const GProtoArgs& args) : m_args(args) {}
  52. explicit GIOProtoArgs(GProtoArgs &&args) : m_args(std::move(args)) {}
  53. GProtoArgs m_args;
  54. // TODO: Think about the addition operator
  55. /**
  56. * @brief This operator allows to complement the proto vectors at runtime.
  57. *
  58. * It's an ordinary overload of addition assignment operator.
  59. *
  60. * Example of usage:
  61. * @snippet samples/cpp/tutorial_code/gapi/doc_snippets/dynamic_graph_snippets.cpp GIOProtoArgs usage
  62. *
  63. */
  64. template<typename Tg>
  65. friend GIOProtoArgs<Tg>& operator += (GIOProtoArgs<Tg> &lhs, const GIOProtoArgs<Tg> &rhs);
  66. };
  67. template<typename Tg>
  68. cv::GIOProtoArgs<Tg>& operator += (cv::GIOProtoArgs<Tg> &lhs, const cv::GIOProtoArgs<Tg> &rhs)
  69. {
  70. lhs.m_args.reserve(lhs.m_args.size() + rhs.m_args.size());
  71. lhs.m_args.insert(lhs.m_args.end(), rhs.m_args.begin(), rhs.m_args.end());
  72. return lhs;
  73. }
  74. struct In_Tag{};
  75. struct Out_Tag{};
  76. using GProtoInputArgs = GIOProtoArgs<In_Tag>;
  77. using GProtoOutputArgs = GIOProtoArgs<Out_Tag>;
  78. // Perfect forwarding
  79. template<typename... Ts> inline GProtoInputArgs GIn(Ts&&... ts)
  80. {
  81. return GProtoInputArgs(detail::packArgs(std::forward<Ts>(ts)...));
  82. }
  83. template<typename... Ts> inline GProtoOutputArgs GOut(Ts&&... ts)
  84. {
  85. return GProtoOutputArgs(detail::packArgs(std::forward<Ts>(ts)...));
  86. }
  87. namespace detail
  88. {
  89. // Extract elements form tuple
  90. // FIXME: Someday utilize a generic tuple_to_vec<> routine
  91. template<typename... Ts, int... Indexes>
  92. static GProtoOutputArgs getGOut_impl(const std::tuple<Ts...>& ts, detail::Seq<Indexes...>)
  93. {
  94. return GProtoOutputArgs{ detail::packArgs(std::get<Indexes>(ts)...)};
  95. }
  96. }
  97. template<typename... Ts> inline GProtoOutputArgs GOut(const std::tuple<Ts...>& ts)
  98. {
  99. // TODO: think of std::forward(ts)
  100. return detail::getGOut_impl(ts, typename detail::MkSeq<sizeof...(Ts)>::type());
  101. }
  102. // Takes rvalue as input arg
  103. template<typename... Ts> inline GProtoOutputArgs GOut(std::tuple<Ts...>&& ts)
  104. {
  105. // TODO: think of std::forward(ts)
  106. return detail::getGOut_impl(ts, typename detail::MkSeq<sizeof...(Ts)>::type());
  107. }
  108. // Extract run-time arguments from node origin
  109. // Can be used to extract constant values associated with G-objects
  110. // (like GScalar) at graph construction time
  111. GRunArg value_of(const GOrigin &origin);
  112. // Transform run-time computation arguments into a collection of metadata
  113. // extracted from that arguments
  114. GMetaArg GAPI_EXPORTS descr_of(const GRunArg &arg );
  115. GMetaArgs GAPI_EXPORTS descr_of(const GRunArgs &args);
  116. // Transform run-time operation result argument into metadata extracted from that argument
  117. // Used to compare the metadata, which generated at compile time with the metadata result operation in run time
  118. GMetaArg GAPI_EXPORTS descr_of(const GRunArgP& argp);
  119. // Checks if run-time computation argument can be described by metadata
  120. bool GAPI_EXPORTS can_describe(const GMetaArg& meta, const GRunArg& arg);
  121. bool GAPI_EXPORTS can_describe(const GMetaArgs& metas, const GRunArgs& args);
  122. // Checks if run-time computation result argument can be described by metadata.
  123. // Used to check if the metadata generated at compile time
  124. // coincides with output arguments passed to computation in cpu and ocl backends
  125. bool GAPI_EXPORTS can_describe(const GMetaArg& meta, const GRunArgP& argp);
  126. // Validates input arguments
  127. void GAPI_EXPORTS validate_input_arg(const GRunArg& arg);
  128. void GAPI_EXPORTS validate_input_args(const GRunArgs& args);
  129. } // namespace cv
  130. #endif // OPENCV_GAPI_GPROTO_HPP