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