1 | /* $Id: list.h 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * MS COM / XPCOM Abstraction Layer - List classes declaration.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2020 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_INCLUDED_com_list_h
|
---|
28 | #define VBOX_INCLUDED_com_list_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/list.h>
|
---|
37 |
|
---|
38 |
|
---|
39 | /** @defgroup grp_com_list List Classes
|
---|
40 | * @ingroup grp_com
|
---|
41 | * @{
|
---|
42 | */
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Specialized 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 RTCList< ComPtr<C> >: public RTCListBase< ComPtr<C>, ComPtr<C>*, false>
|
---|
51 | {
|
---|
52 | /* Traits */
|
---|
53 | typedef ComPtr<C> T;
|
---|
54 | typedef T *ITYPE;
|
---|
55 | static const bool MT = false;
|
---|
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 | RTCList(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 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 RTCList< ComObjPtr<C> >: public RTCListBase< ComObjPtr<C>, ComObjPtr<C>*, false>
|
---|
81 | {
|
---|
82 | /* Traits */
|
---|
83 | typedef ComObjPtr<C> T;
|
---|
84 | typedef T *ITYPE;
|
---|
85 | static const bool MT = false;
|
---|
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 | RTCList(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 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 RTCList<com::Utf8Str>: public RTCListBase<com::Utf8Str, com::Utf8Str*, false>
|
---|
111 | {
|
---|
112 | /* Traits */
|
---|
113 | typedef com::Utf8Str T;
|
---|
114 | typedef T *ITYPE;
|
---|
115 | static const bool MT = false;
|
---|
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 | RTCList(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 | RTCList(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 | RTCList(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 |
|
---|
179 | /* Values cleanup */
|
---|
180 | RTCListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cElements);
|
---|
181 |
|
---|
182 | /* Copy */
|
---|
183 | size_t cElementsOther = other.size();
|
---|
184 | if (cElementsOther != m_cCapacity)
|
---|
185 | resizeArrayNoErase(cElementsOther);
|
---|
186 | m_cElements = cElementsOther;
|
---|
187 | for (size_t i = 0; i < cElementsOther; ++i)
|
---|
188 | RTCListHelper<T, ITYPE>::set(m_pArray, i, T(other[i]));
|
---|
189 |
|
---|
190 | m_guard.leaveWrite();
|
---|
191 | return *this;
|
---|
192 | }
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * Implicit conversion to a RTCString list.
|
---|
196 | *
|
---|
197 | * This allows the usage of the RTCString::join method with this list type.
|
---|
198 | *
|
---|
199 | * @return a converted const reference to this list.
|
---|
200 | */
|
---|
201 | operator const RTCList<RTCString, RTCString*>&()
|
---|
202 | {
|
---|
203 | return *reinterpret_cast<RTCList<RTCString, RTCString*> *>(this);
|
---|
204 | }
|
---|
205 |
|
---|
206 | /* Define our own new and delete. */
|
---|
207 | RTMEMEF_NEW_AND_DELETE_OPERATORS();
|
---|
208 | };
|
---|
209 |
|
---|
210 | /** @} */
|
---|
211 |
|
---|
212 | #endif /* !VBOX_INCLUDED_com_list_h */
|
---|
213 |
|
---|