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
|
#include <pch.hpp>
#include "autotree.h"
#include "presenter.h"
#include "scriptLoader.h"
#include <boost/bind.hpp>
NAMEDFACTORY("autotree", AutoTree, ViewFactory);
AutoTree::AutoTree(ScriptNodePtr p) :
SourceObject(p),
View(p),
RowProcessor(p),
root(new AutoTreeNode(p->child("tree"), 0, 0)),
depth(0)
{
auto node = root;
while (node) {
depth += 1;
node = node->child();
}
}
AutoTree::~AutoTree()
{
}
void
AutoTree::loadComplete(const CommonObjects * co)
{
RowProcessor::loadComplete(co);
}
void
AutoTree::execute(const MultiRowSetPresenter * p, ExecContext * ec) const
{
AutoTreeState state;
for (unsigned int d = 0; d < depth; ++d) {
state.opened.push_back(AutoTreeState::Opened(false, false));
}
root->openArray(p, ec, state);
RowProcessor::execute(ec, boost::bind(&AutoTreeNode::rowReady, root, _1, p, ec, boost::ref(state)));
root->closeArray(p, ec, state);
}
AutoTreeNode::AutoTreeNode(ScriptNodePtr sn, unsigned int p, unsigned int d) :
rootName(sn, "rootname", Null()),
objectName(sn, "objectname", Null()),
pos(p),
depth(d)
{
for (const auto & n : sn->children("key")) {
keys.insert({n->value("name", NULL).as<Glib::ustring>(), Variable::fromScriptNode(n)} );
}
for (const auto & n : sn->children("include")) {
includes.insert({n->value("name", NULL).as<Glib::ustring>(), Variable::fromScriptNode(n)} );
}
if (sn->valueExists("tree")) {
tree = new AutoTreeNode(sn->child("tree"), pos + keys.size(), depth + 1);
}
}
void
AutoTreeNode::rowReady(const RowState * rs, const MultiRowSetPresenter * p, ExecContext * ec, AutoTreeState & state) const
{
std::vector<VariableType> values;
values.reserve(keys.size());
for (const auto & v : keys) {
values.push_back(v.second(ec));
}
bool isNew = false;
if (state.values.size() < keys.size() + pos) {
isNew = true;
}
else {
for (unsigned int k = 0; k < keys.size(); ++k) {
if (state.values[pos + k] != values[k]) {
isNew = true;
break;
}
}
}
if (isNew) {
closeObject(p, ec, state);
state.values.resize(pos);
openObject(p, ec, state);
unsigned int k = 0;
for (const auto & v : keys) {
state.values.push_back(values[k]);
p->addNamedValue(v.first, values[k++]);
}
for (const auto & v : includes) {
p->addNamedValue(v.first, v.second(ec));
}
if (tree) {
tree->openArray(p, ec, state);
}
}
if (tree) {
tree->rowReady(rs, p, ec, state);
}
}
#define arrayOpened opened[depth].get<0>()
#define objectOpened opened[depth].get<1>()
void
AutoTreeNode::openArray(const MultiRowSetPresenter * p, ExecContext * ec, AutoTreeState & state) const
{
if (!state.arrayOpened && !rootName(ec).isNull()) {
p->addNewRowSet(rootName(ec));
state.arrayOpened = true;
}
}
void
AutoTreeNode::openObject(const MultiRowSetPresenter * p, ExecContext * ec, AutoTreeState & state) const
{
if (!state.objectOpened && !objectName(ec).isNull()) {
p->addNewRowSet(objectName(ec));
state.objectOpened = true;
}
}
void
AutoTreeNode::closeArray(const MultiRowSetPresenter * p, ExecContext * ec, AutoTreeState & state) const
{
closeObject(p, ec, state);
if (state.arrayOpened && !rootName(ec).isNull()) {
p->finishRowSet();
state.arrayOpened = false;
}
}
void
AutoTreeNode::closeObject(const MultiRowSetPresenter * p, ExecContext * ec, AutoTreeState & state) const
{
if (tree) {
tree->closeArray(p, ec, state);
}
if (state.objectOpened && !objectName(ec).isNull()) {
p->finishRow();
state.objectOpened = false;
}
}
AutoTreeNodePtr
AutoTreeNode::child() const
{
return tree;
}
|