VirtualBox

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

Last change on this file since 41279 was 37861, checked in by vboxsync, 13 years ago

IPRT: fix assigning of empty lists

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