merged tag ooo/DEV300_m102
[LibreOffice.git] / cppu / source / uno / prim.hxx
blob4acafd125d5579ae6a7ae88f74758b539f54ee0f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
27 #ifndef PRIM_HXX
28 #define PRIM_HXX
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"
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"
49 #if OSL_DEBUG_LEVEL > 1
50 #include "rtl/ustrbuf.hxx"
51 #include "rtl/string.hxx"
52 #endif
55 namespace cppu
58 extern uno_Sequence g_emptySeq;
59 extern typelib_TypeDescriptionReference * g_pVoidType;
61 //--------------------------------------------------------------------------------------------------
62 inline void * _map(
63 void * p,
64 typelib_TypeDescriptionReference * pType, typelib_TypeDescription * pTypeDescr,
65 uno_Mapping * mapping )
66 SAL_THROW( () )
68 void * pRet = 0;
69 if (p)
71 if (pTypeDescr)
73 (*mapping->mapInterface)(
74 mapping, &pRet, p, (typelib_InterfaceTypeDescription *)pTypeDescr );
76 else
78 TYPELIB_DANGER_GET( &pTypeDescr, pType );
79 (*mapping->mapInterface)(
80 mapping, &pRet, p, (typelib_InterfaceTypeDescription *)pTypeDescr );
81 TYPELIB_DANGER_RELEASE( pTypeDescr );
84 return pRet;
86 //--------------------------------------------------------------------------------------------------
87 inline void _acquire( void * p, uno_AcquireFunc acquire ) SAL_THROW( () )
89 if (p)
91 if (acquire)
93 (*acquire)( p );
95 else
97 (*((uno_Interface *)p)->acquire)( (uno_Interface *)p );
101 //--------------------------------------------------------------------------------------------------
102 inline void _release( void * p, uno_ReleaseFunc release ) SAL_THROW( () )
104 if (p)
106 if (release)
108 (*release)( p );
110 else
112 (*((uno_Interface *)p)->release)( (uno_Interface *)p );
117 //------------------------------------------------------------------------------
118 inline sal_uInt32 calcSeqMemSize(
119 sal_Int32 nElementSize, sal_Int32 nElements )
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;
130 //--------------------------------------------------------------------------------------------------
131 inline uno_Sequence * createEmptySequence() SAL_THROW( () )
133 ::osl_incrementInterlockedCount( &g_emptySeq.nRefCount );
134 return &g_emptySeq;
136 //--------------------------------------------------------------------------------------------------
137 inline typelib_TypeDescriptionReference * _getVoidType()
138 SAL_THROW( () )
140 if (! g_pVoidType)
142 g_pVoidType = * ::typelib_static_type_getByTypeClass( typelib_TypeClass_VOID );
144 ::typelib_typedescriptionreference_acquire( g_pVoidType );
145 return g_pVoidType;
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
159 //--------------------------------------------------------------------------------------------------
160 #define TYPE_ACQUIRE( pType ) \
161 ::osl_incrementInterlockedCount( &(pType)->nRefCount );
163 //--------------------------------------------------------------------------------------------------
164 extern "C" void * binuno_queryInterface(
165 void * pUnoI, typelib_TypeDescriptionReference * pDestType );
167 //--------------------------------------------------------------------------------------------------
168 inline typelib_TypeDescriptionReference * _unionGetSetType(
169 void * pUnion, typelib_TypeDescription * pTD )
170 SAL_THROW( () )
172 typelib_TypeDescriptionReference * pRet = 0;
173 sal_Int32 nPos;
175 sal_Int64 * pDiscr = ((typelib_UnionTypeDescription *)pTD)->pDiscriminants;
176 sal_Int64 nDiscr = *(sal_Int64 *)pUnion;
177 for ( nPos = ((typelib_UnionTypeDescription *)pTD)->nMembers; nPos--; )
179 if (pDiscr[nPos] == nDiscr)
181 pRet = ((typelib_UnionTypeDescription *)pTD)->ppTypeRefs[nPos];
182 break;
185 if (nPos >= 0)
187 // default
188 pRet = ((typelib_UnionTypeDescription *)pTD)->pDefaultTypeRef;
190 typelib_typedescriptionreference_acquire( pRet );
191 return pRet;
193 //--------------------------------------------------------------------------------------------------
194 inline sal_Bool _type_equals(
195 typelib_TypeDescriptionReference * pType1, typelib_TypeDescriptionReference * pType2 )
196 SAL_THROW( () )
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));
206 #endif