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