1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
5 // Copyright (C) 2018-2019 Intel Corporation
8 #ifndef OPENCV_GAPI_COMPILER_TRANSACTIONS_HPP
9 #define OPENCV_GAPI_COMPILER_TRANSACTIONS_HPP
11 #include <algorithm> // find_if
15 #include <ade/graph.hpp>
17 #include "opencv2/gapi/own/assert.hpp"
19 enum class Direction: int {Invalid, In, Out};
21 ////////////////////////////////////////////////////////////////////////////
23 // TODO: Probably it can be moved to ADE
29 virtual void commit (ade::Graph & ) {};
30 virtual void rollback(ade::Graph & ) {};
31 virtual ~Base() = default;
34 class NodeCreated final: public Base
36 ade::NodeHandle m_node;
38 explicit NodeCreated(const ade::NodeHandle &nh) : m_node(nh) {}
39 virtual void rollback(ade::Graph &g) override { g.erase(m_node); }
42 // NB: Drops all metadata stored in the EdgeHandle,
43 // which is not restored even in the rollback
45 // FIXME: either add a way for users to preserve meta manually
46 // or extend ADE to manipulate with meta such way
47 class DropLink final: public Base
49 ade::NodeHandle m_node;
52 ade::NodeHandle m_sibling;
55 DropLink(ade::Graph &g,
56 const ade::NodeHandle &node,
57 const ade::EdgeHandle &edge)
58 : m_node(node), m_dir(node == edge->srcNode()
62 m_sibling = (m_dir == Direction::In
68 virtual void rollback(ade::Graph &g) override
72 case Direction::In: g.link(m_sibling, m_node); break;
73 case Direction::Out: g.link(m_node, m_sibling); break;
74 default: GAPI_Assert(false);
79 class NewLink final: public Base
81 ade::EdgeHandle m_edge;
84 NewLink(ade::Graph &g,
85 const ade::NodeHandle &prod,
86 const ade::NodeHandle &cons)
87 : m_edge(g.link(prod, cons))
91 virtual void rollback(ade::Graph &g) override
97 class DropNode final: public Base
99 ade::NodeHandle m_node;
102 explicit DropNode(const ade::NodeHandle &nh)
105 // According to the semantic, node should be disconnected
106 // manually before it is dropped
107 GAPI_Assert(m_node->inEdges().size() == 0);
108 GAPI_Assert(m_node->outEdges().size() == 0);
111 virtual void commit(ade::Graph &g) override
119 std::list< std::unique_ptr<Base> > m_changes;
122 template<typename T, typename ...Args>
123 void enqueue(Args&&... args)
125 std::unique_ptr<Base> p(new T(args...));
126 m_changes.push_back(std::move(p));
129 void commit(ade::Graph &g)
131 // Commit changes in the forward order
132 for (auto& ch : m_changes) ch->commit(g);
135 void rollback(ade::Graph &g)
137 // Rollback changes in the reverse order
138 for (auto it = m_changes.rbegin(); it != m_changes.rend(); ++it)
144 } // namespace Change
145 ////////////////////////////////////////////////////////////////////////////
147 #endif // OPENCV_GAPI_COMPILER_TRANSACTIONS_HPP