VirtualBox

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

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

Main: bring back r55600 with fix for broken machine creation

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

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