VirtualBox

source: vbox/trunk/src/VBox/Main/include/Wrapper.h@ 53517

Last change on this file since 53517 was 50416, checked in by vboxsync, 11 years ago

Main: fix the remaining NULL safearray issues, the template had useless assertions and the helper code used by the wrappers also needed to check for it everywhere

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.6 KB
Line 
1/* $Id: Wrapper.h 50416 2014-02-11 18:00:45Z vboxsync $ */
2/** @file
3 * VirtualBox COM: API wrapper helpers
4 */
5
6/*
7 * Copyright (C) 2012-2014 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
18#ifndef ____H_WRAPPER
19#define ____H_WRAPPER
20
21#include <vector>
22#include <VBox/com/ptr.h>
23#include <VBox/com/array.h>
24
25#include "AutoCaller.h"
26
27
28/**
29 * Checks that the given pointer to an output argument is valid and throws
30 * E_POINTER + extended error info otherwise.
31 * @param arg Pointer argument.
32 */
33#define CheckComArgOutPointerValidThrow(arg) \
34 do { \
35 if (RT_UNLIKELY(!VALID_PTR(arg))) \
36 throw setError(E_POINTER, \
37 tr("Output argument %s points to invalid memory location (%p)"), \
38 #arg, (void *)(arg)); \
39 } while (0)
40
41
42class BSTROutConverter
43{
44public:
45 BSTROutConverter() : mDst(NULL)
46 {
47 }
48
49 BSTROutConverter(BSTR *aDst) : mDst(aDst)
50 {
51 }
52
53 ~BSTROutConverter()
54 {
55 if (mDst)
56 Bstr(mStr).detachTo(mDst);
57 }
58
59 com::Utf8Str &str()
60 {
61 return mStr;
62 }
63
64private:
65 com::Utf8Str mStr;
66 BSTR *mDst;
67};
68
69class BSTRInConverter
70{
71public:
72 BSTRInConverter() : mSrc()
73 {
74 }
75
76 BSTRInConverter(CBSTR aSrc) : mSrc(aSrc)
77 {
78 }
79
80 ~BSTRInConverter()
81 {
82 }
83
84 const com::Utf8Str &str()
85 {
86 return mSrc;
87 }
88
89private:
90 const com::Utf8Str mSrc;
91};
92
93class ArrayBSTROutConverter
94{
95public:
96 ArrayBSTROutConverter() :
97#ifdef VBOX_WITH_XPCOM
98 mDstSize(NULL),
99 mDst(NULL)
100#else // !VBOX_WITH_XPCOM
101 mDst(NULL)
102#endif // !VBOX_WITH_XPCOM
103 {
104 }
105
106 ArrayBSTROutConverter(ComSafeArrayOut(BSTR, aDst)) :
107#ifdef VBOX_WITH_XPCOM
108 mDstSize(aDstSize),
109 mDst(aDst)
110#else // !VBOX_WITH_XPCOM
111 mDst(aDst)
112#endif // !VBOX_WITH_XPCOM
113 {
114 }
115
116 ~ArrayBSTROutConverter()
117 {
118 if (mDst)
119 {
120 com::SafeArray<BSTR> outArray(mArray.size());
121 for (size_t i = 0; i < mArray.size(); i++)
122 Bstr(mArray[i]).detachTo(&outArray[i]);
123 outArray.detachTo(ComSafeArrayOutArg(mDst));
124 }
125 }
126
127 std::vector<com::Utf8Str> &array()
128 {
129 return mArray;
130 }
131
132private:
133 std::vector<com::Utf8Str> mArray;
134#ifdef VBOX_WITH_XPCOM
135 PRUint32 *mDstSize;
136 BSTR **mDst;
137#else // !VBOX_WITH_XPCOM
138 SAFEARRAY **mDst;
139#endif // !VBOX_WITH_XPCOM
140};
141
142class ArrayBSTRInConverter
143{
144public:
145 ArrayBSTRInConverter()
146 {
147 }
148
149 ArrayBSTRInConverter(ComSafeArrayIn(IN_BSTR, aSrc))
150 {
151 if (!ComSafeArrayInIsNull(aSrc))
152 {
153 com::SafeArray<IN_BSTR> inArray(ComSafeArrayInArg(aSrc));
154 mArray.resize(inArray.size());
155 for (size_t i = 0; i < inArray.size(); i++)
156 mArray[i] = inArray[i];
157 }
158 }
159
160 ~ArrayBSTRInConverter()
161 {
162 }
163
164 const std::vector<com::Utf8Str> &array()
165 {
166 return mArray;
167 }
168
169private:
170 std::vector<com::Utf8Str> mArray;
171};
172
173class UuidOutConverter
174{
175public:
176 UuidOutConverter() : mDst(NULL)
177 {
178 }
179
180 UuidOutConverter(BSTR *aDst) : mDst(aDst)
181 {
182 }
183
184 ~UuidOutConverter()
185 {
186 if (mDst)
187 mUuid.toUtf16().detachTo(mDst);
188 }
189
190 com::Guid &uuid()
191 {
192 return mUuid;
193 }
194
195private:
196 com::Guid mUuid;
197 BSTR *mDst;
198};
199
200class UuidInConverter
201{
202public:
203 UuidInConverter() : mSrc()
204 {
205 }
206
207 UuidInConverter(CBSTR aSrc) : mSrc(aSrc)
208 {
209 }
210
211 ~UuidInConverter()
212 {
213 }
214
215 const com::Guid &uuid()
216 {
217 return mSrc;
218 }
219
220private:
221 const com::Guid mSrc;
222};
223
224class ArrayUuidOutConverter
225{
226public:
227 ArrayUuidOutConverter() :
228#ifdef VBOX_WITH_XPCOM
229 mDstSize(NULL),
230 mDst(NULL)
231#else // !VBOX_WITH_XPCOM
232 mDst(NULL)
233#endif // !VBOX_WITH_XPCOM
234 {
235 }
236
237 ArrayUuidOutConverter(ComSafeArrayOut(BSTR, aDst)) :
238#ifdef VBOX_WITH_XPCOM
239 mDstSize(aDstSize),
240 mDst(aDst)
241#else // !VBOX_WITH_XPCOM
242 mDst(aDst)
243#endif // !VBOX_WITH_XPCOM
244 {
245 }
246
247 ~ArrayUuidOutConverter()
248 {
249 if (mDst)
250 {
251 com::SafeArray<BSTR> outArray(mArray.size());
252 for (size_t i = 0; i < mArray.size(); i++)
253 mArray[i].toUtf16().detachTo(&outArray[i]);
254 outArray.detachTo(ComSafeArrayOutArg(mDst));
255 }
256 }
257
258 std::vector<com::Guid> &array()
259 {
260 return mArray;
261 }
262
263private:
264 std::vector<com::Guid> mArray;
265#ifdef VBOX_WITH_XPCOM
266 PRUint32 *mDstSize;
267 BSTR **mDst;
268#else // !VBOX_WITH_XPCOM
269 SAFEARRAY **mDst;
270#endif // !VBOX_WITH_XPCOM
271};
272
273template <class A>
274class ComTypeOutConverter
275{
276public:
277 ComTypeOutConverter() : mDst(NULL)
278 {
279 }
280
281 ComTypeOutConverter(A **aDst) : mDst(aDst)
282 {
283 }
284
285 ~ComTypeOutConverter()
286 {
287 if (mDst)
288 mPtr.queryInterfaceTo(mDst);
289 }
290
291 ComPtr<A> &ptr()
292 {
293 return mPtr;
294 }
295
296private:
297 ComPtr<A> mPtr;
298 A **mDst;
299};
300
301template <class A>
302class ComTypeInConverter
303{
304public:
305 ComTypeInConverter() : mSrc(NULL)
306 {
307 }
308
309 ComTypeInConverter(A *aSrc) : mSrc(aSrc)
310 {
311 }
312
313 ~ComTypeInConverter()
314 {
315 }
316
317 const ComPtr<A> &ptr()
318 {
319 return mSrc;
320 }
321
322private:
323 const ComPtr<A> mSrc;
324};
325
326template <class A>
327class ArrayComTypeOutConverter
328{
329public:
330 ArrayComTypeOutConverter() :
331#ifdef VBOX_WITH_XPCOM
332 mDstSize(NULL),
333 mDst(NULL)
334#else // !VBOX_WITH_XPCOM
335 mDst(NULL)
336#endif // !VBOX_WITH_XPCOM
337 {
338 }
339
340 ArrayComTypeOutConverter(ComSafeArrayOut(A *, aDst)) :
341#ifdef VBOX_WITH_XPCOM
342 mDstSize(aDstSize),
343 mDst(aDst)
344#else // !VBOX_WITH_XPCOM
345 mDst(aDst)
346#endif // !VBOX_WITH_XPCOM
347 {
348 }
349
350 ~ArrayComTypeOutConverter()
351 {
352 if (mDst)
353 {
354 com::SafeIfaceArray<A> outArray(mArray);
355 outArray.detachTo(ComSafeArrayOutArg(mDst));
356 }
357 }
358
359 std::vector<ComPtr<A> > &array()
360 {
361 return mArray;
362 }
363
364private:
365 std::vector<ComPtr<A> > mArray;
366#ifdef VBOX_WITH_XPCOM
367 PRUint32 *mDstSize;
368 A ***mDst;
369#else // !VBOX_WITH_XPCOM
370 SAFEARRAY **mDst;
371#endif // !VBOX_WITH_XPCOM
372};
373
374template <class A>
375class ArrayComTypeInConverter
376{
377public:
378 ArrayComTypeInConverter()
379 {
380 }
381
382 ArrayComTypeInConverter(ComSafeArrayIn(A *, aSrc))
383 {
384 if (!ComSafeArrayInIsNull(aSrc))
385 {
386 com::SafeIfaceArray<A> inArray(ComSafeArrayInArg(aSrc));
387 mArray.resize(inArray.size());
388 for (size_t i = 0; i < inArray.size(); i++)
389 mArray[i] = inArray[i];
390 }
391 }
392
393 ~ArrayComTypeInConverter()
394 {
395 }
396
397 const std::vector<ComPtr<A> > &array()
398 {
399 return mArray;
400 }
401
402private:
403 std::vector<ComPtr<A> > mArray;
404};
405
406template <typename A>
407class ArrayOutConverter
408{
409public:
410 ArrayOutConverter() :
411#ifdef VBOX_WITH_XPCOM
412 mDstSize(NULL),
413 mDst(NULL)
414#else // !VBOX_WITH_XPCOM
415 mDst(NULL)
416#endif // !VBOX_WITH_XPCOM
417 {
418 }
419
420 ArrayOutConverter(ComSafeArrayOut(A, aDst)) :
421#ifdef VBOX_WITH_XPCOM
422 mDstSize(aDstSize),
423 mDst(aDst)
424#else // !VBOX_WITH_XPCOM
425 mDst(aDst)
426#endif // !VBOX_WITH_XPCOM
427 {
428 }
429
430 ~ArrayOutConverter()
431 {
432 if (mDst)
433 {
434 com::SafeArray<A> outArray(mArray.size());
435 for (size_t i = 0; i < mArray.size(); i++)
436 outArray[i] = mArray[i];
437 outArray.detachTo(ComSafeArrayOutArg(mDst));
438 }
439 }
440
441 std::vector<A> &array()
442 {
443 return mArray;
444 }
445
446private:
447 std::vector<A> mArray;
448#ifdef VBOX_WITH_XPCOM
449 PRUint32 *mDstSize;
450 A **mDst;
451#else // !VBOX_WITH_XPCOM
452 SAFEARRAY **mDst;
453#endif // !VBOX_WITH_XPCOM
454};
455
456template <typename A>
457class ArrayInConverter
458{
459public:
460 ArrayInConverter()
461 {
462 }
463
464 ArrayInConverter(ComSafeArrayIn(A, aSrc))
465 {
466 if (!ComSafeArrayInIsNull(aSrc))
467 {
468 com::SafeArray<A> inArray(ComSafeArrayInArg(aSrc));
469 mArray.resize(inArray.size());
470 for (size_t i = 0; i < inArray.size(); i++)
471 mArray[i] = inArray[i];
472 }
473 }
474
475 ~ArrayInConverter()
476 {
477 }
478
479 const std::vector<A> &array()
480 {
481 return mArray;
482 }
483
484private:
485 std::vector<A> mArray;
486};
487
488#endif // !____H_WRAPPER
489/* vi: set tabstop=4 shiftwidth=4 expandtab: */
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