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 _UNO_MAPPING_HXX_
24 #define _UNO_MAPPING_HXX_
25
26 #include <cppu/macros.hxx>
27 #include <rtl/alloc.h>
28 #include <rtl/ustring.hxx>
29 #include <uno/mapping.h>
30 #include <com/sun/star/uno/Type.hxx>
31 #include <com/sun/star/uno/Reference.hxx>
32 #include "cppu/unotype.hxx"
33 #include "uno/environment.hxx"
34
35 typedef struct _typelib_TypeDescription typelib_TypeDescription;
36 typedef struct _typelib_InterfaceTypeDescription typelib_InterfaceTypeDescription;
37 typedef struct _uno_Interface uno_Interface;
38
39 namespace com
40 {
41 namespace sun
42 {
43 namespace star
44 {
45 namespace uno
46 {
47
48 /** C++ wrapper for C uno_Mapping.
49
50 @see uno_Mapping
51 */
52 class Mapping
53 {
54 uno_Mapping * _pMapping;
55
56 public:
57 // these are here to force memory de/allocation to sal lib.
58 /** @internal */
operator new(size_t nSize)59 inline static void * SAL_CALL operator new ( size_t nSize ) SAL_THROW( () )
60 { return ::rtl_allocateMemory( nSize ); }
61 /** @internal */
operator delete(void * pMem)62 inline static void SAL_CALL operator delete ( void * pMem ) SAL_THROW( () )
63 { ::rtl_freeMemory( pMem ); }
64 /** @internal */
operator new(size_t,void * pMem)65 inline static void * SAL_CALL operator new ( size_t, void * pMem ) SAL_THROW( () )
66 { return pMem; }
67 /** @internal */
operator delete(void *,void *)68 inline static void SAL_CALL operator delete ( void *, void * ) SAL_THROW( () )
69 {}
70
71 /** Holds a mapping from the specified source to the specified destination by environment
72 type names.
73
74 @param rFrom type name of source environment
75 @param rTo type name of destination environment
76 @param rAddPurpose additional purpose
77 */
78 inline Mapping(
79 const ::rtl::OUString & rFrom, const ::rtl::OUString & rTo,
80 const ::rtl::OUString & rAddPurpose = ::rtl::OUString() )
81 SAL_THROW( () );
82
83 /** Holds a mapping from the specified source to the specified destination.
84
85 @param pFrom source environment
86 @param pTo destination environment
87 @param rAddPurpose additional purpose
88 */
89 inline Mapping(
90 uno_Environment * pFrom, uno_Environment * pTo,
91 const ::rtl::OUString & rAddPurpose = ::rtl::OUString() )
92 SAL_THROW( () );
93
94 /** Holds a mapping from the specified source to the specified destination
95 environment.
96
97 @param from source environment
98 @param to destination environment
99 @param rAddPurpose additional purpose
100 */
101 inline Mapping(const Environment & rFrom, const Environment & rTo,
102 const ::rtl::OUString & rAddPurpose = ::rtl::OUString() )
103 SAL_THROW( () );
104
105 /** Constructor.
106
107 @param pMapping another mapping
108 */
109 inline Mapping( uno_Mapping * pMapping = 0 ) SAL_THROW( () );
110
111 /** Copy constructor.
112
113 @param rMapping another mapping
114 */
115 inline Mapping( const Mapping & rMapping ) SAL_THROW( () );
116
117 /** Destructor.
118 */
119 inline ~Mapping() SAL_THROW( () );
120
121 /** Sets a given mapping.
122
123 @param pMapping another mapping
124 @return this mapping
125 */
126 inline Mapping & SAL_CALL operator = ( uno_Mapping * pMapping ) SAL_THROW( () );
127 /** Sets a given mapping.
128
129 @param rMapping another mapping
130 @return this mapping
131 */
operator =(const Mapping & rMapping)132 inline Mapping & SAL_CALL operator = ( const Mapping & rMapping ) SAL_THROW( () )
133 { return operator = ( rMapping._pMapping ); }
134
135 /** Provides a pointer to the C mapping. The returned mapping is NOT acquired!
136
137 @return UNacquired C mapping
138 */
get() const139 inline uno_Mapping * SAL_CALL get() const SAL_THROW( () )
140 { return _pMapping; }
141
142 /** Tests if a mapping is set.
143
144 @return true if a mapping is set
145 */
is() const146 inline sal_Bool SAL_CALL is() const SAL_THROW( () )
147 { return (_pMapping != 0); }
148
149 /** Releases a set mapping.
150 */
151 inline void SAL_CALL clear() SAL_THROW( () );
152
153 /** Maps an interface from one environment to another.
154
155 @param pInterface source interface
156 @param pTypeDescr type description of interface
157 @return mapped interface
158 */
159 inline void * SAL_CALL mapInterface( void * pInterface, typelib_InterfaceTypeDescription * pTypeDescr ) const SAL_THROW( () );
160 /** Maps an interface from one environment to another.
161
162 @param pInterface source interface
163 @param pTypeDescr type description of interface
164 @return mapped interface
165 */
mapInterface(void * pInterface,typelib_TypeDescription * pTypeDescr) const166 inline void * SAL_CALL mapInterface( void * pInterface, typelib_TypeDescription * pTypeDescr ) const SAL_THROW( () )
167 { return mapInterface( pInterface, (typelib_InterfaceTypeDescription *)pTypeDescr ); }
168
169 /** Maps an interface from one environment to another.
170
171 @param pInterface source interface
172 @param rType type of interface
173 @return mapped interface
174 */
175 inline void * SAL_CALL mapInterface(
176 void * pInterface, const ::com::sun::star::uno::Type & rType ) const SAL_THROW( () );
177
178 /** Maps an interface from one environment to another.
179
180 @param ppOut inout mapped interface
181 @param pInterface source interface
182 @param pTypeDescr type description of interface
183 */
mapInterface(void ** ppOut,void * pInterface,typelib_InterfaceTypeDescription * pTypeDescr) const184 inline void SAL_CALL mapInterface( void ** ppOut, void * pInterface, typelib_InterfaceTypeDescription * pTypeDescr ) const SAL_THROW( () )
185 { (*_pMapping->mapInterface)( _pMapping, ppOut, pInterface, pTypeDescr ); }
186 /** Maps an interface from one environment to another.
187
188 @param ppOut inout mapped interface
189 @param pInterface source interface
190 @param pTypeDescr type description of interface
191 */
mapInterface(void ** ppOut,void * pInterface,typelib_TypeDescription * pTypeDescr) const192 inline void SAL_CALL mapInterface( void ** ppOut, void * pInterface, typelib_TypeDescription * pTypeDescr ) const SAL_THROW( () )
193 { (*_pMapping->mapInterface)( _pMapping, ppOut, pInterface, (typelib_InterfaceTypeDescription *)pTypeDescr ); }
194
195 /** Maps an interface from one environment to another.
196
197 @param ppOut inout mapped interface
198 @param pInterface source interface
199 @param rType type of interface to be mapped
200 */
201 inline void SAL_CALL mapInterface( void ** ppOut, void * pInterface, const ::com::sun::star::uno::Type & rType ) const SAL_THROW( () );
202 };
203 //__________________________________________________________________________________________________
Mapping(const::rtl::OUString & rFrom,const::rtl::OUString & rTo,const::rtl::OUString & rAddPurpose)204 inline Mapping::Mapping(
205 const ::rtl::OUString & rFrom, const ::rtl::OUString & rTo, const ::rtl::OUString & rAddPurpose )
206 SAL_THROW( () )
207 : _pMapping( 0 )
208 {
209 uno_getMappingByName( &_pMapping, rFrom.pData, rTo.pData, rAddPurpose.pData );
210 }
211 //__________________________________________________________________________________________________
Mapping(uno_Environment * pFrom,uno_Environment * pTo,const::rtl::OUString & rAddPurpose)212 inline Mapping::Mapping(
213 uno_Environment * pFrom, uno_Environment * pTo, const ::rtl::OUString & rAddPurpose )
214 SAL_THROW( () )
215 : _pMapping( 0 )
216 {
217 uno_getMapping( &_pMapping, pFrom, pTo, rAddPurpose.pData );
218 }
219 //__________________________________________________________________________________________________
Mapping(const Environment & rFrom,const Environment & rTo,const::rtl::OUString & rAddPurpose)220 inline Mapping::Mapping(
221 const Environment & rFrom, const Environment & rTo, const ::rtl::OUString & rAddPurpose )
222 SAL_THROW( () )
223 : _pMapping(0)
224 {
225 uno_getMapping( &_pMapping, rFrom.get(), rTo.get(), rAddPurpose.pData );
226 }
227 //__________________________________________________________________________________________________
Mapping(uno_Mapping * pMapping)228 inline Mapping::Mapping( uno_Mapping * pMapping ) SAL_THROW( () )
229 : _pMapping( pMapping )
230 {
231 if (_pMapping)
232 (*_pMapping->acquire)( _pMapping );
233 }
234 //__________________________________________________________________________________________________
Mapping(const Mapping & rMapping)235 inline Mapping::Mapping( const Mapping & rMapping ) SAL_THROW( () )
236 : _pMapping( rMapping._pMapping )
237 {
238 if (_pMapping)
239 (*_pMapping->acquire)( _pMapping );
240 }
241 //__________________________________________________________________________________________________
~Mapping()242 inline Mapping::~Mapping() SAL_THROW( () )
243 {
244 if (_pMapping)
245 (*_pMapping->release)( _pMapping );
246 }
247 //__________________________________________________________________________________________________
clear()248 inline void Mapping::clear() SAL_THROW( () )
249 {
250 if (_pMapping)
251 {
252 (*_pMapping->release)( _pMapping );
253 _pMapping = 0;
254 }
255 }
256 //__________________________________________________________________________________________________
operator =(uno_Mapping * pMapping)257 inline Mapping & Mapping::operator = ( uno_Mapping * pMapping ) SAL_THROW( () )
258 {
259 if (pMapping)
260 (*pMapping->acquire)( pMapping );
261 if (_pMapping)
262 (*_pMapping->release)( _pMapping );
263 _pMapping = pMapping;
264 return *this;
265 }
266 //__________________________________________________________________________________________________
mapInterface(void ** ppOut,void * pInterface,const::com::sun::star::uno::Type & rType) const267 inline void Mapping::mapInterface(
268 void ** ppOut, void * pInterface, const ::com::sun::star::uno::Type & rType ) const
269 SAL_THROW( () )
270 {
271 typelib_TypeDescription * pTD = 0;
272 TYPELIB_DANGER_GET( &pTD, rType.getTypeLibType() );
273 if (pTD)
274 {
275 (*_pMapping->mapInterface)( _pMapping, ppOut, pInterface, (typelib_InterfaceTypeDescription *)pTD );
276 TYPELIB_DANGER_RELEASE( pTD );
277 }
278 }
279 //__________________________________________________________________________________________________
mapInterface(void * pInterface,typelib_InterfaceTypeDescription * pTypeDescr) const280 inline void * Mapping::mapInterface(
281 void * pInterface, typelib_InterfaceTypeDescription * pTypeDescr ) const
282 SAL_THROW( () )
283 {
284 void * pOut = 0;
285 (*_pMapping->mapInterface)( _pMapping, &pOut, pInterface, pTypeDescr );
286 return pOut;
287 }
288 //__________________________________________________________________________________________________
mapInterface(void * pInterface,const::com::sun::star::uno::Type & rType) const289 inline void * Mapping::mapInterface(
290 void * pInterface, const ::com::sun::star::uno::Type & rType ) const
291 SAL_THROW( () )
292 {
293 void * pOut = 0;
294 mapInterface( &pOut, pInterface, rType );
295 return pOut;
296 }
297
298 /** Deprecated. This function DOES NOT WORK with Purpose Environments
299 (http://wiki.services.openoffice.org/wiki/Uno/Binary/Spec/Purpose Environments)
300
301 Maps an binary C UNO interface to be used in the currently used compiler environment.
302
303 @tplparam C interface type
304 @param ppRet inout returned interface pointer
305 @param pUnoI binary C UNO interface
306 @return true if successful, false otherwise
307
308 @deprecated
309 */
310 template< class C >
mapToCpp(Reference<C> * ppRet,uno_Interface * pUnoI)311 inline sal_Bool mapToCpp( Reference< C > * ppRet, uno_Interface * pUnoI ) SAL_THROW( () )
312 {
313 Mapping aMapping(
314 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(UNO_LB_UNO) ),
315 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME) ) );
316 OSL_ASSERT( aMapping.is() );
317 aMapping.mapInterface(
318 (void **)ppRet, pUnoI, ::cppu::getTypeFavourUnsigned( ppRet ) );
319 return (0 != *ppRet);
320 }
321 /** Deprecated. This function DOES NOT WORK with Purpose Environments
322 (http://wiki.services.openoffice.org/wiki/Uno/Binary/Spec/Purpose Environments)
323
324 Maps an UNO interface of the currently used compiler environment to binary C UNO.
325
326 @tplparam C interface type
327 @param ppRet inout returned interface pointer
328 @param x interface reference
329 @return true if successful, false otherwise
330
331 @deprecated
332 */
333 template< class C >
mapToUno(uno_Interface ** ppRet,const Reference<C> & x)334 inline sal_Bool mapToUno( uno_Interface ** ppRet, const Reference< C > & x ) SAL_THROW( () )
335 {
336 Mapping aMapping(
337 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME) ),
338 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(UNO_LB_UNO) ) );
339 OSL_ASSERT( aMapping.is() );
340 aMapping.mapInterface(
341 (void **)ppRet, x.get(), ::cppu::getTypeFavourUnsigned( &x ) );
342 return (0 != *ppRet);
343 }
344
345 }
346 }
347 }
348 }
349
350 #endif
351