VirtualBox

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

Last change on this file since 45518 was 45518, checked in by vboxsync, 12 years ago

Main: Code generator for (xp)com API implementations, including logging and parameter conversion, so far only used by MediumFormat. Next try, needed significant tweaks to work with xpcom (safearray handling fixes in the parameter conversion helpers), different STL implementation (which doesn't support declaring template type parameters as const), missing build dependencies (which didn't show on the dual core system used for writing the code), and finally the duplicate XPCOM classinfo and AddRef/Release/QueryInterface method definitions needed to be removed.

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