VirtualBox

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

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

Main/idl/apiwrap-server: add support for providing hints to the wrapper generation, we'll sporadically need to get at the AutoCaller object so that we can call uninit().

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.5 KB
Line 
1/* $Id: Wrapper.h 47650 2013-08-09 14:27:21Z vboxsync $ */
2
3/** @file
4 *
5 * VirtualBox COM: API wrapper helpers
6 */
7
8/*
9 * Copyright (C) 2012-2013 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20#ifndef ____H_WRAPPER
21#define ____H_WRAPPER
22
23#include <vector>
24#include <VBox/com/ptr.h>
25#include <VBox/com/array.h>
26
27#include "AutoCaller.h"
28
29
30/**
31 * Checks that the given pointer to an output argument is valid and throws
32 * E_POINTER + extended error info otherwise.
33 * @param arg Pointer argument.
34 */
35#define CheckComArgOutPointerValidThrow(arg) \
36 do { \
37 if (RT_UNLIKELY(!VALID_PTR(arg))) \
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 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 ~ArrayBSTRInConverter()
160 {
161 }
162
163 const std::vector<com::Utf8Str> &array()
164 {
165 return mArray;
166 }
167
168private:
169 std::vector<com::Utf8Str> mArray;
170};
171
172class UuidOutConverter
173{
174public:
175 UuidOutConverter() : mDst(NULL)
176 {
177 }
178
179 UuidOutConverter(BSTR *aDst) : mDst(aDst)
180 {
181 }
182
183 ~UuidOutConverter()
184 {
185 if (mDst)
186 mUuid.toUtf16().detachTo(mDst);
187 }
188
189 com::Guid &uuid()
190 {
191 return mUuid;
192 }
193
194private:
195 com::Guid mUuid;
196 BSTR *mDst;
197};
198
199class UuidInConverter
200{
201public:
202 UuidInConverter() : mSrc()
203 {
204 }
205
206 UuidInConverter(CBSTR aSrc) : mSrc(aSrc)
207 {
208 }
209
210 ~UuidInConverter()
211 {
212 }
213
214 const com::Guid &uuid()
215 {
216 return mSrc;
217 }
218
219private:
220 const com::Guid mSrc;
221};
222
223class ArrayUuidOutConverter
224{
225public:
226 ArrayUuidOutConverter() :
227#ifdef VBOX_WITH_XPCOM
228 mDstSize(NULL),
229 mDst(NULL)
230#else // !VBOX_WITH_XPCOM
231 mDst(NULL)
232#endif // !VBOX_WITH_XPCOM
233 {
234 }
235
236 ArrayUuidOutConverter(ComSafeArrayOut(BSTR, aDst)) :
237#ifdef VBOX_WITH_XPCOM
238 mDstSize(aDstSize),
239 mDst(aDst)
240#else // !VBOX_WITH_XPCOM
241 mDst(aDst)
242#endif // !VBOX_WITH_XPCOM
243 {
244 }
245
246 ~ArrayUuidOutConverter()
247 {
248 if (mDst)
249 {
250 com::SafeArray<BSTR> outArray(mArray.size());
251 for (size_t i = 0; i < mArray.size(); i++)
252 mArray[i].toUtf16().detachTo(&outArray[i]);
253 outArray.detachTo(ComSafeArrayOutArg(mDst));
254 }
255 }
256
257 std::vector<com::Guid> &array()
258 {
259 return mArray;
260 }
261
262private:
263 std::vector<com::Guid> mArray;
264#ifdef VBOX_WITH_XPCOM
265 PRUint32 *mDstSize;
266 BSTR **mDst;
267#else // !VBOX_WITH_XPCOM
268 SAFEARRAY **mDst;
269#endif // !VBOX_WITH_XPCOM
270};
271
272template <class A>
273class ComTypeOutConverter
274{
275public:
276 ComTypeOutConverter() : mDst(NULL)
277 {
278 }
279
280 ComTypeOutConverter(A **aDst) : mDst(aDst)
281 {
282 }
283
284 ~ComTypeOutConverter()
285 {
286 if (mDst)
287 mPtr.queryInterfaceTo(mDst);
288 }
289
290 ComPtr<A> &ptr()
291 {
292 return mPtr;
293 }
294
295private:
296 ComPtr<A> mPtr;
297 A **mDst;
298};
299
300template <class A>
301class ComTypeInConverter
302{
303public:
304 ComTypeInConverter() : mSrc(NULL)
305 {
306 }
307
308 ComTypeInConverter(A *aSrc) : mSrc(mSrc)
309 {
310 }
311
312 ~ComTypeInConverter()
313 {
314 }
315
316 const ComPtr<A> &ptr()
317 {
318 return mSrc;
319 }
320
321private:
322 const ComPtr<A> mSrc;
323};
324
325template <class A>
326class ArrayComTypeOutConverter
327{
328public:
329 ArrayComTypeOutConverter() :
330#ifdef VBOX_WITH_XPCOM
331 mDstSize(NULL),
332 mDst(NULL)
333#else // !VBOX_WITH_XPCOM
334 mDst(NULL)
335#endif // !VBOX_WITH_XPCOM
336 {
337 }
338
339 ArrayComTypeOutConverter(ComSafeArrayOut(A *, aDst)) :
340#ifdef VBOX_WITH_XPCOM
341 mDstSize(aDstSize),
342 mDst(aDst)
343#else // !VBOX_WITH_XPCOM
344 mDst(aDst)
345#endif // !VBOX_WITH_XPCOM
346 {
347 }
348
349 ~ArrayComTypeOutConverter()
350 {
351 if (mDst)
352 {
353 com::SafeIfaceArray<A> outArray(mArray.size());
354 for (size_t i = 0; i < mArray.size(); i++)
355 outArray[i] = mArray[i];
356 outArray.detachTo(ComSafeArrayOutArg(mDst));
357 }
358 }
359
360 std::vector<ComPtr<A> > &array()
361 {
362 return mArray;
363 }
364
365private:
366 std::vector<ComPtr<A> > mArray;
367#ifdef VBOX_WITH_XPCOM
368 PRUint32 *mDstSize;
369 A ***mDst;
370#else // !VBOX_WITH_XPCOM
371 SAFEARRAY **mDst;
372#endif // !VBOX_WITH_XPCOM
373};
374
375template <class A>
376class ArrayComTypeInConverter
377{
378public:
379 ArrayComTypeInConverter()
380 {
381 }
382
383 ArrayComTypeInConverter(ComSafeArrayIn(A *, aSrc))
384 {
385 com::SafeIfaceArray<A> inArray(ComSafeArrayInArg(aSrc));
386 mArray.resize(inArray.size());
387 for (size_t i = 0; i < inArray.size(); i++)
388 mArray[i] = inArray[i];
389 }
390
391 ~ArrayComTypeInConverter()
392 {
393 }
394
395 const std::vector<ComPtr<A> > &array()
396 {
397 return mArray;
398 }
399
400private:
401 std::vector<ComPtr<A> > mArray;
402};
403
404template <typename A>
405class ArrayOutConverter
406{
407public:
408 ArrayOutConverter() :
409#ifdef VBOX_WITH_XPCOM
410 mDstSize(NULL),
411 mDst(NULL)
412#else // !VBOX_WITH_XPCOM
413 mDst(NULL)
414#endif // !VBOX_WITH_XPCOM
415 {
416 }
417
418 ArrayOutConverter(ComSafeArrayOut(A, aDst)) :
419#ifdef VBOX_WITH_XPCOM
420 mDstSize(aDstSize),
421 mDst(aDst)
422#else // !VBOX_WITH_XPCOM
423 mDst(aDst)
424#endif // !VBOX_WITH_XPCOM
425 {
426 }
427
428 ~ArrayOutConverter()
429 {
430 if (mDst)
431 {
432 com::SafeArray<A> outArray(mArray.size());
433 for (size_t i = 0; i < mArray.size(); i++)
434 outArray[i] = mArray[i];
435 outArray.detachTo(ComSafeArrayOutArg(mDst));
436 }
437 }
438
439 std::vector<A> &array()
440 {
441 return mArray;
442 }
443
444private:
445 std::vector<A> mArray;
446#ifdef VBOX_WITH_XPCOM
447 PRUint32 *mDstSize;
448 A **mDst;
449#else // !VBOX_WITH_XPCOM
450 SAFEARRAY **mDst;
451#endif // !VBOX_WITH_XPCOM
452};
453
454template <typename A>
455class ArrayInConverter
456{
457public:
458 ArrayInConverter()
459 {
460 }
461
462 ArrayInConverter(ComSafeArrayIn(A, aSrc))
463 {
464 com::SafeArray<A> inArray(ComSafeArrayInArg(aSrc));
465 mArray.resize(inArray.size());
466 for (size_t i = 0; i < inArray.size(); i++)
467 mArray[i] = inArray[i];
468 }
469
470 ~ArrayInConverter()
471 {
472 }
473
474 const std::vector<A> &array()
475 {
476 return mArray;
477 }
478
479private:
480 std::vector<A> mArray;
481};
482
483#endif // ____H_WRAPPER
484/* 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