1 | /* $Id: mtlist.h 76507 2018-12-30 03:43:09Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * MS COM / XPCOM Abstraction Layer - Thread-safe list classes declaration.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2017 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 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
30 | # pragma once
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | #include <VBox/com/ptr.h>
|
---|
34 | #include <VBox/com/string.h>
|
---|
35 | #include <VBox/com/array.h>
|
---|
36 | #include <iprt/cpp/mtlist.h>
|
---|
37 |
|
---|
38 |
|
---|
39 | /** @defgroup grp_com_mtlist Thread-safe List Classes
|
---|
40 | * @ingroup grp_com
|
---|
41 | * @{
|
---|
42 | */
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Specialized thread-safe list class for using with com::ComPtr<C>
|
---|
46 | *
|
---|
47 | * @note: This is necessary cause ComPtr<IFACE> has a size of 8.
|
---|
48 | */
|
---|
49 | template <typename C>
|
---|
50 | class RTCMTList< ComPtr<C> >: public RTCListBase< ComPtr<C>, ComPtr<C>*, true>
|
---|
51 | {
|
---|
52 | /* Traits */
|
---|
53 | typedef ComPtr<C> T;
|
---|
54 | typedef T *ITYPE;
|
---|
55 | static const bool MT = true;
|
---|
56 | typedef RTCListBase<T, ITYPE, MT> BASE;
|
---|
57 |
|
---|
58 | public:
|
---|
59 | /**
|
---|
60 | * Creates a new list.
|
---|
61 | *
|
---|
62 | * This preallocates @a cCapacity elements within the list.
|
---|
63 | *
|
---|
64 | * @param cCapacity The initial capacity the list has.
|
---|
65 | * @throws std::bad_alloc
|
---|
66 | */
|
---|
67 | RTCMTList(size_t cCapacity = BASE::kDefaultCapacity)
|
---|
68 | : BASE(cCapacity) {}
|
---|
69 |
|
---|
70 | /* Define our own new and delete. */
|
---|
71 | RTMEMEF_NEW_AND_DELETE_OPERATORS();
|
---|
72 | };
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Specialized thread-safe list class for using with com::ComObjPtr<C>
|
---|
76 | *
|
---|
77 | * @note: This is necessary cause ComObjPtr<IFACE> has a size of 8.
|
---|
78 | */
|
---|
79 | template <typename C>
|
---|
80 | class RTCMTList< ComObjPtr<C> >: public RTCListBase< ComObjPtr<C>, ComObjPtr<C>*, true>
|
---|
81 | {
|
---|
82 | /* Traits */
|
---|
83 | typedef ComObjPtr<C> T;
|
---|
84 | typedef T *ITYPE;
|
---|
85 | static const bool MT = true;
|
---|
86 | typedef RTCListBase<T, ITYPE, MT> BASE;
|
---|
87 |
|
---|
88 | public:
|
---|
89 | /**
|
---|
90 | * Creates a new list.
|
---|
91 | *
|
---|
92 | * This preallocates @a cCapacity elements within the list.
|
---|
93 | *
|
---|
94 | * @param cCapacity The initial capacity the list has.
|
---|
95 | * @throws std::bad_alloc
|
---|
96 | */
|
---|
97 | RTCMTList(size_t cCapacity = BASE::kDefaultCapacity)
|
---|
98 | : BASE(cCapacity) {}
|
---|
99 |
|
---|
100 | /* Define our own new and delete. */
|
---|
101 | RTMEMEF_NEW_AND_DELETE_OPERATORS();
|
---|
102 | };
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Specialized thread-safe list class for using with com::Utf8Str.
|
---|
106 | *
|
---|
107 | * The class offers methods for importing com::SafeArray's of com::Bstr's.
|
---|
108 | */
|
---|
109 | template <>
|
---|
110 | class RTCMTList<com::Utf8Str>: public RTCListBase<com::Utf8Str, com::Utf8Str *, true>
|
---|
111 | {
|
---|
112 | /* Traits */
|
---|
113 | typedef com::Utf8Str T;
|
---|
114 | typedef T *ITYPE;
|
---|
115 | static const bool MT = true;
|
---|
116 | typedef RTCListBase<T, ITYPE, MT> BASE;
|
---|
117 |
|
---|
118 | public:
|
---|
119 | /**
|
---|
120 | * Creates a new list.
|
---|
121 | *
|
---|
122 | * This preallocates @a cCapacity elements within the list.
|
---|
123 | *
|
---|
124 | * @param cCapacity The initial capacity the list has.
|
---|
125 | * @throws std::bad_alloc
|
---|
126 | */
|
---|
127 | RTCMTList(size_t cCapacity = BASE::kDefaultCapacity)
|
---|
128 | : BASE(cCapacity) {}
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Creates a copy of another list.
|
---|
132 | *
|
---|
133 | * The other list will be fully copied and the capacity will be the same as
|
---|
134 | * the size of the other list. The com::Bstr's are silently converted to
|
---|
135 | * com::Utf8Str's.
|
---|
136 | *
|
---|
137 | * @param other The list to copy.
|
---|
138 | * @throws std::bad_alloc
|
---|
139 | */
|
---|
140 | RTCMTList(ComSafeArrayIn(IN_BSTR, other))
|
---|
141 | {
|
---|
142 | com::SafeArray<IN_BSTR> sfaOther(ComSafeArrayInArg(other));
|
---|
143 | size_t const cElementsOther = sfaOther.size();
|
---|
144 | resizeArray(cElementsOther);
|
---|
145 | m_cElements = cElementsOther;
|
---|
146 | for (size_t i = 0; i < cElementsOther; ++i)
|
---|
147 | RTCListHelper<T, ITYPE>::set(m_pArray, i, T(sfaOther[i]));
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Creates a copy of another list.
|
---|
152 | *
|
---|
153 | * The other list will be fully copied and the capacity will be the same as
|
---|
154 | * the size of the other list. The com::Bstr's are silently converted to
|
---|
155 | * com::Utf8Str's.
|
---|
156 | *
|
---|
157 | * @param other The list to copy.
|
---|
158 | * @throws std::bad_alloc
|
---|
159 | */
|
---|
160 | RTCMTList(const com::SafeArray<IN_BSTR> &other)
|
---|
161 | : BASE(other.size())
|
---|
162 | {
|
---|
163 | for (size_t i = 0; i < m_cElements; ++i)
|
---|
164 | RTCListHelper<T, ITYPE>::set(m_pArray, i, T(other[i]));
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Copy the items of the other list into this list. All previous items of
|
---|
169 | * this list are deleted.
|
---|
170 | *
|
---|
171 | * @param other The list to copy.
|
---|
172 | * @return a reference to this list.
|
---|
173 | * @throws std::bad_alloc
|
---|
174 | */
|
---|
175 | RTCListBase<T, ITYPE, MT> &operator=(const com::SafeArray<IN_BSTR> &other)
|
---|
176 | {
|
---|
177 | m_guard.enterWrite();
|
---|
178 | /* Values cleanup */
|
---|
179 | RTCListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cElements);
|
---|
180 | /* Copy */
|
---|
181 | if (other.size() != m_cCapacity)
|
---|
182 | resizeArrayNoErase(other.size());
|
---|
183 | m_cElements = other.size();
|
---|
184 | for (size_t i = 0; i < other.size(); ++i)
|
---|
185 | RTCListHelper<T, ITYPE>::set(m_pArray, i, T(other[i]));
|
---|
186 | m_guard.leaveWrite();
|
---|
187 |
|
---|
188 | return *this;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Implicit conversion to a RTCString list.
|
---|
193 | *
|
---|
194 | * This allows the usage of the RTCString::join method with this list type.
|
---|
195 | *
|
---|
196 | * @return a converted const reference to this list.
|
---|
197 | */
|
---|
198 | operator const RTCMTList<RTCString, RTCString*>&()
|
---|
199 | {
|
---|
200 | return *reinterpret_cast<RTCMTList<RTCString, RTCString*> *>(this);
|
---|
201 | }
|
---|
202 |
|
---|
203 | /* Define our own new and delete. */
|
---|
204 | RTMEMEF_NEW_AND_DELETE_OPERATORS();
|
---|
205 | };
|
---|
206 |
|
---|
207 | /** @} */
|
---|
208 |
|
---|
209 | #endif /* !___VBox_com_mtlist_h */
|
---|
210 |
|
---|