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