VirtualBox

source: vbox/trunk/include/VBox/com/list.h@ 98651

Last change on this file since 98651 was 98103, checked in by vboxsync, 22 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.5 KB
Line 
1/* $Id: list.h 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * MS COM / XPCOM Abstraction Layer - List classes declaration.
4 */
5
6/*
7 * Copyright (C) 2011-2023 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_list_h
38#define VBOX_INCLUDED_com_list_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/list.h>
47
48
49/** @defgroup grp_com_list List Classes
50 * @ingroup grp_com
51 * @{
52 */
53
54/**
55 * Specialized list class for using with com::ComPtr<C>
56 *
57 * @note This is necessary cause ComPtr<IFACE> has a size of 8.
58 */
59template <typename C>
60class RTCList< ComPtr<C> >: public RTCListBase< ComPtr<C>, ComPtr<C>*, false>
61{
62 /* Traits */
63 typedef ComPtr<C> T;
64 typedef T *ITYPE;
65 static const bool MT = false;
66 typedef RTCListBase<T, ITYPE, MT> BASE;
67
68public:
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 RTCList(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 list class for using with com::ComObjPtr<C>
86 *
87 * @note: This is necessary cause ComObjPtr<IFACE> has a size of 8.
88 */
89template <typename C>
90class RTCList< ComObjPtr<C> >: public RTCListBase< ComObjPtr<C>, ComObjPtr<C>*, false>
91{
92 /* Traits */
93 typedef ComObjPtr<C> T;
94 typedef T *ITYPE;
95 static const bool MT = false;
96 typedef RTCListBase<T, ITYPE, MT> BASE;
97
98public:
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 RTCList(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 list class for using with com::Utf8Str.
116 *
117 * The class offers methods for importing com::SafeArray's of com::Bstr's.
118 */
119template <>
120class RTCList<com::Utf8Str>: public RTCListBase<com::Utf8Str, com::Utf8Str*, false>
121{
122 /* Traits */
123 typedef com::Utf8Str T;
124 typedef T *ITYPE;
125 static const bool MT = false;
126 typedef RTCListBase<T, ITYPE, MT> BASE;
127
128public:
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 RTCList(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 RTCList(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 RTCList(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
189 /* Values cleanup */
190 RTCListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cElements);
191
192 /* Copy */
193 size_t cElementsOther = other.size();
194 if (cElementsOther != m_cCapacity)
195 resizeArrayNoErase(cElementsOther);
196 m_cElements = cElementsOther;
197 for (size_t i = 0; i < cElementsOther; ++i)
198 RTCListHelper<T, ITYPE>::set(m_pArray, i, T(other[i]));
199
200 m_guard.leaveWrite();
201 return *this;
202 }
203
204 /**
205 * Implicit conversion to a RTCString list.
206 *
207 * This allows the usage of the RTCString::join method with this list type.
208 *
209 * @return a converted const reference to this list.
210 */
211 operator const RTCList<RTCString, RTCString*>&()
212 {
213 return *reinterpret_cast<RTCList<RTCString, RTCString*> *>(this);
214 }
215
216 /* Define our own new and delete. */
217 RTMEMEF_NEW_AND_DELETE_OPERATORS();
218};
219
220/** @} */
221
222#endif /* !VBOX_INCLUDED_com_list_h */
223
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