// This file is part of OpenCV project. // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. // // Copyright (C) 2020 Intel Corporation #ifndef OPENCV_GAPI_S11N_HPP #define OPENCV_GAPI_S11N_HPP #include #include #include #include #include namespace cv { namespace gapi { namespace detail { GAPI_EXPORTS cv::GComputation getGraph(const std::vector &p); GAPI_EXPORTS cv::GMetaArgs getMetaArgs(const std::vector &p); GAPI_EXPORTS cv::GRunArgs getRunArgs(const std::vector &p); template cv::GCompileArgs getCompileArgs(const std::vector &p); } // namespace detail GAPI_EXPORTS std::vector serialize(const cv::GComputation &c); //namespace{ template static inline T deserialize(const std::vector &p); //} //ananymous namespace GAPI_EXPORTS std::vector serialize(const cv::GCompileArgs&); GAPI_EXPORTS std::vector serialize(const cv::GMetaArgs&); GAPI_EXPORTS std::vector serialize(const cv::GRunArgs&); template<> inline cv::GComputation deserialize(const std::vector &p) { return detail::getGraph(p); } template<> inline cv::GMetaArgs deserialize(const std::vector &p) { return detail::getMetaArgs(p); } template<> inline cv::GRunArgs deserialize(const std::vector &p) { return detail::getRunArgs(p); } template inline typename std::enable_if::value, GCompileArgs>:: type deserialize(const std::vector &p) { return detail::getCompileArgs(p); } } // namespace gapi } // namespace cv namespace cv { namespace gapi { namespace s11n { struct GAPI_EXPORTS IOStream { virtual ~IOStream() = default; // Define the native support for basic C++ types at the API level: virtual IOStream& operator<< (bool) = 0; virtual IOStream& operator<< (char) = 0; virtual IOStream& operator<< (unsigned char) = 0; virtual IOStream& operator<< (short) = 0; virtual IOStream& operator<< (unsigned short) = 0; virtual IOStream& operator<< (int) = 0; virtual IOStream& operator<< (uint32_t) = 0; virtual IOStream& operator<< (uint64_t) = 0; virtual IOStream& operator<< (float) = 0; virtual IOStream& operator<< (double) = 0; virtual IOStream& operator<< (const std::string&) = 0; }; struct GAPI_EXPORTS IIStream { virtual ~IIStream() = default; virtual IIStream& operator>> (bool &) = 0; virtual IIStream& operator>> (std::vector::reference) = 0; virtual IIStream& operator>> (char &) = 0; virtual IIStream& operator>> (unsigned char &) = 0; virtual IIStream& operator>> (short &) = 0; virtual IIStream& operator>> (unsigned short &) = 0; virtual IIStream& operator>> (int &) = 0; virtual IIStream& operator>> (float &) = 0; virtual IIStream& operator>> (double &) = 0; virtual IIStream& operator >> (uint32_t &) = 0; virtual IIStream& operator >> (uint64_t &) = 0; virtual IIStream& operator>> (std::string &) = 0; }; namespace detail { GAPI_EXPORTS std::unique_ptr getInStream(const std::vector &p); } // namespace detail //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// // S11N operators // Note: operators for basic types are defined in IIStream/IOStream // OpenCV types //////////////////////////////////////////////////////////////// GAPI_EXPORTS IOStream& operator<< (IOStream& os, const cv::Point &pt); GAPI_EXPORTS IIStream& operator>> (IIStream& is, cv::Point &pt); GAPI_EXPORTS IOStream& operator<< (IOStream& os, const cv::Size &sz); GAPI_EXPORTS IIStream& operator>> (IIStream& is, cv::Size &sz); GAPI_EXPORTS IOStream& operator<< (IOStream& os, const cv::Rect &rc); GAPI_EXPORTS IIStream& operator>> (IIStream& is, cv::Rect &rc); GAPI_EXPORTS IOStream& operator<< (IOStream& os, const cv::Scalar &s); GAPI_EXPORTS IIStream& operator>> (IIStream& is, cv::Scalar &s); GAPI_EXPORTS IOStream& operator<< (IOStream& os, const cv::Mat &m); GAPI_EXPORTS IIStream& operator>> (IIStream& is, cv::Mat &m); // Generic STL types //////////////////////////////////////////////////////////////// template IOStream& operator<< (IOStream& os, const std::map &m) { const uint32_t sz = static_cast(m.size()); os << sz; for (const auto& it : m) os << it.first << it.second; return os; } template IIStream& operator>> (IIStream& is, std::map &m) { m.clear(); uint32_t sz = 0u; is >> sz; for (std::size_t i = 0; i < sz; ++i) { K k{}; V v{}; is >> k >> v; m[k] = v; } return is; } template IOStream& operator<< (IOStream& os, const std::unordered_map &m) { const uint32_t sz = static_cast(m.size()); os << sz; for (auto &&it : m) os << it.first << it.second; return os; } template IIStream& operator>> (IIStream& is, std::unordered_map &m) { m.clear(); uint32_t sz = 0u; is >> sz; for (std::size_t i = 0; i < sz; ++i) { K k{}; V v{}; is >> k >> v; m[k] = v; } return is; } template IOStream& operator<< (IOStream& os, const std::vector &ts) { const uint32_t sz = static_cast(ts.size()); os << sz; for (auto &&v : ts) os << v; return os; } template IIStream& operator>> (IIStream& is, std::vector &ts) { uint32_t sz = 0u; is >> sz; if (sz == 0u) { ts.clear(); } else { ts.resize(sz); for (std::size_t i = 0; i < sz; ++i) is >> ts[i]; } return is; } } // namespace s11n namespace detail { template struct deserialize_arg; template<> struct deserialize_arg> { static GCompileArg exec(cv::gapi::s11n::IIStream&, const std::string&) { throw std::logic_error("Passed arg can't be deserialized!"); } }; template struct deserialize_arg> { static GCompileArg exec(cv::gapi::s11n::IIStream& is, const std::string& tag) { if (tag == cv::detail::CompileArgTag::tag()) { return GCompileArg { cv::gapi::s11n::detail::S11N::deserialize(is) }; } return deserialize_arg>::exec(is, tag); } }; template cv::GCompileArgs getCompileArgs(const std::vector &p) { std::unique_ptr pIs = cv::gapi::s11n::detail::getInStream(p); cv::gapi::s11n::IIStream& is = *pIs; cv::GCompileArgs args; uint32_t sz = 0; is >> sz; for (uint32_t i = 0; i < sz; ++i) { std::string tag; is >> tag; args.push_back(cv::gapi::detail::deserialize_arg>::exec(is, tag)); } return args; } } // namespace detail } // namespace gapi } // namespace cv #endif // OPENCV_GAPI_S11N_HPP