VirtualBox

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

Last change on this file since 94249 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

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