VirtualBox

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

Last change on this file since 60952 was 55943, checked in by vboxsync, 10 years ago

Wrapper.h: Replaced RT_UNLIKELY with RT_LIKELY to improve our karma wrt MSC.

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