1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 * This file is part of LibreOffice published API.
23 #ifndef INCLUDED_UNO_MAPPING_HXX
24 #define INCLUDED_UNO_MAPPING_HXX
26 #include "sal/config.h"
30 #include "uno/lbnames.h"
31 #include "rtl/alloc.h"
32 #include "rtl/ustring.hxx"
33 #include "osl/diagnose.h"
34 #include "uno/mapping.h"
35 #include "com/sun/star/uno/Type.hxx"
36 #include "com/sun/star/uno/Reference.hxx"
37 #include "cppu/unotype.hxx"
38 #include "uno/environment.hxx"
40 typedef struct _typelib_TypeDescription typelib_TypeDescription
;
41 typedef struct _typelib_InterfaceTypeDescription typelib_InterfaceTypeDescription
;
42 typedef struct _uno_Interface uno_Interface
;
53 /** C++ wrapper for C uno_Mapping.
59 uno_Mapping
* _pMapping
;
62 // these are here to force memory de/allocation to sal lib.
64 static void * SAL_CALL
operator new ( size_t nSize
)
65 { return ::rtl_allocateMemory( nSize
); }
66 static void SAL_CALL
operator delete ( void * pMem
)
67 { ::rtl_freeMemory( pMem
); }
68 static void * SAL_CALL
operator new ( size_t, void * pMem
)
70 static void SAL_CALL
operator delete ( void *, void * )
74 /** Holds a mapping from the specified source to the specified destination by environment
77 @param rFrom type name of source environment
78 @param rTo type name of destination environment
79 @param rAddPurpose additional purpose
82 const ::rtl::OUString
& rFrom
, const ::rtl::OUString
& rTo
,
83 const ::rtl::OUString
& rAddPurpose
= ::rtl::OUString() );
85 /** Holds a mapping from the specified source to the specified destination.
87 @param pFrom source environment
88 @param pTo destination environment
89 @param rAddPurpose additional purpose
92 uno_Environment
* pFrom
, uno_Environment
* pTo
,
93 const ::rtl::OUString
& rAddPurpose
= ::rtl::OUString() );
95 /** Holds a mapping from the specified source to the specified destination
98 @param rFrom source environment
99 @param rTo destination environment
100 @param rAddPurpose additional purpose
102 inline Mapping(const Environment
& rFrom
, const Environment
& rTo
,
103 const ::rtl::OUString
& rAddPurpose
= ::rtl::OUString() );
107 @param pMapping another mapping
109 inline Mapping( uno_Mapping
* pMapping
= NULL
);
111 /** Copy constructor.
113 @param rMapping another mapping
115 inline Mapping( const Mapping
& rMapping
);
117 #if defined LIBO_INTERNAL_ONLY
118 Mapping(Mapping
&& other
) noexcept
: _pMapping(other
._pMapping
)
119 { other
._pMapping
= nullptr; }
126 /** Sets a given mapping.
128 @param pMapping another mapping
131 inline Mapping
& SAL_CALL
operator = ( uno_Mapping
* pMapping
);
132 /** Sets a given mapping.
134 @param rMapping another mapping
137 Mapping
& SAL_CALL
operator = ( const Mapping
& rMapping
)
138 { return operator = ( rMapping
._pMapping
); }
140 #if defined LIBO_INTERNAL_ONLY
141 Mapping
& operator =(Mapping
&& other
) noexcept
{
142 if (_pMapping
!= nullptr) {
143 (*_pMapping
->release
)(_pMapping
);
145 _pMapping
= other
._pMapping
;
146 other
._pMapping
= nullptr;
151 /** Provides a pointer to the C mapping. The returned mapping is NOT acquired!
153 @return UNacquired C mapping
155 uno_Mapping
* SAL_CALL
get() const
156 { return _pMapping
; }
158 /** Tests if a mapping is set.
160 @return true if a mapping is set
162 bool SAL_CALL
is() const
163 { return (_pMapping
!= NULL
); }
165 /** Releases a set mapping.
167 inline void SAL_CALL
clear();
169 /** Maps an interface from one environment to another.
171 @param pInterface source interface
172 @param pTypeDescr type description of interface
173 @return mapped interface
175 inline void * SAL_CALL
mapInterface( void * pInterface
, typelib_InterfaceTypeDescription
* pTypeDescr
) const;
176 /** Maps an interface from one environment to another.
178 @param pInterface source interface
179 @param pTypeDescr type description of interface
180 @return mapped interface
182 void * SAL_CALL
mapInterface( void * pInterface
, typelib_TypeDescription
* pTypeDescr
) const
183 { return mapInterface( pInterface
, reinterpret_cast<typelib_InterfaceTypeDescription
*>(pTypeDescr
) ); }
185 /** Maps an interface from one environment to another.
187 @param pInterface source interface
188 @param rType type of interface
189 @return mapped interface
191 inline void * SAL_CALL
mapInterface(
192 void * pInterface
, const css::uno::Type
& rType
) const;
194 /** Maps an interface from one environment to another.
196 @param ppOut inout mapped interface
197 @param pInterface source interface
198 @param pTypeDescr type description of interface
200 void SAL_CALL
mapInterface( void ** ppOut
, void * pInterface
, typelib_InterfaceTypeDescription
* pTypeDescr
) const
201 { (*_pMapping
->mapInterface
)( _pMapping
, ppOut
, pInterface
, pTypeDescr
); }
202 /** Maps an interface from one environment to another.
204 @param ppOut inout mapped interface
205 @param pInterface source interface
206 @param pTypeDescr type description of interface
208 void SAL_CALL
mapInterface( void ** ppOut
, void * pInterface
, typelib_TypeDescription
* pTypeDescr
) const
209 { (*_pMapping
->mapInterface
)( _pMapping
, ppOut
, pInterface
, reinterpret_cast<typelib_InterfaceTypeDescription
*>(pTypeDescr
) ); }
211 /** Maps an interface from one environment to another.
213 @param ppOut inout mapped interface
214 @param pInterface source interface
215 @param rType type of interface to be mapped
217 inline void SAL_CALL
mapInterface( void ** ppOut
, void * pInterface
, const css::uno::Type
& rType
) const;
220 inline Mapping::Mapping(
221 const ::rtl::OUString
& rFrom
, const ::rtl::OUString
& rTo
, const ::rtl::OUString
& rAddPurpose
)
224 uno_getMappingByName( &_pMapping
, rFrom
.pData
, rTo
.pData
, rAddPurpose
.pData
);
227 inline Mapping::Mapping(
228 uno_Environment
* pFrom
, uno_Environment
* pTo
, const ::rtl::OUString
& rAddPurpose
)
231 uno_getMapping( &_pMapping
, pFrom
, pTo
, rAddPurpose
.pData
);
234 inline Mapping::Mapping(
235 const Environment
& rFrom
, const Environment
& rTo
, const ::rtl::OUString
& rAddPurpose
)
238 uno_getMapping( &_pMapping
, rFrom
.get(), rTo
.get(), rAddPurpose
.pData
);
241 inline Mapping::Mapping( uno_Mapping
* pMapping
)
242 : _pMapping( pMapping
)
245 (*_pMapping
->acquire
)( _pMapping
);
248 inline Mapping::Mapping( const Mapping
& rMapping
)
249 : _pMapping( rMapping
._pMapping
)
252 (*_pMapping
->acquire
)( _pMapping
);
255 inline Mapping::~Mapping()
258 (*_pMapping
->release
)( _pMapping
);
261 inline void Mapping::clear()
265 (*_pMapping
->release
)( _pMapping
);
270 inline Mapping
& Mapping::operator = ( uno_Mapping
* pMapping
)
273 (*pMapping
->acquire
)( pMapping
);
275 (*_pMapping
->release
)( _pMapping
);
276 _pMapping
= pMapping
;
280 inline void Mapping::mapInterface(
281 void ** ppOut
, void * pInterface
, const css::uno::Type
& rType
) const
283 typelib_TypeDescription
* pTD
= NULL
;
284 TYPELIB_DANGER_GET( &pTD
, rType
.getTypeLibType() );
287 (*_pMapping
->mapInterface
)( _pMapping
, ppOut
, pInterface
, reinterpret_cast<typelib_InterfaceTypeDescription
*>(pTD
) );
288 TYPELIB_DANGER_RELEASE( pTD
);
292 inline void * Mapping::mapInterface(
293 void * pInterface
, typelib_InterfaceTypeDescription
* pTypeDescr
) const
296 (*_pMapping
->mapInterface
)( _pMapping
, &pOut
, pInterface
, pTypeDescr
);
300 inline void * Mapping::mapInterface(
301 void * pInterface
, const css::uno::Type
& rType
) const
304 mapInterface( &pOut
, pInterface
, rType
);
308 /** Deprecated. This function DOES NOT WORK with Purpose Environments
309 (http://wiki.openoffice.org/wiki/Uno/Binary/Spec/Purpose Environments)
311 Maps a binary C UNO interface to be used in the currently used compiler environment.
313 @tparam C interface type
314 @param ppRet inout returned interface pointer
315 @param pUnoI binary C UNO interface
316 @return true if successful, false otherwise
321 SAL_DEPRECATED("use uno_Mapping")
322 inline bool mapToCpp( Reference
< C
> * ppRet
, uno_Interface
* pUnoI
)
324 Mapping
aMapping( UNO_LB_UNO
, CPPU_CURRENT_LANGUAGE_BINDING_NAME
);
325 OSL_ASSERT( aMapping
.is() );
326 aMapping
.mapInterface(
327 reinterpret_cast<void **>(ppRet
), pUnoI
, ::cppu::getTypeFavourUnsigned( ppRet
) );
328 return (0 != *ppRet
);
330 /** Deprecated. This function DOES NOT WORK with Purpose Environments
331 (http://wiki.openoffice.org/wiki/Uno/Binary/Spec/Purpose Environments)
333 Maps a UNO interface of the currently used compiler environment to binary C UNO.
335 @tparam C interface type
336 @param ppRet inout returned interface pointer
337 @param x interface reference
338 @return true if successful, false otherwise
343 SAL_DEPRECATED("use uno_Mapping")
344 inline bool mapToUno( uno_Interface
** ppRet
, const Reference
< C
> & x
)
346 Mapping
aMapping( CPPU_CURRENT_LANGUAGE_BINDING_NAME
, UNO_LB_UNO
);
347 OSL_ASSERT( aMapping
.is() );
348 aMapping
.mapInterface(
349 reinterpret_cast<void **>(ppRet
), x
.get(), ::cppu::getTypeFavourUnsigned( &x
) );
350 return (NULL
!= *ppRet
);
360 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */