nss: upgrade to release 3.73
[LibreOffice.git] / stoc / source / corereflection / base.hxx
blob02742fdb660ecb9f0b50b9e750ab9803987d98c5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/mutex.hxx>
28 #include <uno/mapping.hxx>
29 #include <uno/dispatcher.h>
30 #include <cppuhelper/implbase.hxx>
31 #include <cppuhelper/component.hxx>
32 #include <rtl/ustring.hxx>
33 #include <rtl/ref.hxx>
35 #include "lrucache.hxx"
37 #ifdef TEST_LIST_CLASSES
38 #include <vector>
39 #include <algorithm>
40 #endif
41 #include <unordered_map>
42 #include <memory>
44 #include <com/sun/star/lang/XServiceInfo.hpp>
45 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
47 #include <com/sun/star/reflection/XIdlReflection.hpp>
49 namespace com::sun::star::uno { class XComponentContext; }
50 namespace com::sun::star::reflection { class XIdlClass; }
51 namespace com::sun::star::reflection { class XIdlField; }
52 namespace com::sun::star::reflection { class XIdlMethod; }
54 namespace stoc_corefl
57 #ifdef TEST_LIST_CLASSES
58 extern std::vector<OUString> g_aClassNames;
59 #endif
62 ::osl::Mutex & getMutexAccess();
65 inline bool td_equals( typelib_TypeDescription * pTD, typelib_TypeDescriptionReference * pType )
67 return (pTD->pWeakRef == pType ||
68 (pTD->pTypeName->length == pType->pTypeName->length &&
69 rtl_ustr_compare( pTD->pTypeName->buffer, pType->pTypeName->buffer ) == 0));
72 typedef std::unordered_map< OUString, css::uno::WeakReference< css::reflection::XIdlField > > OUString2Field;
73 typedef std::unordered_map< OUString, css::uno::WeakReference< css::reflection::XIdlMethod > > OUString2Method;
76 class IdlReflectionServiceImpl
77 : public ::cppu::OComponentHelper
78 , public css::reflection::XIdlReflection
79 , public css::container::XHierarchicalNameAccess
80 , public css::lang::XServiceInfo
82 ::osl::Mutex _aComponentMutex;
83 css::uno::Reference< css::container::XHierarchicalNameAccess > _xTDMgr;
85 // caching
86 LRU_CacheAnyByOUString _aElements;
88 css::uno::Mapping _aCpp2Uno;
89 css::uno::Mapping _aUno2Cpp;
91 inline css::uno::Reference< css::reflection::XIdlClass > constructClass( typelib_TypeDescription * pTypeDescr );
93 public:
94 /// @throws css::uno::RuntimeException
95 const css::uno::Mapping & getCpp2Uno();
96 /// @throws css::uno::RuntimeException
97 const css::uno::Mapping & getUno2Cpp();
98 /// @throws css::uno::RuntimeException
99 uno_Interface * mapToUno( const css::uno::Any & rObj, typelib_InterfaceTypeDescription * pTo );
101 // ctor/ dtor
102 explicit IdlReflectionServiceImpl( const css::uno::Reference< css::uno::XComponentContext > & xContext );
103 virtual ~IdlReflectionServiceImpl() override;
105 // XInterface
106 virtual css::uno::Any SAL_CALL queryInterface( const css::uno::Type & rType ) override;
107 virtual void SAL_CALL acquire() throw() override;
108 virtual void SAL_CALL release() throw() override;
110 // some XComponent part from OComponentHelper
111 virtual void SAL_CALL dispose() override;
113 // XServiceInfo
114 virtual OUString SAL_CALL getImplementationName() override;
115 virtual sal_Bool SAL_CALL supportsService( const OUString & rServiceName ) override;
116 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
118 // XTypeProvider
119 virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override;
120 virtual css::uno::Sequence< sal_Int8 > SAL_CALL getImplementationId() override;
122 // XIdlReflection
123 virtual css::uno::Reference< css::reflection::XIdlClass > SAL_CALL forName( const OUString & rTypeName ) override;
124 virtual css::uno::Reference< css::reflection::XIdlClass > SAL_CALL getType( const css::uno::Any & rObj ) override;
126 // XHierarchicalNameAccess
127 virtual css::uno::Any SAL_CALL getByHierarchicalName( const OUString & rName ) override;
128 virtual sal_Bool SAL_CALL hasByHierarchicalName( const OUString & rName ) override;
130 /// @throws css::uno::RuntimeException
131 css::uno::Reference< css::reflection::XIdlClass > forType( typelib_TypeDescription * pTypeDescr );
132 /// @throws css::uno::RuntimeException
133 css::uno::Reference< css::reflection::XIdlClass > forType( typelib_TypeDescriptionReference * pRef );
137 class IdlClassImpl
138 : public ::cppu::WeakImplHelper< css::reflection::XIdlClass >
140 rtl::Reference<IdlReflectionServiceImpl>
141 m_xReflection;
143 OUString _aName;
144 css::uno::TypeClass _eTypeClass;
146 typelib_TypeDescription * _pTypeDescr;
148 public:
149 typelib_TypeDescription * getTypeDescr() const
150 { return _pTypeDescr; }
151 IdlReflectionServiceImpl * getReflection() const
152 { return m_xReflection.get(); }
154 // Ctor
155 IdlClassImpl( IdlReflectionServiceImpl * pReflection,
156 const OUString & rName, typelib_TypeClass eTypeClass,
157 typelib_TypeDescription * pTypeDescr );
158 virtual ~IdlClassImpl() override;
160 // XIdlClassImpl default implementation
161 virtual css::uno::TypeClass SAL_CALL getTypeClass() override;
162 virtual OUString SAL_CALL getName() override;
163 virtual sal_Bool SAL_CALL equals( const css::uno::Reference< css::reflection::XIdlClass >& xType ) override;
165 virtual sal_Bool SAL_CALL isAssignableFrom( const css::uno::Reference< css::reflection::XIdlClass > & xType ) override;
166 virtual void SAL_CALL createObject( css::uno::Any & rObj ) override;
168 // def impl ????
169 virtual css::uno::Sequence< css::uno::Reference< css::reflection::XIdlClass > > SAL_CALL getClasses() override;
170 virtual css::uno::Reference< css::reflection::XIdlClass > SAL_CALL getClass( const OUString & rName ) override;
171 virtual css::uno::Sequence< css::uno::Reference< css::reflection::XIdlClass > > SAL_CALL getInterfaces() override;
173 // structs, interfaces
174 virtual css::uno::Sequence< css::uno::Reference< css::reflection::XIdlClass > > SAL_CALL getSuperclasses() override;
175 // structs
176 virtual css::uno::Reference< css::reflection::XIdlField > SAL_CALL getField( const OUString & rName ) override;
177 virtual css::uno::Sequence< css::uno::Reference< css::reflection::XIdlField > > SAL_CALL getFields() override;
178 // interfaces
179 virtual css::uno::Uik SAL_CALL getUik() override;
180 virtual css::uno::Reference< css::reflection::XIdlMethod > SAL_CALL getMethod( const OUString & rName ) override;
181 virtual css::uno::Sequence< css::uno::Reference< css::reflection::XIdlMethod > > SAL_CALL getMethods() override;
182 // array
183 virtual css::uno::Reference< css::reflection::XIdlClass > SAL_CALL getComponentType() override;
184 virtual css::uno::Reference< css::reflection::XIdlArray > SAL_CALL getArray() override;
188 class InterfaceIdlClassImpl
189 : public IdlClassImpl
191 typedef std::pair< OUString, typelib_TypeDescription * > MemberInit;
193 css::uno::Sequence< css::uno::Reference< css::reflection::XIdlClass > > _xSuperClasses;
195 std::unique_ptr<MemberInit[]> _pSortedMemberInit; // first methods, then attributes
196 OUString2Field _aName2Field;
197 OUString2Method _aName2Method;
198 sal_Int32 _nMethods;
199 sal_Int32 _nAttributes;
201 void initMembers();
203 public:
204 typelib_InterfaceTypeDescription * getTypeDescr() const
205 { return reinterpret_cast<typelib_InterfaceTypeDescription *>(IdlClassImpl::getTypeDescr()); }
207 // ctor/ dtor
208 InterfaceIdlClassImpl( IdlReflectionServiceImpl * pReflection,
209 const OUString & rName, typelib_TypeClass eTypeClass,
210 typelib_TypeDescription * pTypeDescr )
211 : IdlClassImpl( pReflection, rName, eTypeClass, pTypeDescr )
212 , _nMethods( 0 )
213 , _nAttributes( 0 )
215 virtual ~InterfaceIdlClassImpl() override;
217 // IdlClassImpl modifications
218 virtual sal_Bool SAL_CALL isAssignableFrom( const css::uno::Reference< css::reflection::XIdlClass > & xType ) override;
219 virtual css::uno::Sequence< css::uno::Reference< css::reflection::XIdlClass > > SAL_CALL getSuperclasses() override;
220 virtual css::uno::Uik SAL_CALL getUik() override;
221 virtual css::uno::Reference< css::reflection::XIdlMethod > SAL_CALL getMethod( const OUString & rName ) override;
222 virtual css::uno::Sequence< css::uno::Reference< css::reflection::XIdlMethod > > SAL_CALL getMethods() override;
223 virtual css::uno::Reference< css::reflection::XIdlField > SAL_CALL getField( const OUString & rName ) override;
224 virtual css::uno::Sequence< css::uno::Reference< css::reflection::XIdlField > > SAL_CALL getFields() override;
225 virtual void SAL_CALL createObject( css::uno::Any & rObj ) override;
229 class CompoundIdlClassImpl
230 : public IdlClassImpl
232 css::uno::Reference< css::reflection::XIdlClass >
233 _xSuperClass;
234 std::unique_ptr< css::uno::Sequence< css::uno::Reference< css::reflection::XIdlField > > >
235 _pFields;
236 OUString2Field _aName2Field;
238 public:
239 typelib_CompoundTypeDescription * getTypeDescr() const
240 { return reinterpret_cast<typelib_CompoundTypeDescription *>(IdlClassImpl::getTypeDescr()); }
242 // ctor/ dtor
243 CompoundIdlClassImpl( IdlReflectionServiceImpl * pReflection,
244 const OUString & rName, typelib_TypeClass eTypeClass,
245 typelib_TypeDescription * pTypeDescr )
246 : IdlClassImpl( pReflection, rName, eTypeClass, pTypeDescr )
248 virtual ~CompoundIdlClassImpl() override;
250 // IdlClassImpl modifications
251 virtual sal_Bool SAL_CALL isAssignableFrom( const css::uno::Reference< css::reflection::XIdlClass > & xType ) override;
252 virtual css::uno::Sequence< css::uno::Reference< css::reflection::XIdlClass > > SAL_CALL getSuperclasses() override;
253 virtual css::uno::Reference< css::reflection::XIdlField > SAL_CALL getField( const OUString & rName ) override;
254 virtual css::uno::Sequence< css::uno::Reference< css::reflection::XIdlField > > SAL_CALL getFields() override;
258 class ArrayIdlClassImpl
259 : public IdlClassImpl
260 , public css::reflection::XIdlArray
262 public:
263 typelib_IndirectTypeDescription * getTypeDescr() const
264 { return reinterpret_cast<typelib_IndirectTypeDescription *>(IdlClassImpl::getTypeDescr()); }
266 // ctor
267 ArrayIdlClassImpl( IdlReflectionServiceImpl * pReflection,
268 const OUString & rName, typelib_TypeClass eTypeClass,
269 typelib_TypeDescription * pTypeDescr )
270 : IdlClassImpl( pReflection, rName, eTypeClass, pTypeDescr )
273 virtual css::uno::Any SAL_CALL queryInterface( const css::uno::Type & rType ) override;
274 virtual void SAL_CALL acquire() throw() override;
275 virtual void SAL_CALL release() throw() override;
277 // XTypeProvider
278 virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override;
279 virtual css::uno::Sequence< sal_Int8 > SAL_CALL getImplementationId() override;
281 // IdlClassImpl modifications
282 virtual sal_Bool SAL_CALL isAssignableFrom( const css::uno::Reference< css::reflection::XIdlClass > & xType ) override;
283 virtual css::uno::Reference< css::reflection::XIdlClass > SAL_CALL getComponentType() override;
284 virtual css::uno::Reference< css::reflection::XIdlArray > SAL_CALL getArray() override;
286 // XIdlArray
287 virtual void SAL_CALL realloc( css::uno::Any & rArray, sal_Int32 nLen ) override;
288 virtual sal_Int32 SAL_CALL getLen( const css::uno::Any & rArray ) override;
289 virtual css::uno::Any SAL_CALL get( const css::uno::Any & rArray, sal_Int32 nIndex ) override;
290 virtual void SAL_CALL set( css::uno::Any & rArray, sal_Int32 nIndex, const css::uno::Any & rNewValue ) override;
294 class EnumIdlClassImpl
295 : public IdlClassImpl
297 std::unique_ptr< css::uno::Sequence< css::uno::Reference< css::reflection::XIdlField > > > _pFields;
298 OUString2Field _aName2Field;
300 public:
301 typelib_EnumTypeDescription * getTypeDescr() const
302 { return reinterpret_cast<typelib_EnumTypeDescription *>(IdlClassImpl::getTypeDescr()); }
304 // ctor/ dtor
305 EnumIdlClassImpl( IdlReflectionServiceImpl * pReflection,
306 const OUString & rName, typelib_TypeClass eTypeClass,
307 typelib_TypeDescription * pTypeDescr )
308 : IdlClassImpl( pReflection, rName, eTypeClass, pTypeDescr )
310 virtual ~EnumIdlClassImpl() override;
312 // IdlClassImpl modifications
313 virtual css::uno::Reference< css::reflection::XIdlField > SAL_CALL getField( const OUString & rName ) override;
314 virtual css::uno::Sequence< css::uno::Reference< css::reflection::XIdlField > > SAL_CALL getFields() override;
315 virtual void SAL_CALL createObject( css::uno::Any & rObj ) override;
319 class IdlMemberImpl
320 : public ::cppu::WeakImplHelper< css::reflection::XIdlMember >
322 rtl::Reference<IdlReflectionServiceImpl>
323 m_xReflection;
324 OUString _aName;
326 typelib_TypeDescription * _pTypeDescr;
327 typelib_TypeDescription * _pDeclTypeDescr;
329 protected:
330 css::uno::Reference< css::reflection::XIdlClass > _xDeclClass;
332 public:
333 IdlReflectionServiceImpl * getReflection() const
334 { return m_xReflection.get(); }
335 typelib_TypeDescription * getTypeDescr() const
336 { return _pTypeDescr; }
337 typelib_TypeDescription * getDeclTypeDescr() const
338 { return _pDeclTypeDescr; }
340 // ctor/ dtor
341 IdlMemberImpl( IdlReflectionServiceImpl * pReflection, const OUString & rName,
342 typelib_TypeDescription * pTypeDescr, typelib_TypeDescription * pDeclTypeDescr );
343 virtual ~IdlMemberImpl() override;
345 // XIdlMember
346 virtual css::uno::Reference< css::reflection::XIdlClass > SAL_CALL getDeclaringClass() override;
347 virtual OUString SAL_CALL getName() override;
351 // coerces to type descr pTo else queries for it: the interface pointer is returned via rDest
352 // ## type to XidlClass coercion possible
353 inline bool extract(
354 const css::uno::Any & rObj, typelib_InterfaceTypeDescription * pTo,
355 css::uno::Reference< css::uno::XInterface > & rDest,
356 IdlReflectionServiceImpl * pRefl )
358 rDest.clear();
359 if (nullptr != pTo)
361 if (! rObj.hasValue())
362 return true;
363 if (rObj.getValueTypeClass() == css::uno::TypeClass_INTERFACE)
365 return ::uno_type_assignData(
366 &rDest, pTo->aBase.pWeakRef,
367 const_cast< void * >( rObj.getValue() ), rObj.getValueTypeRef(),
368 reinterpret_cast< uno_QueryInterfaceFunc >(css::uno::cpp_queryInterface),
369 reinterpret_cast< uno_AcquireFunc >(css::uno::cpp_acquire),
370 reinterpret_cast< uno_ReleaseFunc >(css::uno::cpp_release) );
372 else if (auto t = o3tl::tryAccess<css::uno::Type>(rObj))
374 rDest = pRefl->forType( t->getTypeLibType() );
375 return rDest.is();
378 return false;
381 inline bool coerce_assign(
382 void * pDest, typelib_TypeDescription * pTD, const css::uno::Any & rSource,
383 IdlReflectionServiceImpl * pRefl )
385 if (pTD->eTypeClass == typelib_TypeClass_INTERFACE)
387 css::uno::Reference< css::uno::XInterface > xVal;
388 if (extract( rSource, reinterpret_cast<typelib_InterfaceTypeDescription *>(pTD), xVal, pRefl ))
390 if (*static_cast<css::uno::XInterface **>(pDest))
391 (*static_cast<css::uno::XInterface **>(pDest))->release();
392 *static_cast<css::uno::XInterface **>(pDest) = xVal.get();
393 if (*static_cast<css::uno::XInterface **>(pDest))
394 (*static_cast<css::uno::XInterface **>(pDest))->acquire();
395 return true;
397 return false;
399 else if (pTD->eTypeClass == typelib_TypeClass_ANY)
401 return uno_assignData(
402 pDest, pTD,
403 const_cast<css::uno::Any *>(&rSource), pTD,
404 reinterpret_cast< uno_QueryInterfaceFunc >(css::uno::cpp_queryInterface),
405 reinterpret_cast< uno_AcquireFunc >(css::uno::cpp_acquire),
406 reinterpret_cast< uno_ReleaseFunc >(css::uno::cpp_release) );
408 else
410 return uno_type_assignData(
411 pDest, pTD->pWeakRef,
412 const_cast<void *>(rSource.getValue()), rSource.getValueTypeRef(),
413 reinterpret_cast< uno_QueryInterfaceFunc >(css::uno::cpp_queryInterface),
414 reinterpret_cast< uno_AcquireFunc >(css::uno::cpp_acquire),
415 reinterpret_cast< uno_ReleaseFunc >(css::uno::cpp_release) );
422 #endif // INCLUDED_STOC_SOURCE_COREREFLECTION_BASE_HXX
424 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */