xref: /aoo42x/main/cppu/source/uno/prim.hxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir #ifndef PRIM_HXX
28*cdf0e10cSrcweir #define PRIM_HXX
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include "typelib/typedescription.h"
31*cdf0e10cSrcweir #ifndef _typelib_TypeClass_H_
32*cdf0e10cSrcweir #include "typelib/typeclass.h"
33*cdf0e10cSrcweir #endif
34*cdf0e10cSrcweir #include "uno/sequence2.h"
35*cdf0e10cSrcweir #include "uno/any2.h"
36*cdf0e10cSrcweir #include "uno/data.h"
37*cdf0e10cSrcweir #include "uno/mapping.h"
38*cdf0e10cSrcweir #include "uno/dispatcher.h"
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #ifndef _OSL_INTERLCK_H
41*cdf0e10cSrcweir #include "osl/interlck.h"
42*cdf0e10cSrcweir #endif
43*cdf0e10cSrcweir #include "osl/diagnose.h"
44*cdf0e10cSrcweir #ifndef _RTL_USTRING_HXX
45*cdf0e10cSrcweir #include "rtl/ustring.hxx"
46*cdf0e10cSrcweir #endif
47*cdf0e10cSrcweir #include "rtl/alloc.h"
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
50*cdf0e10cSrcweir #include "rtl/ustrbuf.hxx"
51*cdf0e10cSrcweir #include "rtl/string.hxx"
52*cdf0e10cSrcweir #endif
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir namespace cppu
56*cdf0e10cSrcweir {
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir extern uno_Sequence g_emptySeq;
59*cdf0e10cSrcweir extern typelib_TypeDescriptionReference * g_pVoidType;
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
62*cdf0e10cSrcweir inline void * _map(
63*cdf0e10cSrcweir 	void * p,
64*cdf0e10cSrcweir 	typelib_TypeDescriptionReference * pType, typelib_TypeDescription * pTypeDescr,
65*cdf0e10cSrcweir 	uno_Mapping * mapping )
66*cdf0e10cSrcweir 	SAL_THROW( () )
67*cdf0e10cSrcweir {
68*cdf0e10cSrcweir 	void * pRet = 0;
69*cdf0e10cSrcweir 	if (p)
70*cdf0e10cSrcweir 	{
71*cdf0e10cSrcweir 		if (pTypeDescr)
72*cdf0e10cSrcweir 		{
73*cdf0e10cSrcweir 			(*mapping->mapInterface)(
74*cdf0e10cSrcweir                 mapping, &pRet, p, (typelib_InterfaceTypeDescription *)pTypeDescr );
75*cdf0e10cSrcweir 		}
76*cdf0e10cSrcweir 		else
77*cdf0e10cSrcweir 		{
78*cdf0e10cSrcweir 			TYPELIB_DANGER_GET( &pTypeDescr, pType );
79*cdf0e10cSrcweir 			(*mapping->mapInterface)(
80*cdf0e10cSrcweir                 mapping, &pRet, p, (typelib_InterfaceTypeDescription *)pTypeDescr );
81*cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pTypeDescr );
82*cdf0e10cSrcweir 		}
83*cdf0e10cSrcweir 	}
84*cdf0e10cSrcweir 	return pRet;
85*cdf0e10cSrcweir }
86*cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
87*cdf0e10cSrcweir inline void _acquire( void * p, uno_AcquireFunc acquire ) SAL_THROW( () )
88*cdf0e10cSrcweir {
89*cdf0e10cSrcweir 	if (p)
90*cdf0e10cSrcweir 	{
91*cdf0e10cSrcweir 		if (acquire)
92*cdf0e10cSrcweir         {
93*cdf0e10cSrcweir 			(*acquire)( p );
94*cdf0e10cSrcweir         }
95*cdf0e10cSrcweir 		else
96*cdf0e10cSrcweir         {
97*cdf0e10cSrcweir 			(*((uno_Interface *)p)->acquire)( (uno_Interface *)p );
98*cdf0e10cSrcweir         }
99*cdf0e10cSrcweir 	}
100*cdf0e10cSrcweir }
101*cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
102*cdf0e10cSrcweir inline void _release( void * p, uno_ReleaseFunc release ) SAL_THROW( () )
103*cdf0e10cSrcweir {
104*cdf0e10cSrcweir 	if (p)
105*cdf0e10cSrcweir 	{
106*cdf0e10cSrcweir 		if (release)
107*cdf0e10cSrcweir         {
108*cdf0e10cSrcweir 			(*release)( p );
109*cdf0e10cSrcweir         }
110*cdf0e10cSrcweir 		else
111*cdf0e10cSrcweir         {
112*cdf0e10cSrcweir 			(*((uno_Interface *)p)->release)( (uno_Interface *)p );
113*cdf0e10cSrcweir         }
114*cdf0e10cSrcweir 	}
115*cdf0e10cSrcweir }
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir //------------------------------------------------------------------------------
118*cdf0e10cSrcweir inline sal_uInt32 calcSeqMemSize(
119*cdf0e10cSrcweir     sal_Int32 nElementSize, sal_Int32 nElements )
120*cdf0e10cSrcweir {
121*cdf0e10cSrcweir     sal_uInt64 nSize =
122*cdf0e10cSrcweir         (sal_uInt64) SAL_SEQUENCE_HEADER_SIZE +
123*cdf0e10cSrcweir         ((sal_uInt64) nElementSize * (sal_uInt64) nElements);
124*cdf0e10cSrcweir     if (nSize > 0xffffffffU)
125*cdf0e10cSrcweir         return 0;
126*cdf0e10cSrcweir     else
127*cdf0e10cSrcweir         return (sal_uInt32) nSize;
128*cdf0e10cSrcweir }
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
131*cdf0e10cSrcweir inline uno_Sequence * createEmptySequence() SAL_THROW( () )
132*cdf0e10cSrcweir {
133*cdf0e10cSrcweir 	::osl_incrementInterlockedCount( &g_emptySeq.nRefCount );
134*cdf0e10cSrcweir 	return &g_emptySeq;
135*cdf0e10cSrcweir }
136*cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
137*cdf0e10cSrcweir inline typelib_TypeDescriptionReference * _getVoidType()
138*cdf0e10cSrcweir 	SAL_THROW( () )
139*cdf0e10cSrcweir {
140*cdf0e10cSrcweir 	if (! g_pVoidType)
141*cdf0e10cSrcweir 	{
142*cdf0e10cSrcweir         g_pVoidType = * ::typelib_static_type_getByTypeClass( typelib_TypeClass_VOID );
143*cdf0e10cSrcweir 	}
144*cdf0e10cSrcweir 	::typelib_typedescriptionreference_acquire( g_pVoidType );
145*cdf0e10cSrcweir 	return g_pVoidType;
146*cdf0e10cSrcweir }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
149*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
150*cdf0e10cSrcweir #define CONSTRUCT_EMPTY_ANY( pAny ) \
151*cdf0e10cSrcweir (pAny)->pType = _getVoidType(); \
152*cdf0e10cSrcweir (pAny)->pData = (void *)0xdeadbeef;
153*cdf0e10cSrcweir #else
154*cdf0e10cSrcweir #define CONSTRUCT_EMPTY_ANY( pAny ) \
155*cdf0e10cSrcweir (pAny)->pType = _getVoidType(); \
156*cdf0e10cSrcweir (pAny)->pData = (pAny);
157*cdf0e10cSrcweir #endif
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
160*cdf0e10cSrcweir #define TYPE_ACQUIRE( pType ) \
161*cdf0e10cSrcweir 	::osl_incrementInterlockedCount( &(pType)->nRefCount );
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
164*cdf0e10cSrcweir extern "C" void * binuno_queryInterface(
165*cdf0e10cSrcweir     void * pUnoI, typelib_TypeDescriptionReference * pDestType );
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
168*cdf0e10cSrcweir inline typelib_TypeDescriptionReference * _unionGetSetType(
169*cdf0e10cSrcweir 	void * pUnion, typelib_TypeDescription * pTD )
170*cdf0e10cSrcweir 	SAL_THROW( () )
171*cdf0e10cSrcweir {
172*cdf0e10cSrcweir 	typelib_TypeDescriptionReference * pRet = 0;
173*cdf0e10cSrcweir 	sal_Int32 nPos;
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir 	sal_Int64 * pDiscr = ((typelib_UnionTypeDescription *)pTD)->pDiscriminants;
176*cdf0e10cSrcweir 	sal_Int64 nDiscr   = *(sal_Int64 *)pUnion;
177*cdf0e10cSrcweir 	for ( nPos = ((typelib_UnionTypeDescription *)pTD)->nMembers; nPos--; )
178*cdf0e10cSrcweir 	{
179*cdf0e10cSrcweir 		if (pDiscr[nPos] == nDiscr)
180*cdf0e10cSrcweir 		{
181*cdf0e10cSrcweir 			pRet = ((typelib_UnionTypeDescription *)pTD)->ppTypeRefs[nPos];
182*cdf0e10cSrcweir 			break;
183*cdf0e10cSrcweir 		}
184*cdf0e10cSrcweir 	}
185*cdf0e10cSrcweir 	if (nPos >= 0)
186*cdf0e10cSrcweir 	{
187*cdf0e10cSrcweir 		// default
188*cdf0e10cSrcweir 		pRet = ((typelib_UnionTypeDescription *)pTD)->pDefaultTypeRef;
189*cdf0e10cSrcweir 	}
190*cdf0e10cSrcweir 	typelib_typedescriptionreference_acquire( pRet );
191*cdf0e10cSrcweir 	return pRet;
192*cdf0e10cSrcweir }
193*cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
194*cdf0e10cSrcweir inline sal_Bool _type_equals(
195*cdf0e10cSrcweir 	typelib_TypeDescriptionReference * pType1, typelib_TypeDescriptionReference * pType2 )
196*cdf0e10cSrcweir 	SAL_THROW( () )
197*cdf0e10cSrcweir {
198*cdf0e10cSrcweir 	return (pType1 == pType2 ||
199*cdf0e10cSrcweir 			(pType1->eTypeClass == pType2->eTypeClass &&
200*cdf0e10cSrcweir 			 pType1->pTypeName->length == pType2->pTypeName->length &&
201*cdf0e10cSrcweir 			 ::rtl_ustr_compare( pType1->pTypeName->buffer, pType2->pTypeName->buffer ) == 0));
202*cdf0e10cSrcweir }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir }
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir #endif
207