VirtualBox

source: vbox/trunk/src/VBox/Main/include/objectslist.h@ 25728

Last change on this file since 25728 was 25310, checked in by vboxsync, 15 years ago

Main: lock validator, first batch: implement per-thread stack to trace locking (disabled by default, use VBOX_WITH_LOCK_VALIDATOR, but that WILL FAIL presently)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 KB
Line 
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 */
48template<typename T>
49class ObjectsList
50{
51public:
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()
62 { }
63
64 ~ObjectsList()
65 {
66 uninitAll();
67 }
68
69private:
70 // prohibit copying and assignment
71 ObjectsList(const ObjectsList &d);
72 ObjectsList& operator=(const ObjectsList &d);
73
74public:
75
76 /**
77 * Returns the lock handle which protects this list, for use with
78 * AutoReadLock or AutoWriteLock.
79 */
80 RWLockHandle& getLockHandle()
81 {
82 return m_lock;
83 }
84
85 /**
86 * Calls m_ll.push_back(p) with locking.
87 * @param p
88 */
89 void addChild(MyType p)
90 {
91 AutoWriteLock al(m_lock COMMA_LOCKVAL_SRC_POS);
92 m_ll.push_back(p);
93 }
94
95 /**
96 * Calls m_ll.remove(p) with locking. Does NOT call uninit()
97 * on the contained object.
98 * @param p
99 */
100 void removeChild(MyType p)
101 {
102 AutoWriteLock al(m_lock COMMA_LOCKVAL_SRC_POS);
103 m_ll.remove(p);
104 }
105
106 /**
107 * Appends all objects from another list to the member list.
108 * Locks the other list for reading but does not lock "this"
109 * (because it might be on the caller's stack and needs no
110 * locking).
111 * @param ll
112 */
113 void appendOtherList(ObjectsList<T> &ll)
114 {
115 AutoReadLock alr(ll.getLockHandle() COMMA_LOCKVAL_SRC_POS);
116 for (const_iterator it = ll.begin();
117 it != ll.end();
118 ++it)
119 {
120 m_ll.push_back(*it);
121 }
122 }
123
124 /**
125 * Calls uninit() on every COM object on the list and then
126 * clears the list, with locking.
127 */
128 void uninitAll()
129 {
130 AutoWriteLock al(m_lock COMMA_LOCKVAL_SRC_POS);
131 for (iterator it = m_ll.begin();
132 it != m_ll.end();
133 ++it)
134 {
135 MyType &q = *it;
136 q->uninit();
137 }
138 m_ll.clear();
139 }
140
141 /**
142 * Returns the no. of objects on the list (std::list compatibility)
143 * with locking.
144 */
145 size_t size()
146 {
147 AutoReadLock al(m_lock COMMA_LOCKVAL_SRC_POS);
148 return m_ll.size();
149 }
150
151 /**
152 * Returns a raw pointer to the member list of objects.
153 * Does not lock!
154 * @return
155 */
156 MyList& getList()
157 {
158 return m_ll;
159 }
160
161 /**
162 * Returns the first object on the list (std::list compatibility)
163 * with locking.
164 */
165 MyType front()
166 {
167 AutoReadLock al(m_lock COMMA_LOCKVAL_SRC_POS);
168 return m_ll.front();
169 }
170
171 /**
172 * Returns the begin iterator from the list (std::list compatibility).
173 * Does not lock!
174 * @return
175 */
176 iterator begin()
177 {
178 return m_ll.begin();
179 }
180
181 /**
182 * Returns the end iterator from the list (std::list compatibility).
183 * Does not lock!
184 */
185 iterator end()
186 {
187 return m_ll.end();
188 }
189
190 void insert(iterator it, MyType &p)
191 {
192 m_ll.insert(it, p);
193 }
194
195 void erase(iterator it)
196 {
197 m_ll.erase(it);
198 }
199
200private:
201 MyList m_ll;
202 RWLockHandle m_lock;
203};
204
205#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette