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