Merge pull request #18496 from AsyaPronina:comp_args_serialization

Serialization && deserialization for compile arguments

* Initial stub

* Add test on serialization of a custom type

* Namespaces rework

* Fix isSupported in test struct

* Fix clang lookup issue

* Initial implementation

* Drop the isSupported flag

* Initial implementation

* Removed internal header inclusion

* Switched to public API

* Implemented serialization

* Adding desirialize: WIP

* Fixed merge errors

* Implemented

* Final polishing

* Addressed review comments and added debug throw

* Added FluidROI test

* Polishing

* Polishing

* Polishing

* Polishing

* Polishing

* Updated CMakeLists.txt

* Fixed comments

* Addressed review comments

* Removed decay from deserialize_arg

* Addressed review comments

* Removed extra inclusion

* Fixed Win64 warning

* Update gcommon.hpp

* Update serialization.cpp

* Update gcommon.hpp

* gapi: drop GAPI_EXPORTS_W_SIMPLE from GCompileArg

Co-authored-by: Smirnov Alexey <alexey.smirnov@intel.com>
Co-authored-by: AsyaPronina <155jj@mail.ru>
This commit is contained in:
Anastasiya(Asya) Pronina
2020-10-08 00:48:49 +03:00
committed by GitHub
parent 46ccde82cf
commit af2f8c69f0
10 changed files with 180 additions and 20 deletions
+31 -1
View File
@@ -19,6 +19,7 @@
#include <opencv2/gapi/own/exports.hpp>
#include <opencv2/gapi/own/assert.hpp>
#include <opencv2/gapi/render/render_types.hpp>
#include <opencv2/gapi/s11n/base.hpp>
namespace cv {
@@ -94,6 +95,15 @@ enum class GShape: int
GFRAME,
};
namespace gapi {
namespace s11n {
namespace detail {
template<typename T> struct wrap_serialize;
} // namespace detail
} // namespace s11n
} // namespace gapi
struct GCompileArg;
namespace detail {
@@ -139,7 +149,7 @@ namespace detail {
* passed in (a variadic template parameter pack) into a vector of
* cv::GCompileArg objects.
*/
struct GAPI_EXPORTS_W_SIMPLE GCompileArg
struct GCompileArg
{
public:
// NB: Required for pythnon bindings
@@ -151,6 +161,7 @@ public:
template<typename T, typename std::enable_if<!detail::is_compile_arg<T>::value, int>::type = 0>
explicit GCompileArg(T &&t)
: tag(detail::CompileArgTag<typename std::decay<T>::type>::tag())
, serializeF(&cv::gapi::s11n::detail::wrap_serialize<T>::serialize)
, arg(t)
{
}
@@ -165,7 +176,13 @@ public:
return util::any_cast<T>(arg);
}
void serialize(cv::gapi::s11n::IOStream& os) const
{
serializeF(os, *this);
}
private:
std::function<void(cv::gapi::s11n::IOStream&, const GCompileArg&)> serializeF;
util::any arg;
};
@@ -198,6 +215,19 @@ inline cv::util::optional<T> getCompileArg(const cv::GCompileArgs &args)
}
return cv::util::optional<T>();
}
namespace s11n {
namespace detail {
template<typename T> struct wrap_serialize
{
static void serialize(IOStream& os, const GCompileArg& arg)
{
using decayed_type = typename std::decay<T>::type;
S11N<decayed_type>::serialize(os, arg.get<decayed_type>());
}
};
} // namespace detail
} // namespace s11n
} // namespace gapi
/**
+55 -14
View File
@@ -10,6 +10,7 @@
#include <vector>
#include <map>
#include <unordered_map>
#include <opencv2/gapi/s11n/base.hpp>
#include <opencv2/gapi/gcomputation.hpp>
namespace cv {
@@ -17,14 +18,13 @@ namespace gapi {
namespace detail {
GAPI_EXPORTS cv::GComputation getGraph(const std::vector<char> &p);
} // namespace detail
namespace detail {
GAPI_EXPORTS cv::GMetaArgs getMetaArgs(const std::vector<char> &p);
} // namespace detail
namespace detail {
GAPI_EXPORTS cv::GRunArgs getRunArgs(const std::vector<char> &p);
template<typename... Types>
cv::GCompileArgs getCompileArgs(const std::vector<char> &p);
} // namespace detail
GAPI_EXPORTS std::vector<char> serialize(const cv::GComputation &c);
@@ -35,6 +35,7 @@ T deserialize(const std::vector<char> &p);
//} //ananymous namespace
GAPI_EXPORTS std::vector<char> serialize(const cv::GCompileArgs&);
GAPI_EXPORTS std::vector<char> serialize(const cv::GMetaArgs&);
GAPI_EXPORTS std::vector<char> serialize(const cv::GRunArgs&);
@@ -53,6 +54,11 @@ cv::GRunArgs deserialize(const std::vector<char> &p) {
return detail::getRunArgs(p);
}
template<typename T, typename... Types> inline
typename std::enable_if<std::is_same<T, GCompileArgs>::value, GCompileArgs>::
type deserialize(const std::vector<char> &p) {
return detail::getCompileArgs<Types...>(p);
}
} // namespace gapi
} // namespace cv
@@ -91,6 +97,10 @@ struct GAPI_EXPORTS IIStream {
virtual IIStream& operator>> (std::string &) = 0;
};
namespace detail {
GAPI_EXPORTS std::unique_ptr<IIStream> getInStream(const std::vector<char> &p);
} // namespace detail
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// S11N operators
@@ -174,17 +184,48 @@ IIStream& operator>> (IIStream& is, std::vector<T> &ts) {
}
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 detail
{
template<typename T> struct deserialize_arg;
template<> struct deserialize_arg<std::tuple<>> {
static GCompileArg exec(cv::gapi::s11n::IIStream&, const std::string&) {
throw std::logic_error("Passed arg can't be deserialized!");
}
};
template<typename T, typename... Types>
struct deserialize_arg<std::tuple<T, Types...>> {
static GCompileArg exec(cv::gapi::s11n::IIStream& is, const std::string& tag) {
if (tag == cv::detail::CompileArgTag<T>::tag()) {
return GCompileArg {
cv::gapi::s11n::detail::S11N<T>::deserialize(is)
};
}
return deserialize_arg<std::tuple<Types...>>::exec(is, tag);
}
};
template<typename... Types>
cv::GCompileArgs getCompileArgs(const std::vector<char> &p) {
std::unique_ptr<cv::gapi::s11n::IIStream> 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<std::tuple<Types...>>::exec(is, tag));
}
return args;
}
} // namespace detail
} // namespace gapi
} // namespace cv
@@ -0,0 +1,36 @@
// 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_BASE_HPP
#define OPENCV_GAPI_S11N_BASE_HPP
#include <opencv2/gapi/own/assert.hpp>
namespace cv {
namespace gapi {
namespace s11n {
struct IOStream;
struct IIStream;
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 S11N {
static void serialize(IOStream &, const T &) {
GAPI_Assert(false && "No serialization routine is provided!");
}
static T deserialize(IIStream &) {
GAPI_Assert(false && "No deserialization routine is provided!");
}
};
} // namespace detail
} // namespace s11n
} // namespace gapi
} // namespace cv
#endif // OPENCV_GAPI_S11N_BASE_HPP