1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-30 07:43:03 +04:00

Merge pull request #18401 from smirnov-alexey:as/serialization_more_types

[G-API] Add support for more types serialization

* Support more types

* Add std::string support

* Fix GOpaque and gin interaction

* Fix tests on kind

* Make map serialization support templates and add tests on kind
This commit is contained in:
Alexey Smirnov
2020-09-28 21:20:04 +03:00
committed by GitHub
parent 19f4cc57c1
commit 8da1b9aafa
8 changed files with 328 additions and 23 deletions
@@ -478,14 +478,17 @@ I::OStream& operator<< (I::OStream& os, const cv::GArg &arg) {
GAPI_Assert(arg.kind == cv::detail::ArgKind::OPAQUE_VAL);
GAPI_Assert(arg.opaque_kind != cv::detail::OpaqueKind::CV_UNKNOWN);
switch (arg.opaque_kind) {
case cv::detail::OpaqueKind::CV_BOOL: os << arg.get<bool>(); break;
case cv::detail::OpaqueKind::CV_INT: os << arg.get<int>(); break;
case cv::detail::OpaqueKind::CV_DOUBLE: os << arg.get<double>(); break;
case cv::detail::OpaqueKind::CV_POINT: os << arg.get<cv::Point>(); break;
case cv::detail::OpaqueKind::CV_SIZE: os << arg.get<cv::Size>(); break;
case cv::detail::OpaqueKind::CV_RECT: os << arg.get<cv::Rect>(); break;
case cv::detail::OpaqueKind::CV_SCALAR: os << arg.get<cv::Scalar>(); break;
case cv::detail::OpaqueKind::CV_MAT: os << arg.get<cv::Mat>(); break;
case cv::detail::OpaqueKind::CV_BOOL: os << arg.get<bool>(); break;
case cv::detail::OpaqueKind::CV_INT: os << arg.get<int>(); break;
case cv::detail::OpaqueKind::CV_UINT64: os << arg.get<uint64_t>(); break;
case cv::detail::OpaqueKind::CV_DOUBLE: os << arg.get<double>(); break;
case cv::detail::OpaqueKind::CV_FLOAT: os << arg.get<float>(); break;
case cv::detail::OpaqueKind::CV_STRING: os << arg.get<std::string>(); break;
case cv::detail::OpaqueKind::CV_POINT: os << arg.get<cv::Point>(); break;
case cv::detail::OpaqueKind::CV_SIZE: os << arg.get<cv::Size>(); break;
case cv::detail::OpaqueKind::CV_RECT: os << arg.get<cv::Rect>(); break;
case cv::detail::OpaqueKind::CV_SCALAR: os << arg.get<cv::Scalar>(); break;
case cv::detail::OpaqueKind::CV_MAT: os << arg.get<cv::Mat>(); break;
default: GAPI_Assert(false && "GArg: Unsupported (unknown?) opaque value type");
}
}
@@ -511,7 +514,10 @@ I::IStream& operator>> (I::IStream& is, cv::GArg &arg) {
{ T t{}; is >> t; arg = (cv::GArg(t)); } break
HANDLE_CASE(BOOL , bool);
HANDLE_CASE(INT , int);
HANDLE_CASE(UINT64 , uint64_t);
HANDLE_CASE(DOUBLE , double);
HANDLE_CASE(FLOAT , float);
HANDLE_CASE(STRING , std::string);
HANDLE_CASE(POINT , cv::Point);
HANDLE_CASE(SIZE , cv::Size);
HANDLE_CASE(RECT , cv::Rect);
@@ -686,6 +692,12 @@ I::OStream& ByteMemoryOutStream::operator<< (uint32_t atom) {
m_storage.push_back(0xFF & (atom >> 24));
return *this;
}
I::OStream& ByteMemoryOutStream::operator<< (uint64_t atom) {
for (int i = 0; i < 8; ++i) {
m_storage.push_back(0xFF & (atom >> (i * 8)));;
}
return *this;
}
I::OStream& ByteMemoryOutStream::operator<< (bool atom) {
m_storage.push_back(atom ? 1 : 0);
return *this;
@@ -734,7 +746,6 @@ I::OStream& ByteMemoryOutStream::operator<< (const std::string &str) {
for (auto c : str) *this << c;
return *this;
}
ByteMemoryInStream::ByteMemoryInStream(const std::vector<char> &data)
: m_storage(data) {
}
@@ -753,11 +764,26 @@ I::IStream& ByteMemoryInStream::operator>> (bool& atom) {
atom = (m_storage[m_idx++] == 0) ? false : true;
return *this;
}
I::IStream& ByteMemoryInStream::operator>> (std::vector<bool>::reference atom) {
check(sizeof(char));
atom = (m_storage[m_idx++] == 0) ? false : true;
return *this;
}
I::IStream& ByteMemoryInStream::operator>> (char &atom) {
check(sizeof(char));
atom = m_storage[m_idx++];
return *this;
}
I::IStream& ByteMemoryInStream::operator>> (uint64_t &atom) {
check(sizeof(uint64_t));
uint8_t x[8];
atom = 0;
for (int i = 0; i < 8; ++i) {
x[i] = static_cast<uint8_t>(m_storage[m_idx++]);
atom |= (static_cast<uint64_t>(x[i]) << (i * 8));
}
return *this;
}
I::IStream& ByteMemoryInStream::operator>> (unsigned char &atom) {
char c{};
*this >> c;
@@ -9,7 +9,8 @@
#include <iostream>
#include <fstream>
#include <string.h>
#include <string>
#include <map>
#include <ade/util/iota_range.hpp> // used in the vector<</>>
@@ -46,6 +47,7 @@ namespace I {
virtual OStream& operator<< (int) = 0;
//virtual OStream& operator<< (std::size_t) = 0;
virtual OStream& operator<< (uint32_t) = 0;
virtual OStream& operator<< (uint64_t) = 0;
virtual OStream& operator<< (float) = 0;
virtual OStream& operator<< (double) = 0;
virtual OStream& operator<< (const std::string&) = 0;
@@ -55,6 +57,7 @@ namespace I {
virtual ~IStream() = default;
virtual IStream& operator>> (bool &) = 0;
virtual IStream& operator>> (std::vector<bool>::reference) = 0;
virtual IStream& operator>> (char &) = 0;
virtual IStream& operator>> (unsigned char &) = 0;
virtual IStream& operator>> (short &) = 0;
@@ -64,6 +67,7 @@ namespace I {
virtual IStream& operator>> (double &) = 0;
//virtual IStream& operator>> (std::size_t &) = 0;
virtual IStream& operator >> (uint32_t &) = 0;
virtual IStream& operator >> (uint64_t &) = 0;
virtual IStream& operator>> (std::string &) = 0;
};
} // namespace I
@@ -217,6 +221,28 @@ GAPI_EXPORTS void serialize( I::OStream& os
GAPI_EXPORTS GSerialized deserialize(I::IStream& is);
GAPI_EXPORTS void reconstruct(const GSerialized &s, ade::Graph &g);
// Generic: map serialization ////////////////////////////////////////
template<typename K, typename V>
I::OStream& operator<< (I::OStream& os, const std::map<K, V> &m) {
const uint32_t sz = static_cast<uint32_t>(m.size()); // explicitly specify type
os << sz;
for (const auto& it : m) os << it.first << it.second;
return os;
}
template<typename K, typename V>
I::IStream& operator>> (I::IStream& 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;
}
// Legacy //////////////////////////////////////////////////////////////////////
// Generic: unordered_map serialization ////////////////////////////////////////
template<typename K, typename V>
@@ -334,6 +360,7 @@ public:
virtual I::OStream& operator<< (double) override;
virtual I::OStream& operator<< (const std::string&) override;
virtual I::OStream& operator<< (uint32_t) override;
virtual I::OStream& operator<< (uint64_t) override;
};
class GAPI_EXPORTS ByteMemoryInStream final: public I::IStream {
@@ -349,6 +376,7 @@ public:
explicit ByteMemoryInStream(const std::vector<char> &data);
virtual I::IStream& operator>> (bool &) override;
virtual I::IStream& operator>> (std::vector<bool>::reference) override;
virtual I::IStream& operator>> (char &) override;
virtual I::IStream& operator>> (unsigned char &) override;
virtual I::IStream& operator>> (short &) override;
@@ -358,6 +386,7 @@ public:
virtual I::IStream& operator>> (double &) override;
//virtual I::IStream& operator>> (std::size_t &) override;
virtual I::IStream& operator >> (uint32_t &) override;
virtual I::IStream& operator >> (uint64_t &) override;
virtual I::IStream& operator>> (std::string &) override;
};