Skip to content

Commit 6df92b3

Browse files
committed
Merge pull request opencv#18651 from AsyaPronina:asyadev/add_possibility_to_check_that_gcompilearg_has_serialize
2 parents 691c3d1 + 48ccbe3 commit 6df92b3

File tree

5 files changed

+310
-45
lines changed

5 files changed

+310
-45
lines changed

modules/gapi/include/opencv2/gapi/gcommon.hpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ struct GCompileArg
161161
template<typename T, typename std::enable_if<!detail::is_compile_arg<T>::value, int>::type = 0>
162162
explicit GCompileArg(T &&t)
163163
: tag(detail::CompileArgTag<typename std::decay<T>::type>::tag())
164-
, serializeF(&cv::gapi::s11n::detail::wrap_serialize<T>::serialize)
164+
, serializeF(cv::gapi::s11n::detail::has_S11N_spec<T>::value ?
165+
&cv::gapi::s11n::detail::wrap_serialize<T>::serialize :
166+
nullptr)
165167
, arg(t)
166168
{
167169
}
@@ -178,7 +180,10 @@ struct GCompileArg
178180

179181
void serialize(cv::gapi::s11n::IOStream& os) const
180182
{
181-
serializeF(os, *this);
183+
if (serializeF)
184+
{
185+
serializeF(os, *this);
186+
}
182187
}
183188

184189
private:
@@ -222,8 +227,8 @@ template<typename T> struct wrap_serialize
222227
{
223228
static void serialize(IOStream& os, const GCompileArg& arg)
224229
{
225-
using decayed_type = typename std::decay<T>::type;
226-
S11N<decayed_type>::serialize(os, arg.get<decayed_type>());
230+
using DT = typename std::decay<T>::type;
231+
S11N<DT>::serialize(os, arg.get<DT>());
227232
}
228233
};
229234
} // namespace detail

