VirtualBox

source: vbox/trunk/include/VBox/com/mtlist.h@ 67180

Last change on this file since 67180 was 62476, checked in by vboxsync, 8 years ago

(C) 2016

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