Merge pull request #18452 from smirnov-alexey:as/export_serialization_api

[G-API] Export a part of serialization interface

* Initial stub

* Add test on serialization of a custom type

* Namespaces rework

* Fix isSupported in test struct

* Fix clang build and rework namespaces

* Remove redundant header
This commit is contained in:
Alexey Smirnov
2020-10-01 21:11:23 +03:00
committed by GitHub
parent 40b8b58bc6
commit a3e7c2d8e3
9 changed files with 442 additions and 401 deletions
@@ -37,14 +37,12 @@ namespace detail
}
// Forward-declare the serialization objects
namespace gimpl {
namespace gapi {
namespace s11n {
namespace I {
struct IStream;
struct OStream;
} // namespace I
struct IIStream;
struct IOStream;
} // namespace s11n
} // namespace gimpl
} // namespace gapi
/**
* \addtogroup gapi_main_classes
@@ -509,9 +507,9 @@ public:
/// @private
const Priv& priv() const;
/// @private
explicit GComputation(cv::gimpl::s11n::I::IStream &);
explicit GComputation(cv::gapi::s11n::IIStream &);
/// @private
void serialize(cv::gimpl::s11n::I::OStream &) const;
void serialize(cv::gapi::s11n::IOStream &) const;
protected:
+133
View File
@@ -8,6 +8,8 @@
#define OPENCV_GAPI_S11N_HPP
#include <vector>
#include <map>
#include <unordered_map>
#include <opencv2/gapi/gcomputation.hpp>
namespace cv {
@@ -51,7 +53,138 @@ cv::GRunArgs deserialize(const std::vector<char> &p) {
return detail::getRunArgs(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<bool>::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;
};
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// 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<typename K, typename V>
IOStream& operator<< (IOStream& os, const std::map<K, V> &m) {
const uint32_t sz = static_cast<uint32_t>(m.size());
os << sz;
for (const auto& it : m) os << it.first << it.second;
return os;
}
template<typename K, typename V>
IIStream& operator>> (IIStream& is, std::map<K, V> &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<typename K, typename V>
IOStream& operator<< (IOStream& os, const std::unordered_map<K, V> &m) {
const uint32_t sz = static_cast<uint32_t>(m.size());
os << sz;
for (auto &&it : m) os << it.first << it.second;
return os;
}
template<typename K, typename V>
IIStream& operator>> (IIStream& is, std::unordered_map<K, V> &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<typename T>
IOStream& operator<< (IOStream& os, const std::vector<T> &ts) {
const uint32_t sz = static_cast<uint32_t>(ts.size());
os << sz;
for (auto &&v : ts) os << v;
return os;
}
template<typename T>
IIStream& operator>> (IIStream& is, std::vector<T> &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 detail {
// Will be used along with default types if possible in specific cases (compile args, etc)
// Note: actual implementation is defined by user
template<typename T>
struct GAPI_EXPORTS S11N {
static void serialize(IOStream &, const T &) {}
static T deserialize(IIStream &) { T t; return t; }
};
} // namespace detail
} // namespace s11n
} // namespace gapi
} // namespace cv