|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef OPENCV_GAPI_GOPAQUE_HPP |
|
|
#define OPENCV_GAPI_GOPAQUE_HPP |
|
|
|
|
|
#include <functional> |
|
|
#include <ostream> |
|
|
#include <memory> |
|
|
|
|
|
#include <opencv2/gapi/own/exports.hpp> |
|
|
#include <opencv2/gapi/opencv_includes.hpp> |
|
|
|
|
|
#include <opencv2/gapi/util/any.hpp> |
|
|
#include <opencv2/gapi/util/variant.hpp> |
|
|
#include <opencv2/gapi/util/throw.hpp> |
|
|
#include <opencv2/gapi/util/type_traits.hpp> |
|
|
#include <opencv2/gapi/own/assert.hpp> |
|
|
|
|
|
#include <opencv2/gapi/gcommon.hpp> |
|
|
#include <opencv2/gapi/garray.hpp> |
|
|
|
|
|
namespace cv |
|
|
{ |
|
|
|
|
|
|
|
|
class GNode; |
|
|
struct GOrigin; |
|
|
template<typename T> class GOpaque; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct GAPI_EXPORTS_W_SIMPLE GOpaqueDesc |
|
|
{ |
|
|
|
|
|
|
|
|
bool operator== (const GOpaqueDesc&) const { return true; } |
|
|
}; |
|
|
template<typename U> GOpaqueDesc descr_of(const U &) { return {};} |
|
|
GAPI_EXPORTS_W inline GOpaqueDesc empty_gopaque_desc() {return {}; } |
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& os, const cv::GOpaqueDesc &desc); |
|
|
|
|
|
namespace detail |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OpaqueRef; |
|
|
using ConstructOpaque = std::function<void(OpaqueRef&)>; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GAPI_EXPORTS GOpaqueU |
|
|
{ |
|
|
public: |
|
|
GOpaqueU(const GNode &n, std::size_t out); |
|
|
|
|
|
template <typename T> |
|
|
bool holds() const; |
|
|
|
|
|
GOrigin& priv(); |
|
|
const GOrigin& priv() const; |
|
|
|
|
|
protected: |
|
|
GOpaqueU(); |
|
|
template<class> friend class cv::GOpaque; |
|
|
|
|
|
void setConstructFcn(ConstructOpaque &&cv); |
|
|
|
|
|
template <typename T> |
|
|
void specifyType(); |
|
|
|
|
|
template <typename T> |
|
|
void storeKind(); |
|
|
|
|
|
void setKind(cv::detail::OpaqueKind); |
|
|
|
|
|
std::shared_ptr<GOrigin> m_priv; |
|
|
std::shared_ptr<TypeHintBase> m_hint; |
|
|
}; |
|
|
|
|
|
template <typename T> |
|
|
bool GOpaqueU::holds() const{ |
|
|
GAPI_Assert(m_hint != nullptr); |
|
|
using U = util::decay_t<T>; |
|
|
return dynamic_cast<TypeHint<U>*>(m_hint.get()) != nullptr; |
|
|
} |
|
|
|
|
|
template <typename T> |
|
|
void GOpaqueU::specifyType(){ |
|
|
m_hint.reset(new TypeHint<util::decay_t<T>>); |
|
|
} |
|
|
|
|
|
template <typename T> |
|
|
void GOpaqueU::storeKind(){ |
|
|
|
|
|
setKind(cv::detail::GOpaqueTraits<T>::kind); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BasicOpaqueRef |
|
|
{ |
|
|
public: |
|
|
cv::GOpaqueDesc m_desc; |
|
|
virtual ~BasicOpaqueRef() {} |
|
|
|
|
|
virtual void mov(BasicOpaqueRef &ref) = 0; |
|
|
virtual const void* ptr() const = 0; |
|
|
virtual void set(const cv::util::any &a) = 0; |
|
|
}; |
|
|
|
|
|
template<typename T> class OpaqueRefT final: public BasicOpaqueRef |
|
|
{ |
|
|
using empty_t = util::monostate; |
|
|
using ro_ext_t = const T *; |
|
|
using rw_ext_t = T *; |
|
|
using rw_own_t = T ; |
|
|
util::variant<empty_t, ro_ext_t, rw_ext_t, rw_own_t> m_ref; |
|
|
|
|
|
inline bool isEmpty() const { return util::holds_alternative<empty_t>(m_ref); } |
|
|
inline bool isROExt() const { return util::holds_alternative<ro_ext_t>(m_ref); } |
|
|
inline bool isRWExt() const { return util::holds_alternative<rw_ext_t>(m_ref); } |
|
|
inline bool isRWOwn() const { return util::holds_alternative<rw_own_t>(m_ref); } |
|
|
|
|
|
void init(const T* obj = nullptr) |
|
|
{ |
|
|
if (obj) m_desc = cv::descr_of(*obj); |
|
|
} |
|
|
|
|
|
public: |
|
|
OpaqueRefT() { init(); } |
|
|
virtual ~OpaqueRefT() {} |
|
|
|
|
|
explicit OpaqueRefT(const T& obj) : m_ref(&obj) { init(&obj); } |
|
|
explicit OpaqueRefT( T& obj) : m_ref(&obj) { init(&obj); } |
|
|
explicit OpaqueRefT( T&& obj) : m_ref(std::move(obj)) { init(&obj); } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void reset() |
|
|
{ |
|
|
if (isEmpty()) |
|
|
{ |
|
|
T empty_obj{}; |
|
|
m_desc = cv::descr_of(empty_obj); |
|
|
m_ref = std::move(empty_obj); |
|
|
GAPI_Assert(isRWOwn()); |
|
|
} |
|
|
else if (isRWOwn()) |
|
|
{ |
|
|
util::get<rw_own_t>(m_ref) = {}; |
|
|
} |
|
|
else GAPI_Error("InternalError"); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
T& wref() |
|
|
{ |
|
|
GAPI_Assert(isRWExt() || isRWOwn()); |
|
|
if (isRWExt()) return *util::get<rw_ext_t>(m_ref); |
|
|
if (isRWOwn()) return util::get<rw_own_t>(m_ref); |
|
|
util::throw_error(std::logic_error("Impossible happened")); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const T& rref() const |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isROExt()) return *util::get<ro_ext_t>(m_ref); |
|
|
if (isRWExt()) return *util::get<rw_ext_t>(m_ref); |
|
|
if (isRWOwn()) return util::get<rw_own_t>(m_ref); |
|
|
util::throw_error(std::logic_error("Impossible happened")); |
|
|
} |
|
|
|
|
|
virtual void mov(BasicOpaqueRef &v) override { |
|
|
OpaqueRefT<T> *tv = dynamic_cast<OpaqueRefT<T>*>(&v); |
|
|
GAPI_Assert(tv != nullptr); |
|
|
wref() = std::move(tv->wref()); |
|
|
} |
|
|
|
|
|
virtual const void* ptr() const override { return &rref(); } |
|
|
|
|
|
virtual void set(const cv::util::any &a) override { |
|
|
wref() = util::any_cast<T>(a); |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OpaqueRef |
|
|
{ |
|
|
std::shared_ptr<BasicOpaqueRef> m_ref; |
|
|
cv::detail::OpaqueKind m_kind = cv::detail::OpaqueKind::CV_UNKNOWN; |
|
|
|
|
|
template<typename T> inline void check() const |
|
|
{ |
|
|
GAPI_DbgAssert(dynamic_cast<OpaqueRefT<T>*>(m_ref.get()) != nullptr); |
|
|
} |
|
|
|
|
|
public: |
|
|
OpaqueRef() = default; |
|
|
|
|
|
template< |
|
|
typename T, |
|
|
typename = util::are_different_t<OpaqueRef, T> |
|
|
> |
|
|
|
|
|
explicit OpaqueRef(T&& obj) : |
|
|
m_ref(new OpaqueRefT<util::decay_t<T>>(std::forward<T>(obj))), |
|
|
m_kind(GOpaqueTraits<util::decay_t<T>>::kind) {} |
|
|
|
|
|
cv::detail::OpaqueKind getKind() const |
|
|
{ |
|
|
return m_kind; |
|
|
} |
|
|
|
|
|
template<typename T> void reset() |
|
|
{ |
|
|
if (!m_ref) m_ref.reset(new OpaqueRefT<T>()); |
|
|
check<T>(); |
|
|
storeKind<T>(); |
|
|
static_cast<OpaqueRefT<T>&>(*m_ref).reset(); |
|
|
} |
|
|
|
|
|
template <typename T> |
|
|
void storeKind() |
|
|
{ |
|
|
m_kind = cv::detail::GOpaqueTraits<T>::kind; |
|
|
} |
|
|
|
|
|
template<typename T> T& wref() |
|
|
{ |
|
|
check<T>(); |
|
|
return static_cast<OpaqueRefT<T>&>(*m_ref).wref(); |
|
|
} |
|
|
|
|
|
template<typename T> const T& rref() const |
|
|
{ |
|
|
check<T>(); |
|
|
return static_cast<OpaqueRefT<T>&>(*m_ref).rref(); |
|
|
} |
|
|
|
|
|
void mov(OpaqueRef &v) |
|
|
{ |
|
|
m_ref->mov(*v.m_ref); |
|
|
} |
|
|
|
|
|
cv::GOpaqueDesc descr_of() const |
|
|
{ |
|
|
return m_ref->m_desc; |
|
|
} |
|
|
|
|
|
|
|
|
const void *ptr() const { return m_ref->ptr(); } |
|
|
|
|
|
|
|
|
OpaqueRef& operator= (const cv::util::any &a) |
|
|
{ |
|
|
m_ref->set(a); |
|
|
return *this; |
|
|
} |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T> class GOpaque |
|
|
{ |
|
|
public: |
|
|
|
|
|
|
|
|
|
|
|
using HT = typename detail::flatten_g<util::decay_t<T>>::type; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GOpaque() { putDetails(); } |
|
|
|
|
|
|
|
|
explicit GOpaque(detail::GOpaqueU &&ref) |
|
|
: m_ref(ref) { putDetails(); } |
|
|
|
|
|
|
|
|
detail::GOpaqueU strip() const { |
|
|
return m_ref; |
|
|
} |
|
|
|
|
|
static void Ctor(detail::OpaqueRef& ref) { |
|
|
ref.reset<HT>(); |
|
|
} |
|
|
private: |
|
|
void putDetails() { |
|
|
m_ref.setConstructFcn(&Ctor); |
|
|
m_ref.specifyType<HT>(); |
|
|
m_ref.storeKind<HT>(); |
|
|
} |
|
|
|
|
|
detail::GOpaqueU m_ref; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
#endif |
|
|
|