Path: blob/master/modules/gapi/src/compiler/transactions.hpp
16337 views
// This file is part of OpenCV project.1// It is subject to the license terms in the LICENSE file found in the top-level directory2// of this distribution and at http://opencv.org/license.html.3//4// Copyright (C) 2018 Intel Corporation567#ifndef OPENCV_GAPI_COMPILER_TRANSACTIONS_HPP8#define OPENCV_GAPI_COMPILER_TRANSACTIONS_HPP910#include <algorithm> // find_if11#include <functional>12#include <list>1314#include <ade/graph.hpp>1516#include "opencv2/gapi/own/assert.hpp"1718enum class Direction: int {Invalid, In, Out};1920////////////////////////////////////////////////////////////////////////////21////22// TODO: Probably it can be moved to ADE2324namespace Change25{26struct Base27{28virtual void commit (ade::Graph & ) {};29virtual void rollback(ade::Graph & ) {};30virtual ~Base() = default;31};3233class NodeCreated final: public Base34{35ade::NodeHandle m_node;36public:37explicit NodeCreated(const ade::NodeHandle &nh) : m_node(nh) {}38virtual void rollback(ade::Graph &g) override { g.erase(m_node); }39};4041// NB: Drops all metadata stored in the EdgeHandle,42// which is not restored even in the rollback4344// FIXME: either add a way for users to preserve meta manually45// or extend ADE to manipulate with meta such way46class DropLink final: public Base47{48ade::NodeHandle m_node;49Direction m_dir;5051ade::NodeHandle m_sibling;5253public:54DropLink(ade::Graph &g,55const ade::NodeHandle &node,56const ade::EdgeHandle &edge)57: m_node(node), m_dir(node == edge->srcNode()58? Direction::Out59: Direction::In)60{61m_sibling = (m_dir == Direction::In62? edge->srcNode()63: edge->dstNode());64g.erase(edge);65}6667virtual void rollback(ade::Graph &g) override68{69switch(m_dir)70{71case Direction::In: g.link(m_sibling, m_node); break;72case Direction::Out: g.link(m_node, m_sibling); break;73default: GAPI_Assert(false);74}75}76};7778class NewLink final: public Base79{80ade::EdgeHandle m_edge;8182public:83NewLink(ade::Graph &g,84const ade::NodeHandle &prod,85const ade::NodeHandle &cons)86: m_edge(g.link(prod, cons))87{88}8990virtual void rollback(ade::Graph &g) override91{92g.erase(m_edge);93}94};9596class DropNode final: public Base97{98ade::NodeHandle m_node;99100public:101explicit DropNode(const ade::NodeHandle &nh)102: m_node(nh)103{104// According to the semantic, node should be disconnected105// manually before it is dropped106GAPI_Assert(m_node->inEdges().size() == 0);107GAPI_Assert(m_node->outEdges().size() == 0);108}109110virtual void commit(ade::Graph &g) override111{112g.erase(m_node);113}114};115116class List117{118std::list< std::unique_ptr<Base> > m_changes;119120public:121template<typename T, typename ...Args>122void enqueue(Args&&... args)123{124std::unique_ptr<Base> p(new T(args...));125m_changes.push_back(std::move(p));126}127128void commit(ade::Graph &g)129{130// Commit changes in the forward order131for (auto& ch : m_changes) ch->commit(g);132}133134void rollback(ade::Graph &g)135{136// Rollback changes in the reverse order137for (auto it = m_changes.rbegin(); it != m_changes.rend(); ++it)138{139(*it)->rollback(g);140}141}142};143} // namespace Change144////////////////////////////////////////////////////////////////////////////145146#endif // OPENCV_GAPI_COMPILER_TRANSACTIONS_HPP147148149