VirtualBox

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

Last change on this file since 96407 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

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