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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
/*
Ousía
Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _OUSIA_MANAGED_CONTAINERS_H_
#define _OUSIA_MANAGED_CONTAINERS_H_
#include "Managed.hpp"
namespace ousia {
/**
* Template class which can be used to collect "Owned" refrences to a certain
* type of managed object. Do not use this class directly, use ManagedMap or
* ManagedVector instead. This class only provides functionality which is common
* to list and map containers (iterators and state).
*
* @param T is the type of the Managed object that should be managed.
* @param Collection should be a STL container of Owned<T>
*/
template <class T, class Collection>
class ManagedContainer {
public:
using collection_type = Collection;
using value_type = typename collection_type::value_type;
using reference = typename collection_type::reference;
using const_reference = typename collection_type::const_reference;
using iterator = typename collection_type::iterator;
using const_iterator = typename collection_type::const_iterator;
using size_type = typename collection_type::size_type;
protected:
/**
* Handle containing a reference to the owner of the collection.
*/
Handle<Managed> owner;
/**
* Underlying STL collection.
*/
collection_type c;
protected:
/**
* Function which can be overridden by child classes to execute special code
* whenever a new element is added to the collection.
*/
virtual void addManaged(value_type h) {}
/**
* Function which can be overriden by child classes to execute special code
* whenever an element is removed from the collection.
*/
virtual void deleteManaged(value_type h) {}
public:
/**
* Constructor of the ManagedContainer class.
*
* @param owner is the managed object which owns the collection and all
* handles to other managed objects stored within.
*/
ManagedContainer(Handle<Managed> owner) : owner(owner){};
/* State functions */
size_type size() const noexcept { return c.size(); }
bool empty() const noexcept { return c.empty(); }
/* Iterators */
iterator begin() { return c.begin(); }
iterator end() { return c.end(); }
iterator rbegin() { return c.rbegin(); }
iterator rend() { return c.rend(); }
const_iterator begin() const { return c.cbegin(); }
const_iterator end() const { return c.cend(); }
const_iterator cbegin() const { return c.cbegin(); }
const_iterator cend() const { return c.cend(); }
const_iterator rbegin() const { return c.crbegin(); }
const_iterator rend() const { return c.crend(); }
const_iterator crbegin() const { return c.crbegin(); }
const_iterator crend() const { return c.crend(); }
/* Clear function */
void clear() noexcept
{
for (const_iterator it = cbegin(); it != cend(); it++) {
deleteManaged(*it);
}
c.clear();
}
};
/**
* Template class which can be used to collect "Owned" refrences to a certain
* type of managed object. This class should be used in favour of other
* collections of handles, it takes care of acquiring an owned handle from the
* owner of this collection whenever a new element is added.
*
* @param T is the type of the Managed object that should be managed.
* @param Collection should be a STL list container of Owned<T>
*/
template <class T, class Collection>
class ManagedGenericList : public ManagedContainer() {
public:
using ManagedContainer<T, Collection>::ManagedContainer;
/**
* Initialize with an iterator from another collection.
*
* @param owner is the managed object which owns the collection and all
* handles to other managed objects stored within.
* @param first is an iterator pointing at the first element to be copied
* from some other collection.
* @param last is an iterator pointing at the last element to be copied
* from some other collection.
*/
template <class InputIterator>
ManagedGenericList(Handle<Managed> owner, InputIterator first, InputIterator last)
: ManagedContainer<T, Collection>(owner)
{
insert(c.begin(), first, last);
}
/**
* Initialize with another collection.
*
* @param owner is the managed object which owns the collection and all
* handles to other managed objects stored within.
* @param in is a reference at some other collection with content that
* should be copied.
*/
template <class InputCollection>
ManagedGenericList(Handle<Managed> owner, const InputCollection &in)
: ManagedContainer<T, Collection>(owner)
{
for (const auto &e : in) {
push_back(e);
}
}
/* Front and back */
reference front() { return c.front(); }
const_reference front() const { return c.front(); }
reference back() { return c.back(); }
const_reference back() const { return c.back(); }
/* Insert and delete operations */
iterator insert(const_iterator position, Handle<T> h)
{
value_type v = owner->acquire(h);
addManaged(v);
return c.insert(position, owner->acquire(h));
}
template <class InputIterator>
iterator insert(const_iterator position, InputIterator first,
InputIterator last)
{
bool first = true;
const_iterator pos = position;
for (InputIterator it = first; it != last; it++) {
if (first) {
first = false;
} else {
pos++;
}
pos = insert(pos, *it);
}
return pos;
}
iterator find(const Handle<T> h)
{
for (iterator it = begin(); it != end(); it++) {
if (*it == h) {
return it;
}
}
return end();
}
const_iterator find(const Handle<T> h) const
{
for (const_iterator it = cbegin(); it != cend(); it++) {
if (*it == h) {
return it;
}
}
return cend();
}
void push_back(Handle<T> h)
{
Rooted<T> rooted{h};
addManaged(rooted);
c.push_back(owner->acquire(rooted));
}
void pop_back()
{
if (!empty()) {
deleteElement(c.back());
}
c.pop_back();
}
iterator erase(iterator position)
{
deleteManaged(*position);
return c.erase(position);
}
iterator erase(iterator first, iterator last)
{
for (const_iterator it = first; it != last; it++) {
deleteManaged(*it);
}
return c.erase(first, last);
}
};
/**
* Special type of ManagedContainer based on an STL map.
*/
template <class K, class T, class Collection>
class ManagedGenericMap : public ManagedCollection<T, Collection> {
private:
value_type acquirePair(std::pair<K, Handle<T>> val)
{
return std::pair<const K, T>{val->first, owner->acquire(val->second)};
}
public:
/**
* Initialize with an iterator from another collection.
*
* @param owner is the managed object which owns the collection and all
* handles to other managed objects stored within.
* @param first is an iterator pointing at the first element to be copied
* from some other collection.
* @param last is an iterator pointing at the last element to be copied
* from some other collection.
*/
template <class InputIterator>
ManagedGenericMap(Handle<Managed> owner, InputIterator first, InputIterator last)
: ManagedContainer<T, Collection>(owner)
{
insert(first, last);
}
/**
* Initialize with another collection.
*
* @param owner is the managed object which owns the collection and all
* handles to other managed objects stored within.
* @param in is a reference at some other collection with content that
* should be copied.
*/
template <class InputCollection>
ManagedGenericMap(Handle<Managed> owner, const InputCollection &in)
: ManagedContainer<T, Collection>(owner)
{
for (const auto &e : in) {
insert(*in);
}
}
std::pair<iterator, bool> insert(std::pair<K, Handle<T>> val)
{
value_type v = acquirePair(val);
addManaged(v);
return c.insert(v);
}
iterator insert(const_iterator position, std::pair<K, Handle<T>> val)
{
value_type v = acquirePair(val);
addManaged(v);
return c.insert(position, v);
}
template <class InputIterator>
void insert(InputIterator first, InputIterator last)
{
for (auto it = first; it != last; it++) {
insert(acquirePair);
}
}
iterator erase(const_iterator position)
{
deleteManaged(*position);
return c.erase(position);
}
size_t erase(const key_type &k)
{
iterator pos = find(k);
if (pos != end()) {
erase(pos);
return 1;
}
return 0;
}
iterator erase(const_iterator first, const_iterator last)
{
for (const_iterator it = first; it != last; it++) {
deleteManaged(*it);
}
return c.erase(first, last);
}
iterator find(const key_type &k) { return c.find(k); }
const_iterator find(const key_type &k) const { return c.find(k); }
};
/**
* Special type of ManagedGenericList based on an STL vector.
*/
template <class T>
class ManagedVector : public ManagedGenericList<T, std::vector<Owned<T>>> {
public:
using ManagedContainer<T, std::vector<Owned<T>>>::ManagedContainer;
};
/**
* Special type of ManagedGenericMap based on an STL map.
*/
template <class K, class T>
class ManagedMap : public ManagedGenericMap<K, T, std::map<K, Owned<T>>> {
public:
using ManagedGenericMap<K, T, std::map<K, Owned<T>>>::ManagedMap;
};
}
#endif /* _OUSIA_MANAGED_CONTAINERS_H_ */
|