1 | /* $Id: mtlist.h 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * MS COM / XPCOM Abstraction Layer - Thread-safe list classes declaration.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 | #ifndef VBOX_INCLUDED_com_mtlist_h
|
---|
38 | #define VBOX_INCLUDED_com_mtlist_h
|
---|
39 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
40 | # pragma once
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | #include <VBox/com/ptr.h>
|
---|
44 | #include <VBox/com/string.h>
|
---|
45 | #include <VBox/com/array.h>
|
---|
46 | #include <iprt/cpp/mtlist.h>
|
---|
47 |
|
---|
48 |
|
---|
49 | /** @defgroup grp_com_mtlist Thread-safe List Classes
|
---|
50 | * @ingroup grp_com
|
---|
51 | * @{
|
---|
52 | */
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Specialized thread-safe list class for using with com::ComPtr<C>
|
---|
56 | *
|
---|
57 | * @note: This is necessary cause ComPtr<IFACE> has a size of 8.
|
---|
58 | */
|
---|
59 | template <typename C>
|
---|
60 | class RTCMTList< ComPtr<C> >: public RTCListBase< ComPtr<C>, ComPtr<C>*, true>
|
---|
61 | {
|
---|
62 | /* Traits */
|
---|
63 | typedef ComPtr<C> T;
|
---|
64 | typedef T *ITYPE;
|
---|
65 | static const bool MT = true;
|
---|
66 | typedef RTCListBase<T, ITYPE, MT> BASE;
|
---|
67 |
|
---|
68 | public:
|
---|
69 | /**
|
---|
70 | * Creates a new list.
|
---|
71 | *
|
---|
72 | * This preallocates @a cCapacity elements within the list.
|
---|
73 | *
|
---|
74 | * @param cCapacity The initial capacity the list has.
|
---|
75 | * @throws std::bad_alloc
|
---|
76 | */
|
---|
77 | RTCMTList(size_t cCapacity = BASE::kDefaultCapacity)
|
---|
78 | : BASE(cCapacity) {}
|
---|
79 |
|
---|
80 | /* Define our own new and delete. */
|
---|
81 | RTMEMEF_NEW_AND_DELETE_OPERATORS();
|
---|
82 | };
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Specialized thread-safe list class for using with com::ComObjPtr<C>
|
---|
86 | *
|
---|
87 | * @note: This is necessary cause ComObjPtr<IFACE> has a size of 8.
|
---|
88 | */
|
---|
89 | template <typename C>
|
---|
90 | class RTCMTList< ComObjPtr<C> >: public RTCListBase< ComObjPtr<C>, ComObjPtr<C>*, true>
|
---|
91 | {
|
---|
92 | /* Traits */
|
---|
93 | typedef ComObjPtr<C> T;
|
---|
94 | typedef T *ITYPE;
|
---|
95 | static const bool MT = true;
|
---|
96 | typedef RTCListBase<T, ITYPE, MT> BASE;
|
---|
97 |
|
---|
98 | public:
|
---|
99 | /**
|
---|
100 | * Creates a new list.
|
---|
101 | *
|
---|
102 | * This preallocates @a cCapacity elements within the list.
|
---|
103 | *
|
---|
104 | * @param cCapacity The initial capacity the list has.
|
---|
105 | * @throws std::bad_alloc
|
---|
106 | */
|
---|
107 | RTCMTList(size_t cCapacity = BASE::kDefaultCapacity)
|
---|
108 | : BASE(cCapacity) {}
|
---|
109 |
|
---|
110 | /* Define our own new and delete. */
|
---|
111 | RTMEMEF_NEW_AND_DELETE_OPERATORS();
|
---|
112 | };
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Specialized thread-safe list class for using with com::Utf8Str.
|
---|
116 | *
|
---|
117 | * The class offers methods for importing com::SafeArray's of com::Bstr's.
|
---|
118 | */
|
---|
119 | template <>
|
---|
120 | class RTCMTList<com::Utf8Str>: public RTCListBase<com::Utf8Str, com::Utf8Str *, true>
|
---|
121 | {
|
---|
122 | /* Traits */
|
---|
123 | typedef com::Utf8Str T;
|
---|
124 | typedef T *ITYPE;
|
---|
125 | static const bool MT = true;
|
---|
126 | typedef RTCListBase<T, ITYPE, MT> BASE;
|
---|
127 |
|
---|
128 | public:
|
---|
129 | /**
|
---|
130 | * Creates a new list.
|
---|
131 | *
|
---|
132 | * This preallocates @a cCapacity elements within the list.
|
---|
133 | *
|
---|
134 | * @param cCapacity The initial capacity the list has.
|
---|
135 | * @throws std::bad_alloc
|
---|
136 | */
|
---|
137 | RTCMTList(size_t cCapacity = BASE::kDefaultCapacity)
|
---|
138 | : BASE(cCapacity) {}
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Creates a copy of another list.
|
---|
142 | *
|
---|
143 | * The other list will be fully copied and the capacity will be the same as
|
---|
144 | * the size of the other list. The com::Bstr's are silently converted to
|
---|
145 | * com::Utf8Str's.
|
---|
146 | *
|
---|
147 | * @param other The list to copy.
|
---|
148 | * @throws std::bad_alloc
|
---|
149 | */
|
---|
150 | RTCMTList(ComSafeArrayIn(IN_BSTR, other))
|
---|
151 | {
|
---|
152 | com::SafeArray<IN_BSTR> sfaOther(ComSafeArrayInArg(other));
|
---|
153 | size_t const cElementsOther = sfaOther.size();
|
---|
154 | resizeArray(cElementsOther);
|
---|
155 | m_cElements = cElementsOther;
|
---|
156 | for (size_t i = 0; i < cElementsOther; ++i)
|
---|
157 | RTCListHelper<T, ITYPE>::set(m_pArray, i, T(sfaOther[i]));
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Creates a copy of another list.
|
---|
162 | *
|
---|
163 | * The other list will be fully copied and the capacity will be the same as
|
---|
164 | * the size of the other list. The com::Bstr's are silently converted to
|
---|
165 | * com::Utf8Str's.
|
---|
166 | *
|
---|
167 | * @param other The list to copy.
|
---|
168 | * @throws std::bad_alloc
|
---|
169 | */
|
---|
170 | RTCMTList(const com::SafeArray<IN_BSTR> &other)
|
---|
171 | : BASE(other.size())
|
---|
172 | {
|
---|
173 | for (size_t i = 0; i < m_cElements; ++i)
|
---|
174 | RTCListHelper<T, ITYPE>::set(m_pArray, i, T(other[i]));
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Copy the items of the other list into this list. All previous items of
|
---|
179 | * this list are deleted.
|
---|
180 | *
|
---|
181 | * @param other The list to copy.
|
---|
182 | * @return a reference to this list.
|
---|
183 | * @throws std::bad_alloc
|
---|
184 | */
|
---|
185 | RTCListBase<T, ITYPE, MT> &operator=(const com::SafeArray<IN_BSTR> &other)
|
---|
186 | {
|
---|
187 | m_guard.enterWrite();
|
---|
188 | /* Values cleanup */
|
---|
189 | RTCListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cElements);
|
---|
190 | /* Copy */
|
---|
191 | if (other.size() != m_cCapacity)
|
---|
192 | resizeArrayNoErase(other.size());
|
---|
193 | m_cElements = other.size();
|
---|
194 | for (size_t i = 0; i < other.size(); ++i)
|
---|
195 | RTCListHelper<T, ITYPE>::set(m_pArray, i, T(other[i]));
|
---|
196 | m_guard.leaveWrite();
|
---|
197 |
|
---|
198 | return *this;
|
---|
199 | }
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Implicit conversion to a RTCString list.
|
---|
203 | *
|
---|
204 | * This allows the usage of the RTCString::join method with this list type.
|
---|
205 | *
|
---|
206 | * @return a converted const reference to this list.
|
---|
207 | */
|
---|
208 | operator const RTCMTList<RTCString, RTCString*>&()
|
---|
209 | {
|
---|
210 | return *reinterpret_cast<RTCMTList<RTCString, RTCString*> *>(this);
|
---|
211 | }
|
---|
212 |
|
---|
213 | /* Define our own new and delete. */
|
---|
214 | RTMEMEF_NEW_AND_DELETE_OPERATORS();
|
---|
215 | };
|
---|
216 |
|
---|
217 | /** @} */
|
---|
218 |
|
---|
219 | #endif /* !VBOX_INCLUDED_com_mtlist_h */
|
---|
220 |
|
---|