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 .
19 // #define TEST_LIST_CLASSES
21 #ifndef INCLUDED_STOC_SOURCE_COREREFLECTION_BASE_HXX
22 #define INCLUDED_STOC_SOURCE_COREREFLECTION_BASE_HXX
24 #include <sal/config.h>
26 #include <o3tl/any.hxx>
27 #include <osl/diagnose.h>
28 #include <osl/mutex.hxx>
29 #include <uno/mapping.hxx>
30 #include <uno/dispatcher.h>
31 #include <cppuhelper/implbase.hxx>
32 #include <cppuhelper/weak.hxx>
33 #include <cppuhelper/factory.hxx>
34 #include <cppuhelper/component.hxx>
35 #include <cppuhelper/typeprovider.hxx>
36 #include <rtl/ustring.hxx>
37 #include <rtl/ref.hxx>
39 #include "lrucache.hxx"
41 #ifdef TEST_LIST_CLASSES
45 #include <unordered_map>
48 #include <com/sun/star/uno/XComponentContext.hpp>
49 #include <com/sun/star/lang/XServiceInfo.hpp>
50 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
52 #include <com/sun/star/reflection/XIdlClass.hpp>
53 #include <com/sun/star/reflection/XIdlReflection.hpp>
54 #include <com/sun/star/reflection/XIdlField.hpp>
55 #include <com/sun/star/reflection/XIdlField2.hpp>
56 #include <com/sun/star/reflection/XIdlMethod.hpp>
61 #ifdef TEST_LIST_CLASSES
62 typedef std::vector
< OUString
> ClassNameVector
;
63 extern ClassNameVector g_aClassNames
;
67 ::osl::Mutex
& getMutexAccess();
70 inline bool td_equals( typelib_TypeDescription
* pTD
, typelib_TypeDescriptionReference
* pType
)
72 return (pTD
->pWeakRef
== pType
||
73 (pTD
->pTypeName
->length
== pType
->pTypeName
->length
&&
74 rtl_ustr_compare( pTD
->pTypeName
->buffer
, pType
->pTypeName
->buffer
) == 0));
77 typedef std::unordered_map
< OUString
, css::uno::WeakReference
< css::reflection::XIdlField
> > OUString2Field
;
78 typedef std::unordered_map
< OUString
, css::uno::WeakReference
< css::reflection::XIdlMethod
> > OUString2Method
;
81 class IdlReflectionServiceImpl
82 : public ::cppu::OComponentHelper
83 , public css::reflection::XIdlReflection
84 , public css::container::XHierarchicalNameAccess
85 , public css::lang::XServiceInfo
87 ::osl::Mutex _aComponentMutex
;
88 css::uno::Reference
< css::container::XHierarchicalNameAccess
> _xTDMgr
;
91 LRU_CacheAnyByOUString _aElements
;
93 css::uno::Mapping _aCpp2Uno
;
94 css::uno::Mapping _aUno2Cpp
;
96 inline css::uno::Reference
< css::reflection::XIdlClass
> constructClass( typelib_TypeDescription
* pTypeDescr
);
99 /// @throws css::uno::RuntimeException
100 const css::uno::Mapping
& getCpp2Uno();
101 /// @throws css::uno::RuntimeException
102 const css::uno::Mapping
& getUno2Cpp();
103 /// @throws css::uno::RuntimeException
104 uno_Interface
* mapToUno( const css::uno::Any
& rObj
, typelib_InterfaceTypeDescription
* pTo
);
107 explicit IdlReflectionServiceImpl( const css::uno::Reference
< css::uno::XComponentContext
> & xContext
);
108 virtual ~IdlReflectionServiceImpl() override
;
111 virtual css::uno::Any SAL_CALL
queryInterface( const css::uno::Type
& rType
) override
;
112 virtual void SAL_CALL
acquire() throw() override
;
113 virtual void SAL_CALL
release() throw() override
;
115 // some XComponent part from OComponentHelper
116 virtual void SAL_CALL
dispose() override
;
119 virtual OUString SAL_CALL
getImplementationName() override
;
120 virtual sal_Bool SAL_CALL
supportsService( const OUString
& rServiceName
) override
;
121 virtual css::uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() override
;
124 virtual css::uno::Sequence
< css::uno::Type
> SAL_CALL
getTypes() override
;
125 virtual css::uno::Sequence
< sal_Int8
> SAL_CALL
getImplementationId() override
;
128 virtual css::uno::Reference
< css::reflection::XIdlClass
> SAL_CALL
forName( const OUString
& rTypeName
) override
;
129 virtual css::uno::Reference
< css::reflection::XIdlClass
> SAL_CALL
getType( const css::uno::Any
& rObj
) override
;
131 // XHierarchicalNameAccess
132 virtual css::uno::Any SAL_CALL
getByHierarchicalName( const OUString
& rName
) override
;
133 virtual sal_Bool SAL_CALL
hasByHierarchicalName( const OUString
& rName
) override
;
135 /// @throws css::uno::RuntimeException
136 css::uno::Reference
< css::reflection::XIdlClass
> forType( typelib_TypeDescription
* pTypeDescr
);
137 /// @throws css::uno::RuntimeException
138 css::uno::Reference
< css::reflection::XIdlClass
> forType( typelib_TypeDescriptionReference
* pRef
);
143 : public ::cppu::WeakImplHelper
< css::reflection::XIdlClass
>
145 rtl::Reference
<IdlReflectionServiceImpl
>
149 css::uno::TypeClass _eTypeClass
;
151 typelib_TypeDescription
* _pTypeDescr
;
154 typelib_TypeDescription
* getTypeDescr() const
155 { return _pTypeDescr
; }
156 IdlReflectionServiceImpl
* getReflection() const
157 { return m_xReflection
.get(); }
160 IdlClassImpl( IdlReflectionServiceImpl
* pReflection
,
161 const OUString
& rName
, typelib_TypeClass eTypeClass
,
162 typelib_TypeDescription
* pTypeDescr
);
163 virtual ~IdlClassImpl() override
;
165 // XIdlClassImpl default implementation
166 virtual css::uno::TypeClass SAL_CALL
getTypeClass() override
;
167 virtual OUString SAL_CALL
getName() override
;
168 virtual sal_Bool SAL_CALL
equals( const css::uno::Reference
< css::reflection::XIdlClass
>& xType
) override
;
170 virtual sal_Bool SAL_CALL
isAssignableFrom( const css::uno::Reference
< css::reflection::XIdlClass
> & xType
) override
;
171 virtual void SAL_CALL
createObject( css::uno::Any
& rObj
) override
;
174 virtual css::uno::Sequence
< css::uno::Reference
< css::reflection::XIdlClass
> > SAL_CALL
getClasses() override
;
175 virtual css::uno::Reference
< css::reflection::XIdlClass
> SAL_CALL
getClass( const OUString
& rName
) override
;
176 virtual css::uno::Sequence
< css::uno::Reference
< css::reflection::XIdlClass
> > SAL_CALL
getInterfaces() override
;
178 // structs, interfaces
179 virtual css::uno::Sequence
< css::uno::Reference
< css::reflection::XIdlClass
> > SAL_CALL
getSuperclasses() override
;
181 virtual css::uno::Reference
< css::reflection::XIdlField
> SAL_CALL
getField( const OUString
& rName
) override
;
182 virtual css::uno::Sequence
< css::uno::Reference
< css::reflection::XIdlField
> > SAL_CALL
getFields() override
;
184 virtual css::uno::Uik SAL_CALL
getUik() override
;
185 virtual css::uno::Reference
< css::reflection::XIdlMethod
> SAL_CALL
getMethod( const OUString
& rName
) override
;
186 virtual css::uno::Sequence
< css::uno::Reference
< css::reflection::XIdlMethod
> > SAL_CALL
getMethods() override
;
188 virtual css::uno::Reference
< css::reflection::XIdlClass
> SAL_CALL
getComponentType() override
;
189 virtual css::uno::Reference
< css::reflection::XIdlArray
> SAL_CALL
getArray() override
;
193 class InterfaceIdlClassImpl
194 : public IdlClassImpl
196 typedef std::pair
< OUString
, typelib_TypeDescription
* > MemberInit
;
198 css::uno::Sequence
< css::uno::Reference
< css::reflection::XIdlClass
> > _xSuperClasses
;
200 std::unique_ptr
<MemberInit
[]> _pSortedMemberInit
; // first methods, then attributes
201 OUString2Field _aName2Field
;
202 OUString2Method _aName2Method
;
204 sal_Int32 _nAttributes
;
209 typelib_InterfaceTypeDescription
* getTypeDescr() const
210 { return reinterpret_cast<typelib_InterfaceTypeDescription
*>(IdlClassImpl::getTypeDescr()); }
213 InterfaceIdlClassImpl( IdlReflectionServiceImpl
* pReflection
,
214 const OUString
& rName
, typelib_TypeClass eTypeClass
,
215 typelib_TypeDescription
* pTypeDescr
)
216 : IdlClassImpl( pReflection
, rName
, eTypeClass
, pTypeDescr
)
217 , _pSortedMemberInit( nullptr )
221 virtual ~InterfaceIdlClassImpl() override
;
223 // IdlClassImpl modifications
224 virtual sal_Bool SAL_CALL
isAssignableFrom( const css::uno::Reference
< css::reflection::XIdlClass
> & xType
) override
;
225 virtual css::uno::Sequence
< css::uno::Reference
< css::reflection::XIdlClass
> > SAL_CALL
getSuperclasses() override
;
226 virtual css::uno::Uik SAL_CALL
getUik() override
;
227 virtual css::uno::Reference
< css::reflection::XIdlMethod
> SAL_CALL
getMethod( const OUString
& rName
) override
;
228 virtual css::uno::Sequence
< css::uno::Reference
< css::reflection::XIdlMethod
> > SAL_CALL
getMethods() override
;
229 virtual css::uno::Reference
< css::reflection::XIdlField
> SAL_CALL
getField( const OUString
& rName
) override
;
230 virtual css::uno::Sequence
< css::uno::Reference
< css::reflection::XIdlField
> > SAL_CALL
getFields() override
;
231 virtual void SAL_CALL
createObject( css::uno::Any
& rObj
) override
;
235 class CompoundIdlClassImpl
236 : public IdlClassImpl
238 css::uno::Reference
< css::reflection::XIdlClass
>
240 std::unique_ptr
< css::uno::Sequence
< css::uno::Reference
< css::reflection::XIdlField
> > >
242 OUString2Field _aName2Field
;
245 typelib_CompoundTypeDescription
* getTypeDescr() const
246 { return reinterpret_cast<typelib_CompoundTypeDescription
*>(IdlClassImpl::getTypeDescr()); }
249 CompoundIdlClassImpl( IdlReflectionServiceImpl
* pReflection
,
250 const OUString
& rName
, typelib_TypeClass eTypeClass
,
251 typelib_TypeDescription
* pTypeDescr
)
252 : IdlClassImpl( pReflection
, rName
, eTypeClass
, pTypeDescr
)
253 , _pFields( nullptr )
255 virtual ~CompoundIdlClassImpl() override
;
257 // IdlClassImpl modifications
258 virtual sal_Bool SAL_CALL
isAssignableFrom( const css::uno::Reference
< css::reflection::XIdlClass
> & xType
) override
;
259 virtual css::uno::Sequence
< css::uno::Reference
< css::reflection::XIdlClass
> > SAL_CALL
getSuperclasses() override
;
260 virtual css::uno::Reference
< css::reflection::XIdlField
> SAL_CALL
getField( const OUString
& rName
) override
;
261 virtual css::uno::Sequence
< css::uno::Reference
< css::reflection::XIdlField
> > SAL_CALL
getFields() override
;
265 class ArrayIdlClassImpl
266 : public IdlClassImpl
267 , public css::reflection::XIdlArray
270 typelib_IndirectTypeDescription
* getTypeDescr() const
271 { return reinterpret_cast<typelib_IndirectTypeDescription
*>(IdlClassImpl::getTypeDescr()); }
274 ArrayIdlClassImpl( IdlReflectionServiceImpl
* pReflection
,
275 const OUString
& rName
, typelib_TypeClass eTypeClass
,
276 typelib_TypeDescription
* pTypeDescr
)
277 : IdlClassImpl( pReflection
, rName
, eTypeClass
, pTypeDescr
)
280 virtual css::uno::Any SAL_CALL
queryInterface( const css::uno::Type
& rType
) override
;
281 virtual void SAL_CALL
acquire() throw() override
;
282 virtual void SAL_CALL
release() throw() override
;
285 virtual css::uno::Sequence
< css::uno::Type
> SAL_CALL
getTypes() override
;
286 virtual css::uno::Sequence
< sal_Int8
> SAL_CALL
getImplementationId() override
;
288 // IdlClassImpl modifications
289 virtual sal_Bool SAL_CALL
isAssignableFrom( const css::uno::Reference
< css::reflection::XIdlClass
> & xType
) override
;
290 virtual css::uno::Reference
< css::reflection::XIdlClass
> SAL_CALL
getComponentType() override
;
291 virtual css::uno::Reference
< css::reflection::XIdlArray
> SAL_CALL
getArray() override
;
294 virtual void SAL_CALL
realloc( css::uno::Any
& rArray
, sal_Int32 nLen
) override
;
295 virtual sal_Int32 SAL_CALL
getLen( const css::uno::Any
& rArray
) override
;
296 virtual css::uno::Any SAL_CALL
get( const css::uno::Any
& rArray
, sal_Int32 nIndex
) override
;
297 virtual void SAL_CALL
set( css::uno::Any
& rArray
, sal_Int32 nIndex
, const css::uno::Any
& rNewValue
) override
;
301 class EnumIdlClassImpl
302 : public IdlClassImpl
304 std::unique_ptr
< css::uno::Sequence
< css::uno::Reference
< css::reflection::XIdlField
> > > _pFields
;
305 OUString2Field _aName2Field
;
308 typelib_EnumTypeDescription
* getTypeDescr() const
309 { return reinterpret_cast<typelib_EnumTypeDescription
*>(IdlClassImpl::getTypeDescr()); }
312 EnumIdlClassImpl( IdlReflectionServiceImpl
* pReflection
,
313 const OUString
& rName
, typelib_TypeClass eTypeClass
,
314 typelib_TypeDescription
* pTypeDescr
)
315 : IdlClassImpl( pReflection
, rName
, eTypeClass
, pTypeDescr
)
316 , _pFields( nullptr )
318 virtual ~EnumIdlClassImpl() override
;
320 // IdlClassImpl modifications
321 virtual css::uno::Reference
< css::reflection::XIdlField
> SAL_CALL
getField( const OUString
& rName
) override
;
322 virtual css::uno::Sequence
< css::uno::Reference
< css::reflection::XIdlField
> > SAL_CALL
getFields() override
;
323 virtual void SAL_CALL
createObject( css::uno::Any
& rObj
) override
;
328 : public ::cppu::WeakImplHelper
< css::reflection::XIdlMember
>
330 rtl::Reference
<IdlReflectionServiceImpl
>
334 typelib_TypeDescription
* _pTypeDescr
;
335 typelib_TypeDescription
* _pDeclTypeDescr
;
338 css::uno::Reference
< css::reflection::XIdlClass
> _xDeclClass
;
341 IdlReflectionServiceImpl
* getReflection() const
342 { return m_xReflection
.get(); }
343 typelib_TypeDescription
* getTypeDescr() const
344 { return _pTypeDescr
; }
345 typelib_TypeDescription
* getDeclTypeDescr() const
346 { return _pDeclTypeDescr
; }
349 IdlMemberImpl( IdlReflectionServiceImpl
* pReflection
, const OUString
& rName
,
350 typelib_TypeDescription
* pTypeDescr
, typelib_TypeDescription
* pDeclTypeDescr
);
351 virtual ~IdlMemberImpl() override
;
354 virtual css::uno::Reference
< css::reflection::XIdlClass
> SAL_CALL
getDeclaringClass() override
;
355 virtual OUString SAL_CALL
getName() override
;
359 // coerces to type descr pTo else queries for it: the interface pointer is returned via rDest
360 // ## type to XidlClass coercion possible
362 const css::uno::Any
& rObj
, typelib_InterfaceTypeDescription
* pTo
,
363 css::uno::Reference
< css::uno::XInterface
> & rDest
,
364 IdlReflectionServiceImpl
* pRefl
)
369 if (! rObj
.hasValue())
371 if (rObj
.getValueTypeClass() == css::uno::TypeClass_INTERFACE
)
373 return ::uno_type_assignData(
374 &rDest
, pTo
->aBase
.pWeakRef
,
375 const_cast< void * >( rObj
.getValue() ), rObj
.getValueTypeRef(),
376 reinterpret_cast< uno_QueryInterfaceFunc
>(css::uno::cpp_queryInterface
),
377 reinterpret_cast< uno_AcquireFunc
>(css::uno::cpp_acquire
),
378 reinterpret_cast< uno_ReleaseFunc
>(css::uno::cpp_release
) );
380 else if (auto t
= o3tl::tryAccess
<css::uno::Type
>(rObj
))
382 rDest
= pRefl
->forType( t
->getTypeLibType() );
389 inline bool coerce_assign(
390 void * pDest
, typelib_TypeDescription
* pTD
, const css::uno::Any
& rSource
,
391 IdlReflectionServiceImpl
* pRefl
)
393 if (pTD
->eTypeClass
== typelib_TypeClass_INTERFACE
)
395 css::uno::Reference
< css::uno::XInterface
> xVal
;
396 if (extract( rSource
, reinterpret_cast<typelib_InterfaceTypeDescription
*>(pTD
), xVal
, pRefl
))
398 if (*static_cast<css::uno::XInterface
**>(pDest
))
399 (*static_cast<css::uno::XInterface
**>(pDest
))->release();
400 *static_cast<css::uno::XInterface
**>(pDest
) = xVal
.get();
401 if (*static_cast<css::uno::XInterface
**>(pDest
))
402 (*static_cast<css::uno::XInterface
**>(pDest
))->acquire();
407 else if (pTD
->eTypeClass
== typelib_TypeClass_ANY
)
409 return uno_assignData(
411 const_cast<css::uno::Any
*>(&rSource
), pTD
,
412 reinterpret_cast< uno_QueryInterfaceFunc
>(css::uno::cpp_queryInterface
),
413 reinterpret_cast< uno_AcquireFunc
>(css::uno::cpp_acquire
),
414 reinterpret_cast< uno_ReleaseFunc
>(css::uno::cpp_release
) );
418 return uno_type_assignData(
419 pDest
, pTD
->pWeakRef
,
420 const_cast<void *>(rSource
.getValue()), rSource
.getValueTypeRef(),
421 reinterpret_cast< uno_QueryInterfaceFunc
>(css::uno::cpp_queryInterface
),
422 reinterpret_cast< uno_AcquireFunc
>(css::uno::cpp_acquire
),
423 reinterpret_cast< uno_ReleaseFunc
>(css::uno::cpp_release
) );
430 #endif // INCLUDED_STOC_SOURCE_COREREFLECTION_BASE_HXX
432 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */