2018-09-27 02:50:39 +08:00
|
|
|
// 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) 2018 Intel Corporation
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef OPENCV_GAPI_COMPILER_TRANSACTIONS_HPP
|
|
|
|
#define OPENCV_GAPI_COMPILER_TRANSACTIONS_HPP
|
|
|
|
|
|
|
|
#include <algorithm> // find_if
|
|
|
|
#include <functional>
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
#include <ade/graph.hpp>
|
|
|
|
|
2020-03-18 07:38:24 +08:00
|
|
|
#include "opencv2/gapi/util/util.hpp" // Seq
|
2018-09-28 23:42:09 +08:00
|
|
|
#include "opencv2/gapi/own/assert.hpp"
|
2018-09-27 02:50:39 +08:00
|
|
|
|
|
|
|
enum class Direction: int {Invalid, In, Out};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
////
|
|
|
|
// TODO: Probably it can be moved to ADE
|
2020-03-18 07:38:24 +08:00
|
|
|
template<class H, class... Metatypes>
|
|
|
|
class Preserved
|
|
|
|
{
|
|
|
|
using S = typename cv::detail::MkSeq<sizeof...(Metatypes)>::type;
|
|
|
|
std::tuple<cv::util::optional<Metatypes>...> m_data;
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
cv::util::optional<T> get(ade::ConstTypedGraph<Metatypes...> g, H h) {
|
|
|
|
return g.metadata(h).template contains<T>()
|
|
|
|
? cv::util::make_optional(g.metadata(h).template get<T>())
|
|
|
|
: cv::util::optional<T>{};
|
|
|
|
}
|
|
|
|
template<std::size_t Id>
|
|
|
|
int set(ade::TypedGraph<Metatypes...> &g, H &h) {
|
|
|
|
const auto &opt = std::get<Id>(m_data);
|
|
|
|
if (opt.has_value())
|
|
|
|
g.metadata(h).set(opt.value());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
template<int... IIs>
|
|
|
|
void copyTo_impl(ade::TypedGraph<Metatypes...> &g, H h, cv::detail::Seq<IIs...>) {
|
|
|
|
int unused[] = {0, set<IIs>(g, h)...};
|
|
|
|
(void) unused;
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
Preserved(const ade::Graph &g, H h) {
|
|
|
|
ade::ConstTypedGraph<Metatypes...> tg(g);
|
|
|
|
m_data = std::make_tuple(get<Metatypes>(tg, h)...);
|
|
|
|
}
|
|
|
|
void copyTo(ade::Graph &g, H h) {
|
|
|
|
ade::TypedGraph<Metatypes...> tg(g);
|
|
|
|
copyTo_impl(tg, h, S{});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Do nothing if there's no metadata
|
|
|
|
template<class H>
|
|
|
|
class Preserved<H> {
|
|
|
|
public:
|
|
|
|
Preserved(const ade::Graph &, H) {}
|
|
|
|
void copyTo(ade::Graph &, H) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class... Metatypes>
|
|
|
|
struct ChangeT
|
2018-09-27 02:50:39 +08:00
|
|
|
{
|
|
|
|
struct Base
|
|
|
|
{
|
2021-10-27 20:19:05 +08:00
|
|
|
virtual void commit (ade::Graph & ) {}
|
|
|
|
virtual void rollback(ade::Graph & ) {}
|
2018-09-27 02:50:39 +08:00
|
|
|
virtual ~Base() = default;
|
|
|
|
};
|
|
|
|
|
2020-03-18 07:38:24 +08:00
|
|
|
template<typename H> using Preserved = ::Preserved<H, Metatypes...>;
|
|
|
|
|
2018-09-27 02:50:39 +08:00
|
|
|
class NodeCreated final: public Base
|
|
|
|
{
|
|
|
|
ade::NodeHandle m_node;
|
|
|
|
public:
|
|
|
|
explicit NodeCreated(const ade::NodeHandle &nh) : m_node(nh) {}
|
|
|
|
virtual void rollback(ade::Graph &g) override { g.erase(m_node); }
|
|
|
|
};
|
|
|
|
|
2020-03-18 07:38:24 +08:00
|
|
|
// FIXME: maybe extend ADE to clone/copy the whole metadata?
|
2018-09-27 02:50:39 +08:00
|
|
|
class DropLink final: public Base
|
|
|
|
{
|
|
|
|
ade::NodeHandle m_node;
|
|
|
|
Direction m_dir;
|
|
|
|
|
|
|
|
ade::NodeHandle m_sibling;
|
|
|
|
|
2020-03-18 07:38:24 +08:00
|
|
|
Preserved<ade::EdgeHandle> m_meta;
|
|
|
|
|
2018-09-27 02:50:39 +08:00
|
|
|
public:
|
|
|
|
DropLink(ade::Graph &g,
|
|
|
|
const ade::NodeHandle &node,
|
|
|
|
const ade::EdgeHandle &edge)
|
2020-03-18 07:38:24 +08:00
|
|
|
: m_node(node)
|
|
|
|
, m_dir(node == edge->srcNode() ? Direction::Out : Direction::In)
|
|
|
|
, m_meta(g, edge)
|
2018-09-27 02:50:39 +08:00
|
|
|
{
|
|
|
|
m_sibling = (m_dir == Direction::In
|
|
|
|
? edge->srcNode()
|
|
|
|
: edge->dstNode());
|
|
|
|
g.erase(edge);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void rollback(ade::Graph &g) override
|
|
|
|
{
|
2020-03-18 07:38:24 +08:00
|
|
|
// FIXME: Need to preserve metadata here!
|
|
|
|
// GIslandModel edges now have metadata
|
|
|
|
ade::EdgeHandle eh;
|
2018-09-27 02:50:39 +08:00
|
|
|
switch(m_dir)
|
|
|
|
{
|
2020-03-18 07:38:24 +08:00
|
|
|
case Direction::In: eh = g.link(m_sibling, m_node); break;
|
|
|
|
case Direction::Out: eh = g.link(m_node, m_sibling); break;
|
2018-09-28 23:42:09 +08:00
|
|
|
default: GAPI_Assert(false);
|
2018-09-27 02:50:39 +08:00
|
|
|
}
|
2020-03-18 07:38:24 +08:00
|
|
|
GAPI_Assert(eh != nullptr);
|
|
|
|
m_meta.copyTo(g, eh);
|
2018-09-27 02:50:39 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class NewLink final: public Base
|
|
|
|
{
|
|
|
|
ade::EdgeHandle m_edge;
|
|
|
|
|
|
|
|
public:
|
|
|
|
NewLink(ade::Graph &g,
|
2020-03-18 07:38:24 +08:00
|
|
|
const ade::NodeHandle &prod,
|
|
|
|
const ade::NodeHandle &cons,
|
|
|
|
const ade::EdgeHandle ©_from = ade::EdgeHandle())
|
2018-09-27 02:50:39 +08:00
|
|
|
: m_edge(g.link(prod, cons))
|
|
|
|
{
|
2020-03-18 07:38:24 +08:00
|
|
|
if (copy_from != nullptr)
|
|
|
|
{
|
|
|
|
Preserved<ade::EdgeHandle>(g, copy_from).copyTo(g, m_edge);
|
|
|
|
}
|
2018-09-27 02:50:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void rollback(ade::Graph &g) override
|
|
|
|
{
|
|
|
|
g.erase(m_edge);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class DropNode final: public Base
|
|
|
|
{
|
|
|
|
ade::NodeHandle m_node;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit DropNode(const ade::NodeHandle &nh)
|
|
|
|
: m_node(nh)
|
|
|
|
{
|
|
|
|
// According to the semantic, node should be disconnected
|
|
|
|
// manually before it is dropped
|
2018-09-28 23:42:09 +08:00
|
|
|
GAPI_Assert(m_node->inEdges().size() == 0);
|
|
|
|
GAPI_Assert(m_node->outEdges().size() == 0);
|
2018-09-27 02:50:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void commit(ade::Graph &g) override
|
|
|
|
{
|
|
|
|
g.erase(m_node);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class List
|
|
|
|
{
|
|
|
|
std::list< std::unique_ptr<Base> > m_changes;
|
|
|
|
|
|
|
|
public:
|
|
|
|
template<typename T, typename ...Args>
|
|
|
|
void enqueue(Args&&... args)
|
|
|
|
{
|
|
|
|
std::unique_ptr<Base> p(new T(args...));
|
|
|
|
m_changes.push_back(std::move(p));
|
|
|
|
}
|
|
|
|
|
|
|
|
void commit(ade::Graph &g)
|
|
|
|
{
|
|
|
|
// Commit changes in the forward order
|
|
|
|
for (auto& ch : m_changes) ch->commit(g);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rollback(ade::Graph &g)
|
|
|
|
{
|
|
|
|
// Rollback changes in the reverse order
|
|
|
|
for (auto it = m_changes.rbegin(); it != m_changes.rend(); ++it)
|
|
|
|
{
|
|
|
|
(*it)->rollback(g);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-03-18 07:38:24 +08:00
|
|
|
}; // struct Change
|
2018-09-27 02:50:39 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#endif // OPENCV_GAPI_COMPILER_TRANSACTIONS_HPP
|