transactions.hpp
5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// 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>
#include "opencv2/gapi/util/util.hpp" // Seq
#include "opencv2/gapi/own/assert.hpp"
enum class Direction: int {Invalid, In, Out};
////////////////////////////////////////////////////////////////////////////
////
// TODO: Probably it can be moved to ADE
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
{
struct Base
{
virtual void commit (ade::Graph & ) {};
virtual void rollback(ade::Graph & ) {};
virtual ~Base() = default;
};
template<typename H> using Preserved = ::Preserved<H, Metatypes...>;
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); }
};
// FIXME: maybe extend ADE to clone/copy the whole metadata?
class DropLink final: public Base
{
ade::NodeHandle m_node;
Direction m_dir;
ade::NodeHandle m_sibling;
Preserved<ade::EdgeHandle> m_meta;
public:
DropLink(ade::Graph &g,
const ade::NodeHandle &node,
const ade::EdgeHandle &edge)
: m_node(node)
, m_dir(node == edge->srcNode() ? Direction::Out : Direction::In)
, m_meta(g, edge)
{
m_sibling = (m_dir == Direction::In
? edge->srcNode()
: edge->dstNode());
g.erase(edge);
}
virtual void rollback(ade::Graph &g) override
{
// FIXME: Need to preserve metadata here!
// GIslandModel edges now have metadata
ade::EdgeHandle eh;
switch(m_dir)
{
case Direction::In: eh = g.link(m_sibling, m_node); break;
case Direction::Out: eh = g.link(m_node, m_sibling); break;
default: GAPI_Assert(false);
}
GAPI_Assert(eh != nullptr);
m_meta.copyTo(g, eh);
}
};
class NewLink final: public Base
{
ade::EdgeHandle m_edge;
public:
NewLink(ade::Graph &g,
const ade::NodeHandle &prod,
const ade::NodeHandle &cons,
const ade::EdgeHandle ©_from = ade::EdgeHandle())
: m_edge(g.link(prod, cons))
{
if (copy_from != nullptr)
{
Preserved<ade::EdgeHandle>(g, copy_from).copyTo(g, m_edge);
}
}
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
GAPI_Assert(m_node->inEdges().size() == 0);
GAPI_Assert(m_node->outEdges().size() == 0);
}
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);
}
}
};
}; // struct Change
////////////////////////////////////////////////////////////////////////////
#endif // OPENCV_GAPI_COMPILER_TRANSACTIONS_HPP