merge the formfield patch from ooo-build
[ooovba.git] / cppu / source / uno / data.cxx
blobe2636b544945b7f527c195d1a099683591f9e0d4
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: data.cxx,v $
10 * $Revision: 1.33 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_cppu.hxx"
34 #include "cppu/macros.hxx"
36 #include "osl/mutex.hxx"
38 #include "constr.hxx"
39 #include "destr.hxx"
40 #include "copy.hxx"
41 #include "assign.hxx"
42 #include "eq.hxx"
44 #include "boost/static_assert.hpp"
47 using namespace ::cppu;
48 using namespace ::rtl;
49 using namespace ::osl;
52 namespace cppu
55 // Sequence<>() (default ctor) relies on this being static:
56 uno_Sequence g_emptySeq = { 1, 0, { 0 } };
57 typelib_TypeDescriptionReference * g_pVoidType = 0;
59 //--------------------------------------------------------------------------------------------------
60 void * binuno_queryInterface( void * pUnoI, typelib_TypeDescriptionReference * pDestType )
62 // init queryInterface() td
63 static typelib_TypeDescription * g_pQITD = 0;
64 if (0 == g_pQITD)
66 MutexGuard aGuard( Mutex::getGlobalMutex() );
67 if (0 == g_pQITD)
69 typelib_TypeDescriptionReference * type_XInterface =
70 * typelib_static_type_getByTypeClass( typelib_TypeClass_INTERFACE );
71 typelib_InterfaceTypeDescription * pTXInterfaceDescr = 0;
72 TYPELIB_DANGER_GET( (typelib_TypeDescription **) &pTXInterfaceDescr, type_XInterface );
73 OSL_ASSERT( pTXInterfaceDescr->ppAllMembers );
74 typelib_typedescriptionreference_getDescription(
75 &g_pQITD, pTXInterfaceDescr->ppAllMembers[ 0 ] );
76 TYPELIB_DANGER_RELEASE( (typelib_TypeDescription *) pTXInterfaceDescr );
80 uno_Any aRet, aExc;
81 uno_Any * pExc = &aExc;
82 void * aArgs[ 1 ];
83 aArgs[ 0 ] = &pDestType;
84 (*((uno_Interface *) pUnoI)->pDispatcher)(
85 (uno_Interface *) pUnoI, g_pQITD, &aRet, aArgs, &pExc );
87 uno_Interface * ret = 0;
88 if (0 == pExc)
90 typelib_TypeDescriptionReference * ret_type = aRet.pType;
91 switch (ret_type->eTypeClass)
93 case typelib_TypeClass_VOID: // common case
94 typelib_typedescriptionreference_release( ret_type );
95 break;
96 case typelib_TypeClass_INTERFACE:
97 // tweaky... avoiding acquire/ release pair
98 typelib_typedescriptionreference_release( ret_type );
99 ret = (uno_Interface *) aRet.pReserved; // serving acquired interface
100 break;
101 default:
102 _destructAny( &aRet, 0 );
103 break;
106 else
108 #if OSL_DEBUG_LEVEL > 1
109 OUStringBuffer buf( 128 );
110 buf.appendAscii(
111 RTL_CONSTASCII_STRINGPARAM("### exception occured querying for interface ") );
112 buf.append( * reinterpret_cast< OUString const * >( &pDestType->pTypeName ) );
113 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(": [") );
114 buf.append( * reinterpret_cast< OUString const * >( &pExc->pType->pTypeName ) );
115 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("] ") );
116 // Message is very first member
117 buf.append( * reinterpret_cast< OUString const * >( pExc->pData ) );
118 OString cstr(
119 OUStringToOString( buf.makeStringAndClear(), RTL_TEXTENCODING_ASCII_US ) );
120 OSL_ENSURE( 0, cstr.getStr() );
121 #endif
122 uno_any_destruct( pExc, 0 );
124 return ret;
127 //==================================================================================================
128 void defaultConstructStruct(
129 void * pMem,
130 typelib_CompoundTypeDescription * pCompType )
131 SAL_THROW( () )
133 _defaultConstructStruct( pMem, pCompType );
135 //==================================================================================================
136 void copyConstructStruct(
137 void * pDest, void * pSource,
138 typelib_CompoundTypeDescription * pTypeDescr,
139 uno_AcquireFunc acquire, uno_Mapping * mapping )
140 SAL_THROW( () )
142 _copyConstructStruct( pDest, pSource, pTypeDescr, acquire, mapping );
144 //==================================================================================================
145 void destructStruct(
146 void * pValue,
147 typelib_CompoundTypeDescription * pTypeDescr,
148 uno_ReleaseFunc release )
149 SAL_THROW( () )
151 _destructStruct( pValue, pTypeDescr, release );
153 //==================================================================================================
154 sal_Bool equalStruct(
155 void * pDest, void *pSource,
156 typelib_CompoundTypeDescription * pTypeDescr,
157 uno_QueryInterfaceFunc queryInterface, uno_ReleaseFunc release )
158 SAL_THROW( () )
160 return _equalStruct( pDest, pSource, pTypeDescr, queryInterface, release );
162 //==================================================================================================
163 sal_Bool assignStruct(
164 void * pDest, void * pSource,
165 typelib_CompoundTypeDescription * pTypeDescr,
166 uno_QueryInterfaceFunc queryInterface, uno_AcquireFunc acquire, uno_ReleaseFunc release )
167 SAL_THROW( () )
169 return _assignStruct( pDest, pSource, pTypeDescr, queryInterface, acquire, release );
172 //==============================================================================
173 uno_Sequence * copyConstructSequence(
174 uno_Sequence * pSource,
175 typelib_TypeDescriptionReference * pElementType,
176 uno_AcquireFunc acquire, uno_Mapping * mapping )
178 return icopyConstructSequence( pSource, pElementType, acquire, mapping );
181 //==============================================================================
182 void destructSequence(
183 uno_Sequence * pSequence,
184 typelib_TypeDescriptionReference * pType,
185 typelib_TypeDescription * pTypeDescr,
186 uno_ReleaseFunc release )
188 idestructSequence( pSequence, pType, pTypeDescr, release );
191 //==================================================================================================
192 sal_Bool equalSequence(
193 uno_Sequence * pDest, uno_Sequence * pSource,
194 typelib_TypeDescriptionReference * pElementType,
195 uno_QueryInterfaceFunc queryInterface, uno_ReleaseFunc release )
196 SAL_THROW( () )
198 return _equalSequence( pDest, pSource, pElementType, queryInterface, release );
201 extern "C"
203 //##################################################################################################
204 void SAL_CALL uno_type_constructData(
205 void * pMem, typelib_TypeDescriptionReference * pType )
206 SAL_THROW_EXTERN_C()
208 _defaultConstructData( pMem, pType, 0 );
210 //##################################################################################################
211 void SAL_CALL uno_constructData(
212 void * pMem, typelib_TypeDescription * pTypeDescr )
213 SAL_THROW_EXTERN_C()
215 _defaultConstructData( pMem, pTypeDescr->pWeakRef, pTypeDescr );
217 //##################################################################################################
218 void SAL_CALL uno_type_destructData(
219 void * pValue, typelib_TypeDescriptionReference * pType,
220 uno_ReleaseFunc release )
221 SAL_THROW_EXTERN_C()
223 _destructData( pValue, pType, 0, release );
225 //##################################################################################################
226 void SAL_CALL uno_destructData(
227 void * pValue,
228 typelib_TypeDescription * pTypeDescr,
229 uno_ReleaseFunc release )
230 SAL_THROW_EXTERN_C()
232 _destructData( pValue, pTypeDescr->pWeakRef, pTypeDescr, release );
234 //##################################################################################################
235 void SAL_CALL uno_type_copyData(
236 void * pDest, void * pSource,
237 typelib_TypeDescriptionReference * pType,
238 uno_AcquireFunc acquire )
239 SAL_THROW_EXTERN_C()
241 _copyConstructData( pDest, pSource, pType, 0, acquire, 0 );
243 //##################################################################################################
244 void SAL_CALL uno_copyData(
245 void * pDest, void * pSource,
246 typelib_TypeDescription * pTypeDescr,
247 uno_AcquireFunc acquire )
248 SAL_THROW_EXTERN_C()
250 _copyConstructData( pDest, pSource, pTypeDescr->pWeakRef, pTypeDescr, acquire, 0 );
252 //##################################################################################################
253 void SAL_CALL uno_type_copyAndConvertData(
254 void * pDest, void * pSource,
255 typelib_TypeDescriptionReference * pType,
256 uno_Mapping * mapping )
257 SAL_THROW_EXTERN_C()
259 _copyConstructData( pDest, pSource, pType, 0, 0, mapping );
261 //##################################################################################################
262 void SAL_CALL uno_copyAndConvertData(
263 void * pDest, void * pSource,
264 typelib_TypeDescription * pTypeDescr,
265 uno_Mapping * mapping )
266 SAL_THROW_EXTERN_C()
268 _copyConstructData( pDest, pSource, pTypeDescr->pWeakRef, pTypeDescr, 0, mapping );
270 //##################################################################################################
271 sal_Bool SAL_CALL uno_type_equalData(
272 void * pVal1, typelib_TypeDescriptionReference * pVal1Type,
273 void * pVal2, typelib_TypeDescriptionReference * pVal2Type,
274 uno_QueryInterfaceFunc queryInterface, uno_ReleaseFunc release )
275 SAL_THROW_EXTERN_C()
277 return _equalData(
278 pVal1, pVal1Type, 0,
279 pVal2, pVal2Type, 0,
280 queryInterface, release );
282 //##################################################################################################
283 sal_Bool SAL_CALL uno_equalData(
284 void * pVal1, typelib_TypeDescription * pVal1TD,
285 void * pVal2, typelib_TypeDescription * pVal2TD,
286 uno_QueryInterfaceFunc queryInterface, uno_ReleaseFunc release )
287 SAL_THROW_EXTERN_C()
289 return _equalData(
290 pVal1, pVal1TD->pWeakRef, pVal1TD,
291 pVal2, pVal2TD->pWeakRef, pVal2TD,
292 queryInterface, release );
294 //##################################################################################################
295 sal_Bool SAL_CALL uno_type_assignData(
296 void * pDest, typelib_TypeDescriptionReference * pDestType,
297 void * pSource, typelib_TypeDescriptionReference * pSourceType,
298 uno_QueryInterfaceFunc queryInterface, uno_AcquireFunc acquire, uno_ReleaseFunc release )
299 SAL_THROW_EXTERN_C()
301 return _assignData(
302 pDest, pDestType, 0,
303 pSource, pSourceType, 0,
304 queryInterface, acquire, release );
306 //##################################################################################################
307 sal_Bool SAL_CALL uno_assignData(
308 void * pDest, typelib_TypeDescription * pDestTD,
309 void * pSource, typelib_TypeDescription * pSourceTD,
310 uno_QueryInterfaceFunc queryInterface, uno_AcquireFunc acquire, uno_ReleaseFunc release )
311 SAL_THROW_EXTERN_C()
313 return _assignData(
314 pDest, pDestTD->pWeakRef, pDestTD,
315 pSource, pSourceTD->pWeakRef, pSourceTD,
316 queryInterface, acquire, release );
318 //##################################################################################################
319 sal_Bool SAL_CALL uno_type_isAssignableFromData(
320 typelib_TypeDescriptionReference * pAssignable,
321 void * pFrom, typelib_TypeDescriptionReference * pFromType,
322 uno_QueryInterfaceFunc queryInterface, uno_ReleaseFunc release )
323 SAL_THROW_EXTERN_C()
325 if (::typelib_typedescriptionreference_isAssignableFrom( pAssignable, pFromType ))
326 return sal_True;
327 if (typelib_TypeClass_INTERFACE != pFromType->eTypeClass ||
328 typelib_TypeClass_INTERFACE != pAssignable->eTypeClass)
330 return sal_False;
333 // query
334 if (0 == pFrom)
335 return sal_False;
336 void * pInterface = *(void **)pFrom;
337 if (0 == pInterface)
338 return sal_False;
340 if (0 == queryInterface)
341 queryInterface = binuno_queryInterface;
342 void * p = (*queryInterface)( pInterface, pAssignable );
343 _release( p, release );
344 return (0 != p);
349 //##################################################################################################
350 //##################################################################################################
351 //##################################################################################################
354 #if OSL_DEBUG_LEVEL > 1
356 #include <stdio.h>
358 #if defined( SAL_W32)
359 #pragma pack(push, 8)
360 #elif defined(SAL_OS2)
361 #pragma pack(push, 4)
362 #endif
364 #if defined(INTEL) \
365 && (defined(__GNUC__) && (defined(LINUX) || defined(FREEBSD) || defined(OS2)) || defined(MACOSX) \
366 || defined(__SUNPRO_CC) && defined(SOLARIS))
367 #define MAX_ALIGNMENT_4
368 #endif
370 #define OFFSET_OF( s, m ) ((sal_Size)((char *)&((s *)16)->m -16))
372 #define BINTEST_VERIFY( c ) \
373 if (! (c)) { fprintf( stderr, "### binary compatibility test failed: " #c " [line %d]!!!\n", __LINE__ ); abort(); }
374 #define BINTEST_VERIFYOFFSET( s, m, n ) \
375 if (OFFSET_OF(s, m) != n) { fprintf( stderr, "### OFFSET_OF(" #s ", " #m ") = %d instead of expected %d!!!\n", OFFSET_OF(s, m), n ); abort(); }
377 #if OSL_DEBUG_LEVEL > 1
378 #if defined(__GNUC__) && (defined(LINUX) || defined(FREEBSD)) && (defined(INTEL) || defined(POWERPC) || defined(X86_64) || defined(S390))
379 #define BINTEST_VERIFYSIZE( s, n ) \
380 fprintf( stderr, "> sizeof(" #s ") = %d; __alignof__ (" #s ") = %d\n", sizeof(s), __alignof__ (s) ); \
381 if (sizeof(s) != n) { fprintf( stderr, "### sizeof(" #s ") = %d instead of expected %d!!!\n", sizeof(s), n ); abort(); }
382 #else // ! GNUC
383 #define BINTEST_VERIFYSIZE( s, n ) \
384 fprintf( stderr, "> sizeof(" #s ") = %d\n", sizeof(s) ); \
385 if (sizeof(s) != n) { fprintf( stderr, "### sizeof(" #s ") = %d instead of expected %d!!!\n", sizeof(s), n ); abort(); }
386 #endif
387 #else // ! OSL_DEBUG_LEVEL
388 #define BINTEST_VERIFYSIZE( s, n ) \
389 if (sizeof(s) != n) { fprintf( stderr, "### sizeof(" #s ") = %d instead of expected %d!!!\n", sizeof(s), n ); abort(); }
390 #endif
392 struct C1
394 sal_Int16 n1;
396 struct C2 : public C1
398 sal_Int32 n2 CPPU_GCC3_ALIGN( C1 );
400 struct C3 : public C2
402 double d3;
403 sal_Int32 n3;
405 struct C4 : public C3
407 sal_Int32 n4 CPPU_GCC3_ALIGN( C3 );
408 double d4;
410 struct C5 : public C4
412 sal_Int64 n5;
413 sal_Bool b5;
415 struct C6 : public C1
417 C5 c6 CPPU_GCC3_ALIGN( C1 );
418 sal_Bool b6;
421 struct D
423 sal_Int16 d;
424 sal_Int32 e;
426 struct E
428 sal_Bool a;
429 sal_Bool b;
430 sal_Bool c;
431 sal_Int16 d;
432 sal_Int32 e;
435 struct M
437 sal_Int32 n;
438 sal_Int16 o;
441 struct N : public M
443 sal_Int16 p CPPU_GCC3_ALIGN( M );
445 struct N2
447 M m;
448 sal_Int16 p;
451 struct O : public M
453 double p;
454 sal_Int16 q;
456 struct O2 : public O
458 sal_Int16 p2 CPPU_GCC3_ALIGN( O );
461 struct P : public N
463 double p2;
466 struct empty
469 struct second : public empty
471 int a;
474 struct AlignSize_Impl
476 sal_Int16 nInt16;
477 double dDouble;
480 struct Char1
482 char c1;
484 struct Char2 : public Char1
486 char c2 CPPU_GCC3_ALIGN( Char1 );
488 struct Char3 : public Char2
490 char c3 CPPU_GCC3_ALIGN( Char2 );
492 struct Char4
494 Char3 chars;
495 char c;
497 class Ref
499 void * p;
501 enum Enum
503 v = SAL_MAX_ENUM
507 class BinaryCompatible_Impl
509 public:
510 BinaryCompatible_Impl();
512 BinaryCompatible_Impl::BinaryCompatible_Impl()
514 BOOST_STATIC_ASSERT( ((sal_Bool) true) == sal_True &&
515 (1 != 0) == sal_True );
516 BOOST_STATIC_ASSERT( ((sal_Bool) false) == sal_False &&
517 (1 == 0) == sal_False );
518 #ifdef MAX_ALIGNMENT_4
519 // max alignment is 4
520 BINTEST_VERIFYOFFSET( AlignSize_Impl, dDouble, 4 );
521 BINTEST_VERIFYSIZE( AlignSize_Impl, 12 );
522 #else
523 // max alignment is 8
524 BINTEST_VERIFYOFFSET( AlignSize_Impl, dDouble, 8 );
525 BINTEST_VERIFYSIZE( AlignSize_Impl, 16 );
526 #endif
528 // sequence
529 BINTEST_VERIFY( (SAL_SEQUENCE_HEADER_SIZE % 8) == 0 );
530 // enum
531 BINTEST_VERIFY( sizeof( Enum ) == sizeof( sal_Int32 ) );
532 // any
533 BINTEST_VERIFY( sizeof(void *) >= sizeof(sal_Int32) );
534 BINTEST_VERIFY( sizeof( uno_Any ) == sizeof( void * ) * 3 );
535 BINTEST_VERIFYOFFSET( uno_Any, pType, 0 );
536 BINTEST_VERIFYOFFSET( uno_Any, pData, 1 * sizeof (void *) );
537 BINTEST_VERIFYOFFSET( uno_Any, pReserved, 2 * sizeof (void *) );
538 // interface
539 BINTEST_VERIFY( sizeof( Ref ) == sizeof( void * ) );
540 // string
541 BINTEST_VERIFY( sizeof( OUString ) == sizeof( rtl_uString * ) );
542 // struct
543 BINTEST_VERIFYSIZE( M, 8 );
544 BINTEST_VERIFYOFFSET( M, o, 4 );
545 BINTEST_VERIFYSIZE( N, 12 );
546 BINTEST_VERIFYOFFSET( N, p, 8 );
547 BINTEST_VERIFYSIZE( N2, 12 );
548 BINTEST_VERIFYOFFSET( N2, p, 8 );
549 #ifdef MAX_ALIGNMENT_4
550 BINTEST_VERIFYSIZE( O, 20 );
551 #else
552 BINTEST_VERIFYSIZE( O, 24 );
553 #endif
554 BINTEST_VERIFYSIZE( D, 8 );
555 BINTEST_VERIFYOFFSET( D, e, 4 );
556 BINTEST_VERIFYOFFSET( E, d, 4 );
557 BINTEST_VERIFYOFFSET( E, e, 8 );
559 BINTEST_VERIFYSIZE( C1, 2 );
560 BINTEST_VERIFYSIZE( C2, 8 );
561 BINTEST_VERIFYOFFSET( C2, n2, 4 );
563 #ifdef MAX_ALIGNMENT_4
564 BINTEST_VERIFYSIZE( C3, 20 );
565 BINTEST_VERIFYOFFSET( C3, d3, 8 );
566 BINTEST_VERIFYOFFSET( C3, n3, 16 );
567 BINTEST_VERIFYSIZE( C4, 32 );
568 BINTEST_VERIFYOFFSET( C4, n4, 20 );
569 BINTEST_VERIFYOFFSET( C4, d4, 24 );
570 BINTEST_VERIFYSIZE( C5, 44 );
571 BINTEST_VERIFYOFFSET( C5, n5, 32 );
572 BINTEST_VERIFYOFFSET( C5, b5, 40 );
573 BINTEST_VERIFYSIZE( C6, 52 );
574 BINTEST_VERIFYOFFSET( C6, c6, 4 );
575 BINTEST_VERIFYOFFSET( C6, b6, 48 );
577 BINTEST_VERIFYSIZE( O2, 24 );
578 BINTEST_VERIFYOFFSET( O2, p2, 20 );
579 #else
580 BINTEST_VERIFYSIZE( C3, 24 );
581 BINTEST_VERIFYOFFSET( C3, d3, 8 );
582 BINTEST_VERIFYOFFSET( C3, n3, 16 );
583 BINTEST_VERIFYSIZE( C4, 40 );
584 BINTEST_VERIFYOFFSET( C4, n4, 24 );
585 BINTEST_VERIFYOFFSET( C4, d4, 32 );
586 BINTEST_VERIFYSIZE( C5, 56 );
587 BINTEST_VERIFYOFFSET( C5, n5, 40 );
588 BINTEST_VERIFYOFFSET( C5, b5, 48 );
589 BINTEST_VERIFYSIZE( C6, 72 );
590 BINTEST_VERIFYOFFSET( C6, c6, 8 );
591 BINTEST_VERIFYOFFSET( C6, b6, 64 );
593 BINTEST_VERIFYSIZE( O2, 32 );
594 BINTEST_VERIFYOFFSET( O2, p2, 24 );
595 #endif
597 BINTEST_VERIFYSIZE( Char3, 3 );
598 BINTEST_VERIFYOFFSET( Char4, c, 3 );
600 #ifdef MAX_ALIGNMENT_4
601 // max alignment is 4
602 BINTEST_VERIFYSIZE( P, 20 );
603 #else
604 // alignment of P is 8, because of P[] ...
605 BINTEST_VERIFYSIZE( P, 24 );
606 BINTEST_VERIFYSIZE( second, sizeof( int ) );
607 #endif
610 #ifdef SAL_W32
611 # pragma pack(pop)
612 #elif defined(SAL_OS2)
613 # pragma pack()
614 #endif
616 static BinaryCompatible_Impl aTest;
618 #endif