|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef OPENCV_GAPI_GARRAY_HPP |
|
|
#define OPENCV_GAPI_GARRAY_HPP |
|
|
|
|
|
#include <functional> |
|
|
#include <ostream> |
|
|
#include <vector> |
|
|
#include <memory> |
|
|
|
|
|
#include <opencv2/gapi/own/exports.hpp> |
|
|
#include <opencv2/gapi/opencv_includes.hpp> |
|
|
|
|
|
#include <opencv2/gapi/util/variant.hpp> |
|
|
#include <opencv2/gapi/util/throw.hpp> |
|
|
#include <opencv2/gapi/own/assert.hpp> |
|
|
|
|
|
#include <opencv2/gapi/gmat.hpp> |
|
|
#include <opencv2/gapi/gscalar.hpp> |
|
|
|
|
|
namespace cv |
|
|
{ |
|
|
|
|
|
|
|
|
class GNode; |
|
|
struct GOrigin; |
|
|
template<typename T> class GArray; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct GAPI_EXPORTS_W_SIMPLE GArrayDesc |
|
|
{ |
|
|
|
|
|
|
|
|
bool operator== (const GArrayDesc&) const { return true; } |
|
|
}; |
|
|
template<typename U> GArrayDesc descr_of(const std::vector<U> &) { return {};} |
|
|
GAPI_EXPORTS_W inline GArrayDesc empty_array_desc() {return {}; } |
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& os, const cv::GArrayDesc &desc); |
|
|
|
|
|
namespace detail |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class VectorRef; |
|
|
using ConstructVec = std::function<void(VectorRef&)>; |
|
|
|
|
|
|
|
|
struct TypeHintBase{virtual ~TypeHintBase() = default;}; |
|
|
|
|
|
|
|
|
template <typename T> |
|
|
struct TypeHint final : public TypeHintBase{}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GAPI_EXPORTS GArrayU |
|
|
{ |
|
|
public: |
|
|
GArrayU(const GNode &n, std::size_t out); |
|
|
|
|
|
template <typename T> |
|
|
bool holds() const; |
|
|
|
|
|
GOrigin& priv(); |
|
|
const GOrigin& priv() const; |
|
|
|
|
|
protected: |
|
|
GArrayU(); |
|
|
GArrayU(const detail::VectorRef& vref); |
|
|
template<class> friend class cv::GArray; |
|
|
|
|
|
void setConstructFcn(ConstructVec &&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 GArrayU::holds() const{ |
|
|
GAPI_Assert(m_hint != nullptr); |
|
|
using U = typename std::decay<T>::type; |
|
|
return dynamic_cast<TypeHint<U>*>(m_hint.get()) != nullptr; |
|
|
} |
|
|
|
|
|
template <typename T> |
|
|
void GArrayU::specifyType(){ |
|
|
m_hint.reset(new TypeHint<typename std::decay<T>::type>); |
|
|
} |
|
|
|
|
|
template <typename T> |
|
|
void GArrayU::storeKind(){ |
|
|
setKind(cv::detail::GOpaqueTraits<T>::kind); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BasicVectorRef |
|
|
{ |
|
|
public: |
|
|
|
|
|
std::size_t m_elemSize = 0ul; |
|
|
cv::GArrayDesc m_desc; |
|
|
virtual ~BasicVectorRef() {} |
|
|
|
|
|
virtual void mov(BasicVectorRef &ref) = 0; |
|
|
virtual const void* ptr() const = 0; |
|
|
virtual std::size_t size() const = 0; |
|
|
}; |
|
|
|
|
|
template<typename T> class VectorRefT final: public BasicVectorRef |
|
|
{ |
|
|
using empty_t = util::monostate; |
|
|
using ro_ext_t = const std::vector<T> *; |
|
|
using rw_ext_t = std::vector<T> *; |
|
|
using rw_own_t = std::vector<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 std::vector<T>* vec = nullptr) |
|
|
{ |
|
|
m_elemSize = sizeof(T); |
|
|
if (vec) m_desc = cv::descr_of(*vec); |
|
|
} |
|
|
|
|
|
public: |
|
|
VectorRefT() { init(); } |
|
|
virtual ~VectorRefT() {} |
|
|
|
|
|
explicit VectorRefT(const std::vector<T>& vec) : m_ref(&vec) { init(&vec); } |
|
|
explicit VectorRefT(std::vector<T>& vec) : m_ref(&vec) { init(&vec); } |
|
|
explicit VectorRefT(std::vector<T>&& vec) : m_ref(std::move(vec)) { init(&vec); } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void reset() |
|
|
{ |
|
|
if (isEmpty()) |
|
|
{ |
|
|
std::vector<T> empty_vector; |
|
|
m_desc = cv::descr_of(empty_vector); |
|
|
m_ref = std::move(empty_vector); |
|
|
GAPI_Assert(isRWOwn()); |
|
|
} |
|
|
else if (isRWOwn()) |
|
|
{ |
|
|
util::get<rw_own_t>(m_ref).clear(); |
|
|
} |
|
|
else GAPI_Error("InternalError"); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<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 std::vector<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(BasicVectorRef &v) override { |
|
|
VectorRefT<T> *tv = dynamic_cast<VectorRefT<T>*>(&v); |
|
|
GAPI_Assert(tv != nullptr); |
|
|
wref() = std::move(tv->wref()); |
|
|
} |
|
|
|
|
|
virtual const void* ptr() const override { return &rref(); } |
|
|
virtual std::size_t size() const override { return rref().size(); } |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class VectorRef |
|
|
{ |
|
|
std::shared_ptr<BasicVectorRef> m_ref; |
|
|
cv::detail::OpaqueKind m_kind = cv::detail::OpaqueKind::CV_UNKNOWN; |
|
|
|
|
|
template<typename T> inline void check() const |
|
|
{ |
|
|
GAPI_DbgAssert(dynamic_cast<VectorRefT<T>*>(m_ref.get()) != nullptr); |
|
|
GAPI_Assert(sizeof(T) == m_ref->m_elemSize); |
|
|
} |
|
|
|
|
|
public: |
|
|
VectorRef() = default; |
|
|
template<typename T> explicit VectorRef(const std::vector<T>& vec) |
|
|
: m_ref(new VectorRefT<T>(vec)) |
|
|
, m_kind(GOpaqueTraits<T>::kind) |
|
|
{} |
|
|
template<typename T> explicit VectorRef(std::vector<T>& vec) |
|
|
: m_ref(new VectorRefT<T>(vec)) |
|
|
, m_kind(GOpaqueTraits<T>::kind) |
|
|
{} |
|
|
template<typename T> explicit VectorRef(std::vector<T>&& vec) |
|
|
: m_ref(new VectorRefT<T>(std::move(vec))) |
|
|
, m_kind(GOpaqueTraits<T>::kind) |
|
|
{} |
|
|
|
|
|
cv::detail::OpaqueKind getKind() const |
|
|
{ |
|
|
return m_kind; |
|
|
} |
|
|
|
|
|
template<typename T> void reset() |
|
|
{ |
|
|
if (!m_ref) m_ref.reset(new VectorRefT<T>()); |
|
|
check<T>(); |
|
|
storeKind<T>(); |
|
|
static_cast<VectorRefT<T>&>(*m_ref).reset(); |
|
|
} |
|
|
|
|
|
template <typename T> |
|
|
void storeKind() |
|
|
{ |
|
|
m_kind = cv::detail::GOpaqueTraits<T>::kind; |
|
|
} |
|
|
|
|
|
template<typename T> std::vector<T>& wref() |
|
|
{ |
|
|
check<T>(); |
|
|
return static_cast<VectorRefT<T>&>(*m_ref).wref(); |
|
|
} |
|
|
|
|
|
template<typename T> const std::vector<T>& rref() const |
|
|
{ |
|
|
check<T>(); |
|
|
return static_cast<VectorRefT<T>&>(*m_ref).rref(); |
|
|
} |
|
|
|
|
|
|
|
|
template <typename T> bool holds() const |
|
|
{ |
|
|
if (!m_ref) return false; |
|
|
using U = typename std::decay<T>::type; |
|
|
return dynamic_cast<VectorRefT<U>*>(m_ref.get()) != nullptr; |
|
|
} |
|
|
|
|
|
void mov(VectorRef &v) |
|
|
{ |
|
|
m_ref->mov(*v.m_ref); |
|
|
} |
|
|
|
|
|
cv::GArrayDesc descr_of() const |
|
|
{ |
|
|
return m_ref->m_desc; |
|
|
} |
|
|
|
|
|
std::size_t size() const |
|
|
{ |
|
|
return m_ref->size(); |
|
|
} |
|
|
|
|
|
|
|
|
const void *ptr() const { return m_ref->ptr(); } |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(GAPI_STANDALONE) |
|
|
# define FLATTEN_NS cv::gapi::own |
|
|
#else |
|
|
# define FLATTEN_NS cv |
|
|
#endif |
|
|
template<class T> struct flatten_g; |
|
|
template<> struct flatten_g<cv::GMat> { using type = FLATTEN_NS::Mat; }; |
|
|
template<> struct flatten_g<cv::GScalar> { using type = FLATTEN_NS::Scalar; }; |
|
|
template<class T> struct flatten_g<GArray<T>> { using type = std::vector<T>; }; |
|
|
template<class T> struct flatten_g { using type = T; }; |
|
|
#undef FLATTEN_NS |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T> class GArray |
|
|
{ |
|
|
public: |
|
|
|
|
|
|
|
|
|
|
|
using HT = typename detail::flatten_g<typename std::decay<T>::type>::type; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
explicit GArray(const std::vector<HT>& v) |
|
|
: m_ref(detail::GArrayU(detail::VectorRef(v))) { putDetails(); } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
explicit GArray(std::vector<HT>&& v) |
|
|
: m_ref(detail::GArrayU(detail::VectorRef(std::move(v)))) { putDetails(); } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GArray() { putDetails(); } |
|
|
|
|
|
|
|
|
explicit GArray(detail::GArrayU &&ref) |
|
|
: m_ref(ref) { putDetails(); } |
|
|
|
|
|
|
|
|
detail::GArrayU strip() const { |
|
|
return m_ref; |
|
|
} |
|
|
|
|
|
static void VCtor(detail::VectorRef& vref) { |
|
|
vref.reset<HT>(); |
|
|
} |
|
|
|
|
|
private: |
|
|
void putDetails() { |
|
|
m_ref.setConstructFcn(&VCtor); |
|
|
m_ref.specifyType<HT>(); |
|
|
m_ref.storeKind<HT>(); |
|
|
} |
|
|
|
|
|
detail::GArrayU m_ref; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
#endif |
|
|
|