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
|
/** @file
*
* List of COM objects
*/
/*
* Copyright (C) 2009-2010 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#ifndef ____H_OBJECTSLIST
#define ____H_OBJECTSLIST
#include <list>
#include <VBox/com/ptr.h>
/**
* Implements a "flat" objects list with a lock. Since each such list
* has its own lock it is not a good idea to implement trees with this.
*
* ObjectList<T> is designed to behave as if it were a std::list of
* COM pointers of class T; in other words,
* ObjectList<Medium> behaves like std::list< ComObjPtr<Medium> > but
* it's less typing. Iterators, front(), size(), begin() and end()
* are implemented.
*
* In addition it automatically includes an RWLockHandle which can be
* accessed with getLockHandle().
*
* If you need the raw std::list for some reason you can access it with
* getList().
*
* The destructor automatically calls uninit() on every contained
* COM object. If this is not desired, clear the member list before
* deleting the list object.
*/
template<typename T>
class ObjectsList
{
public:
typedef ComObjPtr<T> MyType;
typedef std::list<MyType> MyList;
typedef typename MyList::iterator iterator;
typedef typename MyList::const_iterator const_iterator;
// typename is necessary to disambiguate "::iterator" in templates; see
// the "this might hurt your head" part in
// http://www.parashift.com/c++-faq-lite/templates.html#faq-35.18
ObjectsList(RWLockHandle &lockHandle)
: m_lock(lockHandle)
{ }
~ObjectsList()
{
uninitAll();
}
private:
// prohibit copying and assignment
ObjectsList(const ObjectsList &d);
ObjectsList& operator=(const ObjectsList &d);
public:
/**
* Returns the lock handle which protects this list, for use with
* AutoReadLock or AutoWriteLock.
*/
RWLockHandle& getLockHandle()
{
return m_lock;
}
/**
* Calls m_ll.push_back(p) with locking.
* @param p
*/
void addChild(MyType p)
{
AutoWriteLock al(m_lock COMMA_LOCKVAL_SRC_POS);
m_ll.push_back(p);
}
/**
* Calls m_ll.remove(p) with locking. Does NOT call uninit()
* on the contained object.
* @param p
*/
void removeChild(MyType p)
{
AutoWriteLock al(m_lock COMMA_LOCKVAL_SRC_POS);
m_ll.remove(p);
}
/**
* Appends all objects from another list to the member list.
* Locks the other list for reading but does not lock "this"
* (because it might be on the caller's stack and needs no
* locking).
* @param ll
*/
void appendOtherList(ObjectsList<T> &ll)
{
AutoReadLock alr(ll.getLockHandle() COMMA_LOCKVAL_SRC_POS);
for (const_iterator it = ll.begin();
it != ll.end();
++it)
{
m_ll.push_back(*it);
}
}
/**
* Calls uninit() on every COM object on the list and then
* clears the list, with locking.
*/
void uninitAll()
{
AutoWriteLock al(m_lock COMMA_LOCKVAL_SRC_POS);
for (iterator it = m_ll.begin();
it != m_ll.end();
++it)
{
MyType &q = *it;
q->uninit();
}
m_ll.clear();
}
/**
* Returns the no. of objects on the list (std::list compatibility)
* with locking.
*/
size_t size()
{
AutoReadLock al(m_lock COMMA_LOCKVAL_SRC_POS);
return m_ll.size();
}
/**
* Returns a raw pointer to the member list of objects.
* Does not lock!
* @return
*/
MyList& getList()
{
return m_ll;
}
/**
* Returns the first object on the list (std::list compatibility)
* with locking.
*/
MyType front()
{
AutoReadLock al(m_lock COMMA_LOCKVAL_SRC_POS);
return m_ll.front();
}
/**
* Returns the begin iterator from the list (std::list compatibility).
* Does not lock!
* @return
*/
iterator begin()
{
return m_ll.begin();
}
/**
* Returns the end iterator from the list (std::list compatibility).
* Does not lock!
*/
iterator end()
{
return m_ll.end();
}
void insert(iterator it, MyType &p)
{
m_ll.insert(it, p);
}
void erase(iterator it)
{
m_ll.erase(it);
}
private:
MyList m_ll;
RWLockHandle &m_lock;
};
#endif
|