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