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.
5 // Copyright (C) 2020 Intel Corporation
7 #ifndef OPENCV_GAPI_S11N_HPP
8 #define OPENCV_GAPI_S11N_HPP
12 #include <unordered_map>
13 #include <opencv2/gapi/gcomputation.hpp>
19 GAPI_EXPORTS cv::GComputation getGraph(const std::vector<char> &p);
23 GAPI_EXPORTS cv::GMetaArgs getMetaArgs(const std::vector<char> &p);
27 GAPI_EXPORTS cv::GRunArgs getRunArgs(const std::vector<char> &p);
30 GAPI_EXPORTS std::vector<char> serialize(const cv::GComputation &c);
33 template<typename T> static inline
34 T deserialize(const std::vector<char> &p);
36 //} //ananymous namespace
38 GAPI_EXPORTS std::vector<char> serialize(const cv::GMetaArgs&);
39 GAPI_EXPORTS std::vector<char> serialize(const cv::GRunArgs&);
42 cv::GComputation deserialize(const std::vector<char> &p) {
43 return detail::getGraph(p);
47 cv::GMetaArgs deserialize(const std::vector<char> &p) {
48 return detail::getMetaArgs(p);
52 cv::GRunArgs deserialize(const std::vector<char> &p) {
53 return detail::getRunArgs(p);
62 struct GAPI_EXPORTS IOStream {
63 virtual ~IOStream() = default;
64 // Define the native support for basic C++ types at the API level:
65 virtual IOStream& operator<< (bool) = 0;
66 virtual IOStream& operator<< (char) = 0;
67 virtual IOStream& operator<< (unsigned char) = 0;
68 virtual IOStream& operator<< (short) = 0;
69 virtual IOStream& operator<< (unsigned short) = 0;
70 virtual IOStream& operator<< (int) = 0;
71 virtual IOStream& operator<< (uint32_t) = 0;
72 virtual IOStream& operator<< (uint64_t) = 0;
73 virtual IOStream& operator<< (float) = 0;
74 virtual IOStream& operator<< (double) = 0;
75 virtual IOStream& operator<< (const std::string&) = 0;
78 struct GAPI_EXPORTS IIStream {
79 virtual ~IIStream() = default;
80 virtual IIStream& operator>> (bool &) = 0;
81 virtual IIStream& operator>> (std::vector<bool>::reference) = 0;
82 virtual IIStream& operator>> (char &) = 0;
83 virtual IIStream& operator>> (unsigned char &) = 0;
84 virtual IIStream& operator>> (short &) = 0;
85 virtual IIStream& operator>> (unsigned short &) = 0;
86 virtual IIStream& operator>> (int &) = 0;
87 virtual IIStream& operator>> (float &) = 0;
88 virtual IIStream& operator>> (double &) = 0;
89 virtual IIStream& operator >> (uint32_t &) = 0;
90 virtual IIStream& operator >> (uint64_t &) = 0;
91 virtual IIStream& operator>> (std::string &) = 0;
94 ////////////////////////////////////////////////////////////////////////////////
95 ////////////////////////////////////////////////////////////////////////////////
97 // Note: operators for basic types are defined in IIStream/IOStream
99 // OpenCV types ////////////////////////////////////////////////////////////////
101 GAPI_EXPORTS IOStream& operator<< (IOStream& os, const cv::Point &pt);
102 GAPI_EXPORTS IIStream& operator>> (IIStream& is, cv::Point &pt);
104 GAPI_EXPORTS IOStream& operator<< (IOStream& os, const cv::Size &sz);
105 GAPI_EXPORTS IIStream& operator>> (IIStream& is, cv::Size &sz);
107 GAPI_EXPORTS IOStream& operator<< (IOStream& os, const cv::Rect &rc);
108 GAPI_EXPORTS IIStream& operator>> (IIStream& is, cv::Rect &rc);
110 GAPI_EXPORTS IOStream& operator<< (IOStream& os, const cv::Scalar &s);
111 GAPI_EXPORTS IIStream& operator>> (IIStream& is, cv::Scalar &s);
113 GAPI_EXPORTS IOStream& operator<< (IOStream& os, const cv::Mat &m);
114 GAPI_EXPORTS IIStream& operator>> (IIStream& is, cv::Mat &m);
116 // Generic STL types ////////////////////////////////////////////////////////////////
117 template<typename K, typename V>
118 IOStream& operator<< (IOStream& os, const std::map<K, V> &m) {
119 const uint32_t sz = static_cast<uint32_t>(m.size());
121 for (const auto& it : m) os << it.first << it.second;
124 template<typename K, typename V>
125 IIStream& operator>> (IIStream& is, std::map<K, V> &m) {
129 for (std::size_t i = 0; i < sz; ++i) {
137 template<typename K, typename V>
138 IOStream& operator<< (IOStream& os, const std::unordered_map<K, V> &m) {
139 const uint32_t sz = static_cast<uint32_t>(m.size());
141 for (auto &&it : m) os << it.first << it.second;
144 template<typename K, typename V>
145 IIStream& operator>> (IIStream& is, std::unordered_map<K, V> &m) {
149 for (std::size_t i = 0; i < sz; ++i) {
158 IOStream& operator<< (IOStream& os, const std::vector<T> &ts) {
159 const uint32_t sz = static_cast<uint32_t>(ts.size());
161 for (auto &&v : ts) os << v;
165 IIStream& operator>> (IIStream& is, std::vector<T> &ts) {
173 for (std::size_t i = 0; i < sz; ++i) is >> ts[i];
179 // Will be used along with default types if possible in specific cases (compile args, etc)
180 // Note: actual implementation is defined by user
182 struct GAPI_EXPORTS S11N {
183 static void serialize(IOStream &, const T &) {}
184 static T deserialize(IIStream &) { T t; return t; }
186 } // namespace detail
191 #endif // OPENCV_GAPI_S11N_HPP