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 //  #define TEST_LIST_CLASSES
20 
21 #ifndef INCLUDED_STOC_SOURCE_COREREFLECTION_BASE_HXX
22 #define INCLUDED_STOC_SOURCE_COREREFLECTION_BASE_HXX
23 
24 #include <sal/config.h>
25 
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>
34 
35 #include "lrucache.hxx"
36 
37 #ifdef TEST_LIST_CLASSES
38 #include <vector>
39 #include <algorithm>
40 #endif
41 #include <unordered_map>
42 #include <memory>
43 
44 #include <com/sun/star/lang/XServiceInfo.hpp>
45 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
46 
47 #include <com/sun/star/reflection/XIdlReflection.hpp>
48 
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; }
53 
54 namespace stoc_corefl
55 {
56 
57 #ifdef TEST_LIST_CLASSES
58 extern std::vector<OUString> g_aClassNames;
59 #endif
60 
61 
62 ::osl::Mutex & getMutexAccess();
63 
64 
td_equals(typelib_TypeDescription * pTD,typelib_TypeDescriptionReference * pType)65 inline bool td_equals( typelib_TypeDescription * pTD, typelib_TypeDescriptionReference * pType )
66 {
67     return (pTD->pWeakRef == pType ||
68             (pTD->pTypeName->length == pType->pTypeName->length &&
69              rtl_ustr_compare( pTD->pTypeName->buffer, pType->pTypeName->buffer ) == 0));
70 }
71 
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;
74 
75 
76 class IdlReflectionServiceImpl
77     : public ::cppu::OComponentHelper
78     , public css::reflection::XIdlReflection
79     , public css::container::XHierarchicalNameAccess
80     , public css::lang::XServiceInfo
81 {
82     ::osl::Mutex                            _aComponentMutex;
83     css::uno::Reference< css::container::XHierarchicalNameAccess >    _xTDMgr;
84 
85     // caching
86     LRU_CacheAnyByOUString                  _aElements;
87 
88     css::uno::Mapping                     _aCpp2Uno;
89     css::uno::Mapping                     _aUno2Cpp;
90 
91     inline css::uno::Reference< css::reflection::XIdlClass > constructClass( typelib_TypeDescription * pTypeDescr );
92 
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 );
100 
101     // ctor/ dtor
102     explicit IdlReflectionServiceImpl( const css::uno::Reference< css::uno::XComponentContext > & xContext );
103     virtual ~IdlReflectionServiceImpl() override;
104 
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;
109 
110     // some XComponent part from OComponentHelper
111     virtual void SAL_CALL dispose() override;
112 
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;
117 
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;
121 
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;
125 
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;
129 
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 );
134 };
135 
136 
137 class IdlClassImpl
138     : public ::cppu::WeakImplHelper< css::reflection::XIdlClass >
139 {
140     rtl::Reference<IdlReflectionServiceImpl>
141                                  m_xReflection;
142 
143     OUString const               _aName;
144     css::uno::TypeClass const    _eTypeClass;
145 
146     typelib_TypeDescription *   _pTypeDescr;
147 
148 public:
getTypeDescr() const149     typelib_TypeDescription *   getTypeDescr() const
150         { return _pTypeDescr; }
getReflection() const151     IdlReflectionServiceImpl *  getReflection() const
152         { return m_xReflection.get(); }
153 
154     // Ctor
155     IdlClassImpl( IdlReflectionServiceImpl * pReflection,
156                   const OUString & rName, typelib_TypeClass eTypeClass,
157                   typelib_TypeDescription * pTypeDescr );
158     virtual ~IdlClassImpl() override;
159 
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;
164 
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;
167 
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;
172 
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;
185 };
186 
187 
188 class InterfaceIdlClassImpl
189     : public IdlClassImpl
190 {
191     typedef std::pair< OUString, typelib_TypeDescription * > MemberInit;
192 
193     css::uno::Sequence< css::uno::Reference< css::reflection::XIdlClass > >      _xSuperClasses;
194 
195     std::unique_ptr<MemberInit[]>           _pSortedMemberInit; // first methods, then attributes
196     OUString2Field                          _aName2Field;
197     OUString2Method                         _aName2Method;
198     sal_Int32                               _nMethods;
199     sal_Int32                               _nAttributes;
200 
201     void initMembers();
202 
203 public:
getTypeDescr() const204     typelib_InterfaceTypeDescription * getTypeDescr() const
205         { return reinterpret_cast<typelib_InterfaceTypeDescription *>(IdlClassImpl::getTypeDescr()); }
206 
207     // ctor/ dtor
InterfaceIdlClassImpl(IdlReflectionServiceImpl * pReflection,const OUString & rName,typelib_TypeClass eTypeClass,typelib_TypeDescription * pTypeDescr)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 )
214         {}
215     virtual ~InterfaceIdlClassImpl() override;
216 
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;
226 };
227 
228 
229 class CompoundIdlClassImpl
230     : public IdlClassImpl
231 {
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;
237 
238 public:
getTypeDescr() const239     typelib_CompoundTypeDescription * getTypeDescr() const
240         { return reinterpret_cast<typelib_CompoundTypeDescription *>(IdlClassImpl::getTypeDescr()); }
241 
242     // ctor/ dtor
CompoundIdlClassImpl(IdlReflectionServiceImpl * pReflection,const OUString & rName,typelib_TypeClass eTypeClass,typelib_TypeDescription * pTypeDescr)243     CompoundIdlClassImpl( IdlReflectionServiceImpl * pReflection,
244                           const OUString & rName, typelib_TypeClass eTypeClass,
245                           typelib_TypeDescription * pTypeDescr )
246         : IdlClassImpl( pReflection, rName, eTypeClass, pTypeDescr )
247         {}
248     virtual ~CompoundIdlClassImpl() override;
249 
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;
255 };
256 
257 
258 class ArrayIdlClassImpl
259     : public IdlClassImpl
260     , public css::reflection::XIdlArray
261 {
262 public:
getTypeDescr() const263     typelib_IndirectTypeDescription * getTypeDescr() const
264         { return reinterpret_cast<typelib_IndirectTypeDescription *>(IdlClassImpl::getTypeDescr()); }
265 
266     // ctor
ArrayIdlClassImpl(IdlReflectionServiceImpl * pReflection,const OUString & rName,typelib_TypeClass eTypeClass,typelib_TypeDescription * pTypeDescr)267     ArrayIdlClassImpl( IdlReflectionServiceImpl * pReflection,
268                        const OUString & rName, typelib_TypeClass eTypeClass,
269                        typelib_TypeDescription * pTypeDescr )
270         : IdlClassImpl( pReflection, rName, eTypeClass, pTypeDescr )
271         {}
272 
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;
276 
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;
280 
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;
285 
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;
291 };
292 
293 
294 class EnumIdlClassImpl
295     : public IdlClassImpl
296 {
297     std::unique_ptr< css::uno::Sequence< css::uno::Reference< css::reflection::XIdlField > > > _pFields;
298     OUString2Field                       _aName2Field;
299 
300 public:
getTypeDescr() const301     typelib_EnumTypeDescription * getTypeDescr() const
302         { return reinterpret_cast<typelib_EnumTypeDescription *>(IdlClassImpl::getTypeDescr()); }
303 
304     // ctor/ dtor
EnumIdlClassImpl(IdlReflectionServiceImpl * pReflection,const OUString & rName,typelib_TypeClass eTypeClass,typelib_TypeDescription * pTypeDescr)305     EnumIdlClassImpl( IdlReflectionServiceImpl * pReflection,
306                       const OUString & rName, typelib_TypeClass eTypeClass,
307                       typelib_TypeDescription * pTypeDescr )
308         : IdlClassImpl( pReflection, rName, eTypeClass, pTypeDescr )
309         {}
310     virtual ~EnumIdlClassImpl() override;
311 
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;
316 };
317 
318 
319 class IdlMemberImpl
320     : public ::cppu::WeakImplHelper< css::reflection::XIdlMember >
321 {
322     rtl::Reference<IdlReflectionServiceImpl>
323                                 m_xReflection;
324     OUString const              _aName;
325 
326     typelib_TypeDescription *   _pTypeDescr;
327     typelib_TypeDescription *   _pDeclTypeDescr;
328 
329 protected:
330     css::uno::Reference< css::reflection::XIdlClass >      _xDeclClass;
331 
332 public:
getReflection() const333     IdlReflectionServiceImpl *  getReflection() const
334         { return m_xReflection.get(); }
getTypeDescr() const335     typelib_TypeDescription *   getTypeDescr() const
336         { return _pTypeDescr; }
getDeclTypeDescr() const337     typelib_TypeDescription *   getDeclTypeDescr() const
338         { return _pDeclTypeDescr; }
339 
340     // ctor/ dtor
341     IdlMemberImpl( IdlReflectionServiceImpl * pReflection, const OUString & rName,
342                    typelib_TypeDescription * pTypeDescr, typelib_TypeDescription * pDeclTypeDescr );
343     virtual ~IdlMemberImpl() override;
344 
345     // XIdlMember
346     virtual css::uno::Reference< css::reflection::XIdlClass > SAL_CALL getDeclaringClass() override;
347     virtual OUString SAL_CALL getName() override;
348 };
349 
350 
351 // coerces to type descr pTo else queries for it: the interface pointer is returned via rDest
352 // ## type to XidlClass coercion possible
extract(const css::uno::Any & rObj,typelib_InterfaceTypeDescription * pTo,css::uno::Reference<css::uno::XInterface> & rDest,IdlReflectionServiceImpl * pRefl)353 inline bool extract(
354     const css::uno::Any & rObj, typelib_InterfaceTypeDescription * pTo,
355     css::uno::Reference< css::uno::XInterface > & rDest,
356     IdlReflectionServiceImpl * pRefl )
357 {
358     rDest.clear();
359     if (nullptr != pTo)
360     {
361         if (! rObj.hasValue())
362             return true;
363         if (rObj.getValueTypeClass() == css::uno::TypeClass_INTERFACE)
364         {
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) );
371         }
372         else if (auto t = o3tl::tryAccess<css::uno::Type>(rObj))
373         {
374             rDest = pRefl->forType( t->getTypeLibType() );
375             return rDest.is();
376         }
377     }
378     return false;
379 }
380 
coerce_assign(void * pDest,typelib_TypeDescription * pTD,const css::uno::Any & rSource,IdlReflectionServiceImpl * pRefl)381 inline bool coerce_assign(
382     void * pDest, typelib_TypeDescription * pTD, const css::uno::Any & rSource,
383     IdlReflectionServiceImpl * pRefl )
384 {
385     if (pTD->eTypeClass == typelib_TypeClass_INTERFACE)
386     {
387         css::uno::Reference< css::uno::XInterface > xVal;
388         if (extract( rSource, reinterpret_cast<typelib_InterfaceTypeDescription *>(pTD), xVal, pRefl ))
389         {
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;
396         }
397         return false;
398     }
399     else if (pTD->eTypeClass == typelib_TypeClass_ANY)
400     {
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) );
407     }
408     else
409     {
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) );
416     }
417 }
418 
419 }
420 
421 
422 #endif // INCLUDED_STOC_SOURCE_COREREFLECTION_BASE_HXX
423 
424 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
425