modules/gapi/include/opencv2/gapi/s11n.hpp

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -265,23 +265,25 @@ void getRunArgByIdx (IIStream& is, cv::util::variant<Ts...> &v, uint32_t idx) {
265265

266266
namespace detail
267267
{
268-
template<typename T> struct deserialize_arg;
268+
template<typename T> struct try_deserialize_comparg;
269269

270-
template<> struct deserialize_arg<std::tuple<>> {
271-
static GCompileArg exec(cv::gapi::s11n::IIStream&, const std::string&) {
272-
throw std::logic_error("Passed arg can't be deserialized!");
270+
template<> struct try_deserialize_comparg<std::tuple<>> {
271+
static cv::util::optional<GCompileArg> exec(const std::string&, cv::gapi::s11n::IIStream&) {
272+
return { };
273273
}
274274
};
275275

276276
template<typename T, typename... Types>
277-
struct deserialize_arg<std::tuple<T, Types...>> {
278-
static GCompileArg exec(cv::gapi::s11n::IIStream& is, const std::string& tag) {
277+
struct try_deserialize_comparg<std::tuple<T, Types...>> {
278+
static cv::util::optional<GCompileArg> exec(const std::string& tag, cv::gapi::s11n::IIStream& is) {
279279
if (tag == cv::detail::CompileArgTag<T>::tag()) {
280-
return GCompileArg {
281-
cv::gapi::s11n::detail::S11N<T>::deserialize(is)
282-
};
280+
static_assert(cv::gapi::s11n::detail::has_S11N_spec<T>::value,
281+
"cv::gapi::deserialize<GCompileArgs, Types...> expects Types to have S11N "
282+
"specializations with deserialization callbacks!");
283+
return cv::util::optional<GCompileArg>(
284+
GCompileArg { cv::gapi::s11n::detail::S11N<T>::deserialize(is) });
283285
}
284-
return deserialize_arg<std::tuple<Types...>>::exec(is, tag);
286+
return try_deserialize_comparg<std::tuple<Types...>>::exec(tag, is);
285287
}
286288
};
287289

@@ -303,17 +305,35 @@ static GRunArg exec(cv::gapi::s11n::IIStream& is, uint32_t idx) {
303305
};
304306

305307
template<typename... Types>
306-
cv::GCompileArgs getCompileArgs(const std::vector<char> &p) {
307-
std::unique_ptr<cv::gapi::s11n::IIStream> pIs = cv::gapi::s11n::detail::getInStream(p);
308-
cv::gapi::s11n::IIStream& is = *pIs;
308+
inline cv::util::optional<GCompileArg> tryDeserializeCompArg(const std::string& tag,
309+
const std::vector<char>& sArg) {
310+
std::unique_ptr<cv::gapi::s11n::IIStream> pArgIs = cv::gapi::s11n::detail::getInStream(sArg);
311+
return try_deserialize_comparg<std::tuple<Types...>>::exec(tag, *pArgIs);
312+
}
313+
314+
template<typename... Types>
315+
cv::GCompileArgs getCompileArgs(const std::vector<char> &sArgs) {
309316
cv::GCompileArgs args;
310317

318+
std::unique_ptr<cv::gapi::s11n::IIStream> pIs = cv::gapi::s11n::detail::getInStream(sArgs);
319+
cv::gapi::s11n::IIStream& is = *pIs;
320+
311321
uint32_t sz = 0;
312322
is >> sz;
313323
for (uint32_t i = 0; i < sz; ++i) {
314324
std::string tag;
315325
is >> tag;
316-
args.push_back(cv::gapi::detail::deserialize_arg<std::tuple<Types...>>::exec(is, tag));
326+
327+
std::vector<char> sArg;
328+
is >> sArg;
329+
330+
cv::util::optional<GCompileArg> dArg =
331+
cv::gapi::detail::tryDeserializeCompArg<Types...>(tag, sArg);
332+
333+
if (dArg.has_value())
334+
{
335+
args.push_back(dArg.value());
336+
}
317337
}
318338

319339
return args;

modules/gapi/include/opencv2/gapi/s11n/base.hpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define OPENCV_GAPI_S11N_BASE_HPP
99

1010
#include <opencv2/gapi/own/assert.hpp>
11+
#include <opencv2/gapi/own/exports.hpp>
1112

1213
namespace cv {
1314
namespace gapi {
@@ -16,10 +17,14 @@ struct IOStream;
1617
struct IIStream;
1718

1819
namespace detail {
19-
// Will be used along with default types if possible in specific cases (compile args, etc)
20-
// Note: actual implementation is defined by user
20+
21+
struct NotImplemented {
22+
};
23+
24+
// The default S11N for custom types is NotImplemented
25+
// Don't! sublass from NotImplemented if you actually implement S11N.
2126
template<typename T>
22-
struct S11N {
27+
struct S11N: public NotImplemented {
2328
static void serialize(IOStream &, const T &) {
2429
GAPI_Assert(false && "No serialization routine is provided!");
2530
}
@@ -28,6 +33,11 @@ struct S11N {
2833
}
2934
};
3035

36+
template<typename T> struct has_S11N_spec {
37+
static constexpr bool value = !std::is_base_of<NotImplemented,
38+
S11N<typename std::decay<T>::type>>::value;
39+
};
40+
3141
} // namespace detail
3242
} // namespace s11n
3343
} // namespace gapi

modules/gapi/src/backends/common/serialization.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,13 @@ IIStream& operator>> (IIStream& is, cv::gapi::wip::draw::Line &l) {
338338

339339
IOStream& operator<< (IOStream& os, const cv::GCompileArg& arg)
340340
{
341+
ByteMemoryOutStream tmpS;
342+
arg.serialize(tmpS);
343+
std::vector<char> data = tmpS.data();
344+
341345
os << arg.tag;
342-
arg.serialize(os);
346+
os << data;
347+
343348
return os;
344349
}
345350

0 commit comments

Comments
 (0)