-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDbTree.cc
180 lines (162 loc) · 3.63 KB
/
DbTree.cc
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
#include <iostream>
#include <stdexcept>
#include <sstream>
#include "Common.hh"
#include "DbTree.hh"
// Key
Key::Key() : _key(0), _parent(0)
{
}
Key::Key(const DbVal* key, const Type* parent) : _key(key), _parent(parent)
{
}
void
Key::add(Type* ty)
{
const char* t = ty->getType();
Types::iterator i = _types.find(t);
if (i != _types.end()) {
std::stringstream ss;
ss << "Key::add, type allready exists: " << t;
throw std::invalid_argument(ss.str());
}
_types[t] = ty;
}
const DbVal*
Key::getKey() const
{
return _key;
}
Type*
Key::findOrCreate(const std::string& s,
Yang::Ast::InteriorNode* schemaRef)
{
Type* result;
Types::const_iterator i = _types.find(s.c_str());
if (i == _types.end()) {
result = new Type(s, this, schemaRef);
_types[result->getType()] = result;
} else {
result = i->second;
}
return result;
}
Type*
Key::find(const std::string& s)
{
Type* result = 0;
Types::const_iterator i = _types.find(s.c_str());
if (i != _types.end()) {
result = i->second;
}
return result;
}
std::vector<Vals>&
Key::getLeafs()
{
return _leafs;
}
void
Key::replace(const std::vector<Vals>& leafs)
{
// @todo this most likely leaks
_leafs = leafs;
}
void
Key::print() const
{
std::cerr << "Key:";
_key->put(std::cerr);
std::cerr << " {" << std::endl;
for (unsigned int i = 0; i < _leafs.size(); i++) {
std::cerr << "attr[" << i << "] = {";
const std::list<const DbVal*>& vals = _leafs[i];
for (std::list<const DbVal*>::const_iterator j = vals.begin();
j != vals.end();
++j) {
if (j != vals.begin()) std::cerr << ", ";
(*j)->put(std::cerr);
}
std::cerr << "}" << std::endl;
}
for (Types::const_iterator i = _types.begin();
i != _types.end();
++i) {
i->second->print();
}
std::cerr << "}" << std::endl;
}
Yang::Ast::InteriorNode&
Key::getSchemaObj()
{
if (_parent == 0) {
Common::Error::err(__PRETTY_FUNCTION__, "Should not happen");
}
return const_cast<Type*>(_parent)->getSchemaObj();
}
// Type
Type::Type(const std::string& t,
const Key* parent,
Yang::Ast::InteriorNode* schemaRef) :
_type(t),
_parent(parent),
_schemaRef(schemaRef)
{
}
void
Type::add(Key* key)
{
const DbVal* k = key->getKey();
Keys::iterator i = _keys.find(k);
if (i != _keys.end()) {
std::stringstream ss;
ss << "Type::add, key allready exists: " << k;
throw std::invalid_argument(ss.str());
}
_keys[k] = key;
}
Key*
Type::findOrCreate(const DbVal* k, const Type* parent)
{
Key* result;
Keys::iterator i = _keys.find(k);
if (i == _keys.end()) {
result = new Key(k, parent);
_keys[k] = result;
} else {
delete k;
result = i->second;
}
return result;
}
Key*
Type::find(const DbVal* k)
{
Key* result = 0;
Keys::iterator i = _keys.find(k);
if (i != _keys.end()) {
result = i->second;
}
return result;
}
const char*
Type::getType() const
{
return _type.c_str();
}
Keys&
Type::getKeys()
{
return _keys;
}
void
Type::print() const
{
std::cerr << "Type:" << _type << " {" << std::endl;
for (Keys::const_iterator i = _keys.begin();
i != _keys.end();
++i) {
i->second->print();
}
std::cerr << "}" << std::endl;
}