VirtualBox

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

Last change on this file was 106061, checked in by vboxsync, 6 weeks 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
RevLine 
[36655]1/* $Id: list.h 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * MS COM / XPCOM Abstraction Layer - List classes declaration.
4 */
5
6/*
[106061]7 * Copyright (C) 2011-2024 Oracle and/or its affiliates.
[36655]8 *
[96407]9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
[36655]11 *
[96407]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 *
[36655]25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
[96407]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
[36655]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.
[96407]33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
[36655]35 */
36
[76558]37#ifndef VBOX_INCLUDED_com_list_h
38#define VBOX_INCLUDED_com_list_h
[76507]39#ifndef RT_WITHOUT_PRAGMA_ONCE
40# pragma once
41#endif
[36655]42
[37068]43#include <VBox/com/ptr.h>
[36655]44#include <VBox/com/string.h>
[45520]45#include <VBox/com/array.h>
[36655]46#include <iprt/cpp/list.h>
47
[45520]48
[58110]49/** @defgroup grp_com_list List Classes
50 * @ingroup grp_com
51 * @{
52 */
53
[36655]54/**
[37068]55 * Specialized list class for using with com::ComPtr<C>
56 *
[58106]57 * @note This is necessary cause ComPtr<IFACE> has a size of 8.
[37068]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 *
[58106]74 * @param cCapacity The initial capacity the list has.
[37068]75 * @throws std::bad_alloc
76 */
[45520]77 RTCList(size_t cCapacity = BASE::kDefaultCapacity)
78 : BASE(cCapacity) {}
[37068]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 *
[58106]104 * @param cCapacity The initial capacity the list has.
[37068]105 * @throws std::bad_alloc
106 */
[45520]107 RTCList(size_t cCapacity = BASE::kDefaultCapacity)
108 : BASE(cCapacity) {}
[37068]109
110 /* Define our own new and delete. */
111 RTMEMEF_NEW_AND_DELETE_OPERATORS();
112};
113
114/**
[36655]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 <>
[45520]120class RTCList<com::Utf8Str>: public RTCListBase<com::Utf8Str, com::Utf8Str*, false>
[36655]121{
122 /* Traits */
[45520]123 typedef com::Utf8Str T;
[36655]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 *
[58106]134 * @param cCapacity The initial capacity the list has.
[36655]135 * @throws std::bad_alloc
136 */
[45520]137 RTCList(size_t cCapacity = BASE::kDefaultCapacity)
138 : BASE(cCapacity) {}
[36655]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));
[45520]153 size_t const cElementsOther = sfaOther.size();
154 resizeArray(cElementsOther);
155 m_cElements = cElementsOther;
156 for (size_t i = 0; i < cElementsOther; ++i)
[36655]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)
[45520]171 : BASE(other.size())
[36655]172 {
[45520]173 for (size_t i = 0; i < m_cElements; ++i)
[36655]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();
[45520]188
[36655]189 /* Values cleanup */
[45520]190 RTCListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cElements);
191
[36655]192 /* Copy */
[45520]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)
[36655]198 RTCListHelper<T, ITYPE>::set(m_pArray, i, T(other[i]));
[45520]199
[36655]200 m_guard.leaveWrite();
201 return *this;
202 }
203
[36656]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 {
[36659]213 return *reinterpret_cast<RTCList<RTCString, RTCString*> *>(this);
[36656]214 }
215
[36655]216 /* Define our own new and delete. */
217 RTMEMEF_NEW_AND_DELETE_OPERATORS();
218};
219
[58110]220/** @} */
221
[76585]222#endif /* !VBOX_INCLUDED_com_list_h */
[36655]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