xref: /aoo41x/main/cppu/source/uno/prim.hxx (revision c6ed87c9)
1*c6ed87c9SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*c6ed87c9SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*c6ed87c9SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*c6ed87c9SAndrew Rist  * distributed with this work for additional information
6*c6ed87c9SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*c6ed87c9SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*c6ed87c9SAndrew Rist  * "License"); you may not use this file except in compliance
9*c6ed87c9SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*c6ed87c9SAndrew Rist  *
11*c6ed87c9SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*c6ed87c9SAndrew Rist  *
13*c6ed87c9SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*c6ed87c9SAndrew Rist  * software distributed under the License is distributed on an
15*c6ed87c9SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*c6ed87c9SAndrew Rist  * KIND, either express or implied.  See the License for the
17*c6ed87c9SAndrew Rist  * specific language governing permissions and limitations
18*c6ed87c9SAndrew Rist  * under the License.
19*c6ed87c9SAndrew Rist  *
20*c6ed87c9SAndrew Rist  *************************************************************/
21*c6ed87c9SAndrew Rist 
22*c6ed87c9SAndrew Rist 
23cdf0e10cSrcweir #ifndef PRIM_HXX
24cdf0e10cSrcweir #define PRIM_HXX
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include "typelib/typedescription.h"
27cdf0e10cSrcweir #ifndef _typelib_TypeClass_H_
28cdf0e10cSrcweir #include "typelib/typeclass.h"
29cdf0e10cSrcweir #endif
30cdf0e10cSrcweir #include "uno/sequence2.h"
31cdf0e10cSrcweir #include "uno/any2.h"
32cdf0e10cSrcweir #include "uno/data.h"
33cdf0e10cSrcweir #include "uno/mapping.h"
34cdf0e10cSrcweir #include "uno/dispatcher.h"
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #ifndef _OSL_INTERLCK_H
37cdf0e10cSrcweir #include "osl/interlck.h"
38cdf0e10cSrcweir #endif
39cdf0e10cSrcweir #include "osl/diagnose.h"
40cdf0e10cSrcweir #ifndef _RTL_USTRING_HXX
41cdf0e10cSrcweir #include "rtl/ustring.hxx"
42cdf0e10cSrcweir #endif
43cdf0e10cSrcweir #include "rtl/alloc.h"
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
46cdf0e10cSrcweir #include "rtl/ustrbuf.hxx"
47cdf0e10cSrcweir #include "rtl/string.hxx"
48cdf0e10cSrcweir #endif
49cdf0e10cSrcweir 
50cdf0e10cSrcweir 
51cdf0e10cSrcweir namespace cppu
52cdf0e10cSrcweir {
53cdf0e10cSrcweir 
54cdf0e10cSrcweir extern uno_Sequence g_emptySeq;
55cdf0e10cSrcweir extern typelib_TypeDescriptionReference * g_pVoidType;
56cdf0e10cSrcweir 
57cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
_map(void * p,typelib_TypeDescriptionReference * pType,typelib_TypeDescription * pTypeDescr,uno_Mapping * mapping)58cdf0e10cSrcweir inline void * _map(
59cdf0e10cSrcweir 	void * p,
60cdf0e10cSrcweir 	typelib_TypeDescriptionReference * pType, typelib_TypeDescription * pTypeDescr,
61cdf0e10cSrcweir 	uno_Mapping * mapping )
62cdf0e10cSrcweir 	SAL_THROW( () )
63cdf0e10cSrcweir {
64cdf0e10cSrcweir 	void * pRet = 0;
65cdf0e10cSrcweir 	if (p)
66cdf0e10cSrcweir 	{
67cdf0e10cSrcweir 		if (pTypeDescr)
68cdf0e10cSrcweir 		{
69cdf0e10cSrcweir 			(*mapping->mapInterface)(
70cdf0e10cSrcweir                 mapping, &pRet, p, (typelib_InterfaceTypeDescription *)pTypeDescr );
71cdf0e10cSrcweir 		}
72cdf0e10cSrcweir 		else
73cdf0e10cSrcweir 		{
74cdf0e10cSrcweir 			TYPELIB_DANGER_GET( &pTypeDescr, pType );
75cdf0e10cSrcweir 			(*mapping->mapInterface)(
76cdf0e10cSrcweir                 mapping, &pRet, p, (typelib_InterfaceTypeDescription *)pTypeDescr );
77cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pTypeDescr );
78cdf0e10cSrcweir 		}
79cdf0e10cSrcweir 	}
80cdf0e10cSrcweir 	return pRet;
81cdf0e10cSrcweir }
82cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
_acquire(void * p,uno_AcquireFunc acquire)83cdf0e10cSrcweir inline void _acquire( void * p, uno_AcquireFunc acquire ) SAL_THROW( () )
84cdf0e10cSrcweir {
85cdf0e10cSrcweir 	if (p)
86cdf0e10cSrcweir 	{
87cdf0e10cSrcweir 		if (acquire)
88cdf0e10cSrcweir         {
89cdf0e10cSrcweir 			(*acquire)( p );
90cdf0e10cSrcweir         }
91cdf0e10cSrcweir 		else
92cdf0e10cSrcweir         {
93cdf0e10cSrcweir 			(*((uno_Interface *)p)->acquire)( (uno_Interface *)p );
94cdf0e10cSrcweir         }
95cdf0e10cSrcweir 	}
96cdf0e10cSrcweir }
97cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
_release(void * p,uno_ReleaseFunc release)98cdf0e10cSrcweir inline void _release( void * p, uno_ReleaseFunc release ) SAL_THROW( () )
99cdf0e10cSrcweir {
100cdf0e10cSrcweir 	if (p)
101cdf0e10cSrcweir 	{
102cdf0e10cSrcweir 		if (release)
103cdf0e10cSrcweir         {
104cdf0e10cSrcweir 			(*release)( p );
105cdf0e10cSrcweir         }
106cdf0e10cSrcweir 		else
107cdf0e10cSrcweir         {
108cdf0e10cSrcweir 			(*((uno_Interface *)p)->release)( (uno_Interface *)p );
109cdf0e10cSrcweir         }
110cdf0e10cSrcweir 	}
111cdf0e10cSrcweir }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir //------------------------------------------------------------------------------
calcSeqMemSize(sal_Int32 nElementSize,sal_Int32 nElements)114cdf0e10cSrcweir inline sal_uInt32 calcSeqMemSize(
115cdf0e10cSrcweir     sal_Int32 nElementSize, sal_Int32 nElements )
116cdf0e10cSrcweir {
117cdf0e10cSrcweir     sal_uInt64 nSize =
118cdf0e10cSrcweir         (sal_uInt64) SAL_SEQUENCE_HEADER_SIZE +
119cdf0e10cSrcweir         ((sal_uInt64) nElementSize * (sal_uInt64) nElements);
120cdf0e10cSrcweir     if (nSize > 0xffffffffU)
121cdf0e10cSrcweir         return 0;
122cdf0e10cSrcweir     else
123cdf0e10cSrcweir         return (sal_uInt32) nSize;
124cdf0e10cSrcweir }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
createEmptySequence()127cdf0e10cSrcweir inline uno_Sequence * createEmptySequence() SAL_THROW( () )
128cdf0e10cSrcweir {
129cdf0e10cSrcweir 	::osl_incrementInterlockedCount( &g_emptySeq.nRefCount );
130cdf0e10cSrcweir 	return &g_emptySeq;
131cdf0e10cSrcweir }
132cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
_getVoidType()133cdf0e10cSrcweir inline typelib_TypeDescriptionReference * _getVoidType()
134cdf0e10cSrcweir 	SAL_THROW( () )
135cdf0e10cSrcweir {
136cdf0e10cSrcweir 	if (! g_pVoidType)
137cdf0e10cSrcweir 	{
138cdf0e10cSrcweir         g_pVoidType = * ::typelib_static_type_getByTypeClass( typelib_TypeClass_VOID );
139cdf0e10cSrcweir 	}
140cdf0e10cSrcweir 	::typelib_typedescriptionreference_acquire( g_pVoidType );
141cdf0e10cSrcweir 	return g_pVoidType;
142cdf0e10cSrcweir }
143cdf0e10cSrcweir 
144cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
145cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
146cdf0e10cSrcweir #define CONSTRUCT_EMPTY_ANY( pAny ) \
147cdf0e10cSrcweir (pAny)->pType = _getVoidType(); \
148cdf0e10cSrcweir (pAny)->pData = (void *)0xdeadbeef;
149cdf0e10cSrcweir #else
150cdf0e10cSrcweir #define CONSTRUCT_EMPTY_ANY( pAny ) \
151cdf0e10cSrcweir (pAny)->pType = _getVoidType(); \
152cdf0e10cSrcweir (pAny)->pData = (pAny);
153cdf0e10cSrcweir #endif
154cdf0e10cSrcweir 
155cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
156cdf0e10cSrcweir #define TYPE_ACQUIRE( pType ) \
157cdf0e10cSrcweir 	::osl_incrementInterlockedCount( &(pType)->nRefCount );
158cdf0e10cSrcweir 
159cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
160cdf0e10cSrcweir extern "C" void * binuno_queryInterface(
161cdf0e10cSrcweir     void * pUnoI, typelib_TypeDescriptionReference * pDestType );
162cdf0e10cSrcweir 
163cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
_unionGetSetType(void * pUnion,typelib_TypeDescription * pTD)164cdf0e10cSrcweir inline typelib_TypeDescriptionReference * _unionGetSetType(
165cdf0e10cSrcweir 	void * pUnion, typelib_TypeDescription * pTD )
166cdf0e10cSrcweir 	SAL_THROW( () )
167cdf0e10cSrcweir {
168cdf0e10cSrcweir 	typelib_TypeDescriptionReference * pRet = 0;
169cdf0e10cSrcweir 	sal_Int32 nPos;
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 	sal_Int64 * pDiscr = ((typelib_UnionTypeDescription *)pTD)->pDiscriminants;
172cdf0e10cSrcweir 	sal_Int64 nDiscr   = *(sal_Int64 *)pUnion;
173cdf0e10cSrcweir 	for ( nPos = ((typelib_UnionTypeDescription *)pTD)->nMembers; nPos--; )
174cdf0e10cSrcweir 	{
175cdf0e10cSrcweir 		if (pDiscr[nPos] == nDiscr)
176cdf0e10cSrcweir 		{
177cdf0e10cSrcweir 			pRet = ((typelib_UnionTypeDescription *)pTD)->ppTypeRefs[nPos];
178cdf0e10cSrcweir 			break;
179cdf0e10cSrcweir 		}
180cdf0e10cSrcweir 	}
181cdf0e10cSrcweir 	if (nPos >= 0)
182cdf0e10cSrcweir 	{
183cdf0e10cSrcweir 		// default
184cdf0e10cSrcweir 		pRet = ((typelib_UnionTypeDescription *)pTD)->pDefaultTypeRef;
185cdf0e10cSrcweir 	}
186cdf0e10cSrcweir 	typelib_typedescriptionreference_acquire( pRet );
187cdf0e10cSrcweir 	return pRet;
188cdf0e10cSrcweir }
189cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
_type_equals(typelib_TypeDescriptionReference * pType1,typelib_TypeDescriptionReference * pType2)190cdf0e10cSrcweir inline sal_Bool _type_equals(
191cdf0e10cSrcweir 	typelib_TypeDescriptionReference * pType1, typelib_TypeDescriptionReference * pType2 )
192cdf0e10cSrcweir 	SAL_THROW( () )
193cdf0e10cSrcweir {
194cdf0e10cSrcweir 	return (pType1 == pType2 ||
195cdf0e10cSrcweir 			(pType1->eTypeClass == pType2->eTypeClass &&
196cdf0e10cSrcweir 			 pType1->pTypeName->length == pType2->pTypeName->length &&
197cdf0e10cSrcweir 			 ::rtl_ustr_compare( pType1->pTypeName->buffer, pType2->pTypeName->buffer ) == 0));
198cdf0e10cSrcweir }
199cdf0e10cSrcweir 
200cdf0e10cSrcweir }
201cdf0e10cSrcweir 
202cdf0e10cSrcweir #endif
203