VirtualBox

source: vbox/trunk/include/VBox/com/mtlist.h@ 49165

Last change on this file since 49165 was 45520, checked in by vboxsync, 12 years ago

iprt/cpp/list.h,iprt/cpp/mtlist.h: Added assertions for index out of range conditions and tried to force those cases to have predictable non-destructive behavior. (Not possible when indexing an empty list, unfortunately.) Fixed RTMemRealloc bugs. Added missing read locking. Also changed the testcase to not wait forever in the multithreaded test2() function.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.1 KB
Line 
1/* $Id: mtlist.h 45520 2013-04-12 14:22:41Z vboxsync $ */
2/** @file
3 * MS COM / XPCOM Abstraction Layer - Thread-safe list classes declaration.
4 */
5
6/*
7 * Copyright (C) 2011-2013 Oracle Corporation
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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27#ifndef ___VBox_com_mtlist_h
28#define ___VBox_com_mtlist_h
29
30#include <VBox/com/ptr.h>
31#include <VBox/com/string.h>
32#include <VBox/com/array.h>
33#include <iprt/cpp/mtlist.h>
34
35/**
36 * Specialized thread-safe list class for using with com::ComPtr<C>
37 *
38 * @note: This is necessary cause ComPtr<IFACE> has a size of 8.
39 */
40template <typename C>
41class RTCMTList< ComPtr<C> >: public RTCListBase< ComPtr<C>, ComPtr<C>*, true>
42{
43 /* Traits */
44 typedef ComPtr<C> T;
45 typedef T *ITYPE;
46 static const bool MT = true;
47 typedef RTCListBase<T, ITYPE, MT> BASE;
48
49public:
50 /**
51 * Creates a new list.
52 *
53 * This preallocates @a cCapacity elements within the list.
54 *
55 * @param cCapacitiy The initial capacity the list has.
56 * @throws std::bad_alloc
57 */
58 RTCMTList(size_t cCapacity = BASE::kDefaultCapacity)
59 : BASE(cCapacity) {}
60
61 /* Define our own new and delete. */
62 RTMEMEF_NEW_AND_DELETE_OPERATORS();
63};
64
65/**
66 * Specialized thread-safe list class for using with com::ComObjPtr<C>
67 *
68 * @note: This is necessary cause ComObjPtr<IFACE> has a size of 8.
69 */
70template <typename C>
71class RTCMTList< ComObjPtr<C> >: public RTCListBase< ComObjPtr<C>, ComObjPtr<C>*, true>
72{
73 /* Traits */
74 typedef ComObjPtr<C> T;
75 typedef T *ITYPE;
76 static const bool MT = true;
77 typedef RTCListBase<T, ITYPE, MT> BASE;
78
79public:
80 /**
81 * Creates a new list.
82 *
83 * This preallocates @a cCapacity elements within the list.
84 *
85 * @param cCapacitiy The initial capacity the list has.
86 * @throws std::bad_alloc
87 */
88 RTCMTList(size_t cCapacity = BASE::kDefaultCapacity)
89 : BASE(cCapacity) {}
90
91 /* Define our own new and delete. */
92 RTMEMEF_NEW_AND_DELETE_OPERATORS();
93};
94
95/**
96 * Specialized thread-safe list class for using with com::Utf8Str.
97 *
98 * The class offers methods for importing com::SafeArray's of com::Bstr's.
99 */
100template <>
101class RTCMTList<com::Utf8Str>: public RTCListBase<com::Utf8Str, com::Utf8Str *, true>
102{
103 /* Traits */
104 typedef com::Utf8Str T;
105 typedef T *ITYPE;
106 static const bool MT = true;
107 typedef RTCListBase<T, ITYPE, MT> BASE;
108
109public:
110 /**
111 * Creates a new list.
112 *
113 * This preallocates @a cCapacity elements within the list.
114 *
115 * @param cCapacitiy The initial capacity the list has.
116 * @throws std::bad_alloc
117 */
118 RTCMTList(size_t cCapacity = BASE::kDefaultCapacity)
119 : BASE(cCapacity) {}
120
121 /**
122 * Creates a copy of another list.
123 *
124 * The other list will be fully copied and the capacity will be the same as
125 * the size of the other list. The com::Bstr's are silently converted to
126 * com::Utf8Str's.
127 *
128 * @param other The list to copy.
129 * @throws std::bad_alloc
130 */
131 RTCMTList(ComSafeArrayIn(IN_BSTR, other))
132 {
133 com::SafeArray<IN_BSTR> sfaOther(ComSafeArrayInArg(other));
134 size_t const cElementsOther = sfaOther.size();
135 resizeArray(cElementsOther);
136 m_cElements = cElementsOther;
137 for (size_t i = 0; i < cElementsOther; ++i)
138 RTCListHelper<T, ITYPE>::set(m_pArray, i, T(sfaOther[i]));
139 }
140
141 /**
142 * Creates a copy of another list.
143 *
144 * The other list will be fully copied and the capacity will be the same as
145 * the size of the other list. The com::Bstr's are silently converted to
146 * com::Utf8Str's.
147 *
148 * @param other The list to copy.
149 * @throws std::bad_alloc
150 */
151 RTCMTList(const com::SafeArray<IN_BSTR> &other)
152 : BASE(other.size())
153 {
154 for (size_t i = 0; i < m_cElements; ++i)
155 RTCListHelper<T, ITYPE>::set(m_pArray, i, T(other[i]));
156 }
157
158 /**
159 * Copy the items of the other list into this list. All previous items of
160 * this list are deleted.
161 *
162 * @param other The list to copy.
163 * @return a reference to this list.
164 * @throws std::bad_alloc
165 */
166 RTCListBase<T, ITYPE, MT> &operator=(const com::SafeArray<IN_BSTR> &other)
167 {
168 m_guard.enterWrite();
169 /* Values cleanup */
170 RTCListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cElements);
171 /* Copy */
172 if (other.size() != m_cCapacity)
173 resizeArrayNoErase(other.size());
174 m_cElements = other.size();
175 for (size_t i = 0; i < other.size(); ++i)
176 RTCListHelper<T, ITYPE>::set(m_pArray, i, T(other[i]));
177 m_guard.leaveWrite();
178
179 return *this;
180 }
181
182 /**
183 * Implicit conversion to a RTCString list.
184 *
185 * This allows the usage of the RTCString::join method with this list type.
186 *
187 * @return a converted const reference to this list.
188 */
189 operator const RTCMTList<RTCString, RTCString*>&()
190 {
191 return *reinterpret_cast<RTCMTList<RTCString, RTCString*> *>(this);
192 }
193
194 /* Define our own new and delete. */
195 RTMEMEF_NEW_AND_DELETE_OPERATORS();
196};
197
198#endif /* !___VBox_com_mtlist_h */
199
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