1 | /** @file
|
---|
2 | *
|
---|
3 | * List of COM objects
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #ifndef ____H_OBJECTSLIST
|
---|
23 | #define ____H_OBJECTSLIST
|
---|
24 |
|
---|
25 | #include <list>
|
---|
26 | #include <VBox/com/ptr.h>
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Implements a "flat" objects list with a lock. Since each such list
|
---|
30 | * has its own lock it is not a good idea to implement trees with this.
|
---|
31 | *
|
---|
32 | * ObjectList<T> is designed to behave as if it were a std::list of
|
---|
33 | * COM pointers of class T; in other words,
|
---|
34 | * ObjectList<Medium> behaves like std::list< ComObjPtr<Medium> > but
|
---|
35 | * it's less typing. Iterators, front(), size(), begin() and end()
|
---|
36 | * are implemented.
|
---|
37 | *
|
---|
38 | * In addition it automatically includes an RWLockHandle which can be
|
---|
39 | * accessed with getLockHandle().
|
---|
40 | *
|
---|
41 | * If you need the raw std::list for some reason you can access it with
|
---|
42 | * getList().
|
---|
43 | *
|
---|
44 | * The destructor automatically calls uninit() on every contained
|
---|
45 | * COM object. If this is not desired, clear the member list before
|
---|
46 | * deleting the list object.
|
---|
47 | */
|
---|
48 | template<typename T>
|
---|
49 | class ObjectsList
|
---|
50 | {
|
---|
51 | public:
|
---|
52 | typedef ComObjPtr<T, ComStrongRef> MyType;
|
---|
53 | typedef std::list<MyType> MyList;
|
---|
54 |
|
---|
55 | typedef typename MyList::iterator iterator;
|
---|
56 | typedef typename MyList::const_iterator const_iterator;
|
---|
57 | // typename is necessary to de-ambiguate "::iterator" in templates; see
|
---|
58 | // the "this might hurt your head" part in
|
---|
59 | // http://www.parashift.com/c++-faq-lite/templates.html#faq-35.18
|
---|
60 |
|
---|
61 | ObjectsList(MainLockValidationClasses lockClass)
|
---|
62 | : m_lock(lockClass)
|
---|
63 | { }
|
---|
64 |
|
---|
65 | ~ObjectsList()
|
---|
66 | {
|
---|
67 | uninitAll();
|
---|
68 | }
|
---|
69 |
|
---|
70 | private:
|
---|
71 | // prohibit copying and assignment
|
---|
72 | ObjectsList(const ObjectsList &d);
|
---|
73 | ObjectsList& operator=(const ObjectsList &d);
|
---|
74 |
|
---|
75 | public:
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Returns the lock handle which protects this list, for use with
|
---|
79 | * AutoReadLock or AutoWriteLock.
|
---|
80 | */
|
---|
81 | RWLockHandle& getLockHandle()
|
---|
82 | {
|
---|
83 | return m_lock;
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Calls m_ll.push_back(p) with locking.
|
---|
88 | * @param p
|
---|
89 | */
|
---|
90 | void addChild(MyType p)
|
---|
91 | {
|
---|
92 | AutoWriteLock al(m_lock COMMA_LOCKVAL_SRC_POS);
|
---|
93 | m_ll.push_back(p);
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Calls m_ll.remove(p) with locking. Does NOT call uninit()
|
---|
98 | * on the contained object.
|
---|
99 | * @param p
|
---|
100 | */
|
---|
101 | void removeChild(MyType p)
|
---|
102 | {
|
---|
103 | AutoWriteLock al(m_lock COMMA_LOCKVAL_SRC_POS);
|
---|
104 | m_ll.remove(p);
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Appends all objects from another list to the member list.
|
---|
109 | * Locks the other list for reading but does not lock "this"
|
---|
110 | * (because it might be on the caller's stack and needs no
|
---|
111 | * locking).
|
---|
112 | * @param ll
|
---|
113 | */
|
---|
114 | void appendOtherList(ObjectsList<T> &ll)
|
---|
115 | {
|
---|
116 | AutoReadLock alr(ll.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
117 | for (const_iterator it = ll.begin();
|
---|
118 | it != ll.end();
|
---|
119 | ++it)
|
---|
120 | {
|
---|
121 | m_ll.push_back(*it);
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Calls uninit() on every COM object on the list and then
|
---|
127 | * clears the list, with locking.
|
---|
128 | */
|
---|
129 | void uninitAll()
|
---|
130 | {
|
---|
131 | AutoWriteLock al(m_lock COMMA_LOCKVAL_SRC_POS);
|
---|
132 | for (iterator it = m_ll.begin();
|
---|
133 | it != m_ll.end();
|
---|
134 | ++it)
|
---|
135 | {
|
---|
136 | MyType &q = *it;
|
---|
137 | q->uninit();
|
---|
138 | }
|
---|
139 | m_ll.clear();
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Returns the no. of objects on the list (std::list compatibility)
|
---|
144 | * with locking.
|
---|
145 | */
|
---|
146 | size_t size()
|
---|
147 | {
|
---|
148 | AutoReadLock al(m_lock COMMA_LOCKVAL_SRC_POS);
|
---|
149 | return m_ll.size();
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Returns a raw pointer to the member list of objects.
|
---|
154 | * Does not lock!
|
---|
155 | * @return
|
---|
156 | */
|
---|
157 | MyList& getList()
|
---|
158 | {
|
---|
159 | return m_ll;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Returns the first object on the list (std::list compatibility)
|
---|
164 | * with locking.
|
---|
165 | */
|
---|
166 | MyType front()
|
---|
167 | {
|
---|
168 | AutoReadLock al(m_lock COMMA_LOCKVAL_SRC_POS);
|
---|
169 | return m_ll.front();
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Returns the begin iterator from the list (std::list compatibility).
|
---|
174 | * Does not lock!
|
---|
175 | * @return
|
---|
176 | */
|
---|
177 | iterator begin()
|
---|
178 | {
|
---|
179 | return m_ll.begin();
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Returns the end iterator from the list (std::list compatibility).
|
---|
184 | * Does not lock!
|
---|
185 | */
|
---|
186 | iterator end()
|
---|
187 | {
|
---|
188 | return m_ll.end();
|
---|
189 | }
|
---|
190 |
|
---|
191 | void insert(iterator it, MyType &p)
|
---|
192 | {
|
---|
193 | m_ll.insert(it, p);
|
---|
194 | }
|
---|
195 |
|
---|
196 | void erase(iterator it)
|
---|
197 | {
|
---|
198 | m_ll.erase(it);
|
---|
199 | }
|
---|
200 |
|
---|
201 | private:
|
---|
202 | MyList m_ll;
|
---|
203 | RWLockHandle m_lock;
|
---|
204 | };
|
---|
205 |
|
---|
206 | #endif
|
---|