Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 : #ifndef INCLUDED_UNO_MAPPING_HXX
20 : #define INCLUDED_UNO_MAPPING_HXX
21 :
22 : #include <uno/lbnames.h>
23 : #include <rtl/alloc.h>
24 : #include <rtl/ustring.hxx>
25 : #include <osl/diagnose.h>
26 : #include <uno/mapping.h>
27 : #include <com/sun/star/uno/Type.hxx>
28 : #include <com/sun/star/uno/Reference.hxx>
29 : #include <cppu/unotype.hxx>
30 : #include <uno/environment.hxx>
31 : #include <uno/lbnames.h>
32 :
33 : typedef struct _typelib_TypeDescription typelib_TypeDescription;
34 : typedef struct _typelib_InterfaceTypeDescription typelib_InterfaceTypeDescription;
35 : typedef struct _uno_Interface uno_Interface;
36 :
37 : namespace com
38 : {
39 : namespace sun
40 : {
41 : namespace star
42 : {
43 : namespace uno
44 : {
45 :
46 : /** C++ wrapper for C uno_Mapping.
47 :
48 : @see uno_Mapping
49 : */
50 : class Mapping
51 : {
52 : uno_Mapping * _pMapping;
53 :
54 : public:
55 : // these are here to force memory de/allocation to sal lib.
56 : /// @cond INTERNAL
57 : inline static void * SAL_CALL operator new ( size_t nSize )
58 : { return ::rtl_allocateMemory( nSize ); }
59 : inline static void SAL_CALL operator delete ( void * pMem )
60 : { ::rtl_freeMemory( pMem ); }
61 : inline static void * SAL_CALL operator new ( size_t, void * pMem )
62 : { return pMem; }
63 : inline static void SAL_CALL operator delete ( void *, void * )
64 : {}
65 : /// @endcond
66 :
67 : /** Holds a mapping from the specified source to the specified destination by environment
68 : type names.
69 :
70 : @param rFrom type name of source environment
71 : @param rTo type name of destination environment
72 : @param rAddPurpose additional purpose
73 : */
74 : inline Mapping(
75 : const ::rtl::OUString & rFrom, const ::rtl::OUString & rTo,
76 : const ::rtl::OUString & rAddPurpose = ::rtl::OUString() );
77 :
78 : /** Holds a mapping from the specified source to the specified destination.
79 :
80 : @param pFrom source environment
81 : @param pTo destination environment
82 : @param rAddPurpose additional purpose
83 : */
84 : inline Mapping(
85 : uno_Environment * pFrom, uno_Environment * pTo,
86 : const ::rtl::OUString & rAddPurpose = ::rtl::OUString() );
87 :
88 : /** Holds a mapping from the specified source to the specified destination
89 : environment.
90 :
91 : @param rFrom source environment
92 : @param rTo destination environment
93 : @param rAddPurpose additional purpose
94 : */
95 : inline Mapping(const Environment & rFrom, const Environment & rTo,
96 : const ::rtl::OUString & rAddPurpose = ::rtl::OUString() );
97 :
98 : /** Constructor.
99 :
100 : @param pMapping another mapping
101 : */
102 : inline Mapping( uno_Mapping * pMapping = 0 );
103 :
104 : /** Copy constructor.
105 :
106 : @param rMapping another mapping
107 : */
108 : inline Mapping( const Mapping & rMapping );
109 :
110 : /** Destructor.
111 : */
112 : inline ~Mapping();
113 :
114 : /** Sets a given mapping.
115 :
116 : @param pMapping another mapping
117 : @return this mapping
118 : */
119 : inline Mapping & SAL_CALL operator = ( uno_Mapping * pMapping );
120 : /** Sets a given mapping.
121 :
122 : @param rMapping another mapping
123 : @return this mapping
124 : */
125 78 : inline Mapping & SAL_CALL operator = ( const Mapping & rMapping )
126 78 : { return operator = ( rMapping._pMapping ); }
127 :
128 : /** Provides a pointer to the C mapping. The returned mapping is NOT acquired!
129 :
130 : @return UNacquired C mapping
131 : */
132 158774 : inline uno_Mapping * SAL_CALL get() const
133 158774 : { return _pMapping; }
134 :
135 : /** Tests if a mapping is set.
136 :
137 : @return true if a mapping is set
138 : */
139 53341 : inline bool SAL_CALL is() const
140 53341 : { return (_pMapping != 0); }
141 :
142 : /** Releases a set mapping.
143 : */
144 : inline void SAL_CALL clear();
145 :
146 : /** Maps an interface from one environment to another.
147 :
148 : @param pInterface source interface
149 : @param pTypeDescr type description of interface
150 : @return mapped interface
151 : */
152 : inline void * SAL_CALL mapInterface( void * pInterface, typelib_InterfaceTypeDescription * pTypeDescr ) const;
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 98 : inline void * SAL_CALL mapInterface( void * pInterface, typelib_TypeDescription * pTypeDescr ) const
160 98 : { return mapInterface( pInterface, reinterpret_cast<typelib_InterfaceTypeDescription *>(pTypeDescr) ); }
161 :
162 : /** Maps an interface from one environment to another.
163 :
164 : @param pInterface source interface
165 : @param rType type of interface
166 : @return mapped interface
167 : */
168 : inline void * SAL_CALL mapInterface(
169 : void * pInterface, const ::com::sun::star::uno::Type & rType ) const;
170 :
171 : /** Maps an interface from one environment to another.
172 :
173 : @param ppOut inout mapped interface
174 : @param pInterface source interface
175 : @param pTypeDescr type description of interface
176 : */
177 1096 : inline void SAL_CALL mapInterface( void ** ppOut, void * pInterface, typelib_InterfaceTypeDescription * pTypeDescr ) const
178 1096 : { (*_pMapping->mapInterface)( _pMapping, ppOut, pInterface, pTypeDescr ); }
179 : /** Maps an interface from one environment to another.
180 :
181 : @param ppOut inout mapped interface
182 : @param pInterface source interface
183 : @param pTypeDescr type description of interface
184 : */
185 8265 : inline void SAL_CALL mapInterface( void ** ppOut, void * pInterface, typelib_TypeDescription * pTypeDescr ) const
186 8265 : { (*_pMapping->mapInterface)( _pMapping, ppOut, pInterface, reinterpret_cast<typelib_InterfaceTypeDescription *>(pTypeDescr) ); }
187 :
188 : /** Maps an interface from one environment to another.
189 :
190 : @param ppOut inout mapped interface
191 : @param pInterface source interface
192 : @param rType type of interface to be mapped
193 : */
194 : inline void SAL_CALL mapInterface( void ** ppOut, void * pInterface, const ::com::sun::star::uno::Type & rType ) const;
195 : };
196 :
197 210 : inline Mapping::Mapping(
198 : const ::rtl::OUString & rFrom, const ::rtl::OUString & rTo, const ::rtl::OUString & rAddPurpose )
199 210 : : _pMapping( 0 )
200 : {
201 210 : uno_getMappingByName( &_pMapping, rFrom.pData, rTo.pData, rAddPurpose.pData );
202 210 : }
203 :
204 97550 : inline Mapping::Mapping(
205 : uno_Environment * pFrom, uno_Environment * pTo, const ::rtl::OUString & rAddPurpose )
206 97550 : : _pMapping( 0 )
207 : {
208 97550 : uno_getMapping( &_pMapping, pFrom, pTo, rAddPurpose.pData );
209 97550 : }
210 :
211 32499 : inline Mapping::Mapping(
212 : const Environment & rFrom, const Environment & rTo, const ::rtl::OUString & rAddPurpose )
213 32499 : : _pMapping(0)
214 : {
215 32499 : uno_getMapping( &_pMapping, rFrom.get(), rTo.get(), rAddPurpose.pData );
216 32499 : }
217 :
218 97219 : inline Mapping::Mapping( uno_Mapping * pMapping )
219 97219 : : _pMapping( pMapping )
220 : {
221 97219 : if (_pMapping)
222 96425 : (*_pMapping->acquire)( _pMapping );
223 97219 : }
224 :
225 96425 : inline Mapping::Mapping( const Mapping & rMapping )
226 96425 : : _pMapping( rMapping._pMapping )
227 : {
228 96425 : if (_pMapping)
229 96425 : (*_pMapping->acquire)( _pMapping );
230 96425 : }
231 :
232 323733 : inline Mapping::~Mapping()
233 : {
234 323733 : if (_pMapping)
235 323563 : (*_pMapping->release)( _pMapping );
236 323733 : }
237 :
238 : inline void Mapping::clear()
239 : {
240 : if (_pMapping)
241 : {
242 : (*_pMapping->release)( _pMapping );
243 : _pMapping = 0;
244 : }
245 : }
246 :
247 78 : inline Mapping & Mapping::operator = ( uno_Mapping * pMapping )
248 : {
249 78 : if (pMapping)
250 78 : (*pMapping->acquire)( pMapping );
251 78 : if (_pMapping)
252 0 : (*_pMapping->release)( _pMapping );
253 78 : _pMapping = pMapping;
254 78 : return *this;
255 : }
256 :
257 30462 : inline void Mapping::mapInterface(
258 : void ** ppOut, void * pInterface, const ::com::sun::star::uno::Type & rType ) const
259 : {
260 30462 : typelib_TypeDescription * pTD = 0;
261 30462 : TYPELIB_DANGER_GET( &pTD, rType.getTypeLibType() );
262 30462 : if (pTD)
263 : {
264 30462 : (*_pMapping->mapInterface)( _pMapping, ppOut, pInterface, reinterpret_cast<typelib_InterfaceTypeDescription *>(pTD) );
265 30462 : TYPELIB_DANGER_RELEASE( pTD );
266 : }
267 30462 : }
268 :
269 7909 : inline void * Mapping::mapInterface(
270 : void * pInterface, typelib_InterfaceTypeDescription * pTypeDescr ) const
271 : {
272 7909 : void * pOut = 0;
273 7909 : (*_pMapping->mapInterface)( _pMapping, &pOut, pInterface, pTypeDescr );
274 7909 : return pOut;
275 : }
276 :
277 2278 : inline void * Mapping::mapInterface(
278 : void * pInterface, const ::com::sun::star::uno::Type & rType ) const
279 : {
280 2278 : void * pOut = 0;
281 2278 : mapInterface( &pOut, pInterface, rType );
282 2278 : return pOut;
283 : }
284 :
285 : /** Deprecated. This function DOES NOT WORK with Purpose Environments
286 : (http://wiki.openoffice.org/wiki/Uno/Binary/Spec/Purpose Environments)
287 :
288 : Maps an binary C UNO interface to be used in the currently used compiler environment.
289 :
290 : @tparam C interface type
291 : @param ppRet inout returned interface pointer
292 : @param pUnoI binary C UNO interface
293 : @return true if successful, false otherwise
294 :
295 : @deprecated
296 : */
297 : template< class C >
298 : inline bool mapToCpp( Reference< C > * ppRet, uno_Interface * pUnoI )
299 : {
300 : Mapping aMapping(
301 : ::rtl::OUString( UNO_LB_UNO ),
302 : ::rtl::OUString( CPPU_CURRENT_LANGUAGE_BINDING_NAME ) );
303 : OSL_ASSERT( aMapping.is() );
304 : aMapping.mapInterface(
305 : reinterpret_cast<void **>(ppRet), pUnoI, ::cppu::getTypeFavourUnsigned( ppRet ) );
306 : return (0 != *ppRet);
307 : }
308 : /** Deprecated. This function DOES NOT WORK with Purpose Environments
309 : (http://wiki.openoffice.org/wiki/Uno/Binary/Spec/Purpose Environments)
310 :
311 : Maps an UNO interface of the currently used compiler environment to binary C UNO.
312 :
313 : @tparam C interface type
314 : @param ppRet inout returned interface pointer
315 : @param x interface reference
316 : @return true if successful, false otherwise
317 :
318 : @deprecated
319 : */
320 : template< class C >
321 : inline bool mapToUno( uno_Interface ** ppRet, const Reference< C > & x )
322 : {
323 : Mapping aMapping(
324 : ::rtl::OUString( CPPU_CURRENT_LANGUAGE_BINDING_NAME ),
325 : ::rtl::OUString( UNO_LB_UNO ) );
326 : OSL_ASSERT( aMapping.is() );
327 : aMapping.mapInterface(
328 : reinterpret_cast<void **>(ppRet), x.get(), ::cppu::getTypeFavourUnsigned( &x ) );
329 : return (0 != *ppRet);
330 : }
331 :
332 : }
333 : }
334 : }
335 : }
336 :
337 : #endif
338 :
339 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|