Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / stoc / source / corereflection / crcomp.cxx
blob01e04314407542fdfef65c07f4f4d5935892ad56
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 .
20 #include <rtl/strbuf.hxx>
21 #include <cppuhelper/queryinterface.hxx>
23 #include <com/sun/star/reflection/XIdlField.hpp>
24 #include <com/sun/star/reflection/XIdlField2.hpp>
25 #include <com/sun/star/uno/TypeClass.hpp>
27 #include "base.hxx"
29 using namespace css::lang;
30 using namespace css::reflection;
31 using namespace css::uno;
33 namespace stoc_corefl
37 class IdlCompFieldImpl
38 : public IdlMemberImpl
39 , public XIdlField
40 , public XIdlField2
42 sal_Int32 _nOffset;
44 public:
45 IdlCompFieldImpl( IdlReflectionServiceImpl * pReflection, const OUString & rName,
46 typelib_TypeDescription * pTypeDescr, typelib_TypeDescription * pDeclTypeDescr,
47 sal_Int32 nOffset )
48 : IdlMemberImpl( pReflection, rName, pTypeDescr, pDeclTypeDescr )
49 , _nOffset( nOffset )
52 // XInterface
53 virtual Any SAL_CALL queryInterface( const Type & rType ) override;
54 virtual void SAL_CALL acquire() throw () override;
55 virtual void SAL_CALL release() throw () override;
57 // XTypeProvider
58 virtual Sequence< Type > SAL_CALL getTypes() override;
59 virtual Sequence< sal_Int8 > SAL_CALL getImplementationId() override;
61 // XIdlMember
62 virtual Reference< XIdlClass > SAL_CALL getDeclaringClass() override;
63 virtual OUString SAL_CALL getName() override;
64 // XIdlField
65 virtual Reference< XIdlClass > SAL_CALL getType() override;
66 virtual FieldAccessMode SAL_CALL getAccessMode() override;
67 virtual Any SAL_CALL get( const Any & rObj ) override;
68 virtual void SAL_CALL set( const Any & rObj, const Any & rValue ) override;
69 // XIdlField2: getType, getAccessMode and get are equal to XIdlField
70 virtual void SAL_CALL set( Any & rObj, const Any & rValue ) override;
73 // XInterface
75 Any IdlCompFieldImpl::queryInterface( const Type & rType )
77 Any aRet( ::cppu::queryInterface( rType,
78 static_cast< XIdlField * >( this ),
79 static_cast< XIdlField2 * >( this ) ) );
80 return (aRet.hasValue() ? aRet : IdlMemberImpl::queryInterface( rType ));
83 void IdlCompFieldImpl::acquire() throw()
85 IdlMemberImpl::acquire();
88 void IdlCompFieldImpl::release() throw()
90 IdlMemberImpl::release();
93 // XTypeProvider
95 Sequence< Type > IdlCompFieldImpl::getTypes()
97 static ::cppu::OTypeCollection * s_pTypes = nullptr;
98 if (! s_pTypes)
100 ::osl::MutexGuard aGuard( getMutexAccess() );
101 if (! s_pTypes)
103 static ::cppu::OTypeCollection s_aTypes(
104 cppu::UnoType<XIdlField2>::get(),
105 cppu::UnoType<XIdlField>::get(),
106 IdlMemberImpl::getTypes() );
107 s_pTypes = &s_aTypes;
110 return s_pTypes->getTypes();
113 Sequence< sal_Int8 > IdlCompFieldImpl::getImplementationId()
115 return css::uno::Sequence<sal_Int8>();
118 // XIdlMember
120 Reference< XIdlClass > IdlCompFieldImpl::getDeclaringClass()
122 if (! _xDeclClass.is())
124 ::osl::MutexGuard aGuard( getMutexAccess() );
125 if (! _xDeclClass.is())
127 typelib_CompoundTypeDescription * pTD =
128 reinterpret_cast<typelib_CompoundTypeDescription *>(getDeclTypeDescr());
129 while (pTD)
131 typelib_TypeDescriptionReference ** ppTypeRefs = pTD->ppTypeRefs;
132 for ( sal_Int32 nPos = pTD->nMembers; nPos--; )
134 if (td_equals( getTypeDescr(), ppTypeRefs[nPos] ))
136 _xDeclClass = getReflection()->forType( &pTD->aBase );
137 return _xDeclClass;
140 pTD = pTD->pBaseTypeDescription;
144 return _xDeclClass;
147 OUString IdlCompFieldImpl::getName()
149 return IdlMemberImpl::getName();
152 // XIdlField
154 Reference< XIdlClass > IdlCompFieldImpl::getType()
156 return getReflection()->forType( getTypeDescr() );
159 FieldAccessMode IdlCompFieldImpl::getAccessMode()
161 return FieldAccessMode_READWRITE;
164 Any IdlCompFieldImpl::get( const Any & rObj )
166 if (rObj.getValueTypeClass() == css::uno::TypeClass_STRUCT ||
167 rObj.getValueTypeClass() == css::uno::TypeClass_EXCEPTION)
169 typelib_TypeDescription * pObjTD = nullptr;
170 TYPELIB_DANGER_GET( &pObjTD, rObj.getValueTypeRef() );
172 typelib_TypeDescription * pTD = pObjTD;
173 typelib_TypeDescription * pDeclTD = getDeclTypeDescr();
174 while (pTD && !typelib_typedescription_equals( pTD, pDeclTD ))
175 pTD = &reinterpret_cast<typelib_CompoundTypeDescription *>(pTD)->pBaseTypeDescription->aBase;
177 OSL_ENSURE( pTD, "### illegal object type!" );
178 if (pTD)
180 TYPELIB_DANGER_RELEASE( pObjTD );
181 Any aRet;
182 uno_any_destruct(
183 &aRet, reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
184 uno_any_construct(
185 &aRet, const_cast<char *>(static_cast<char const *>(rObj.getValue()) + _nOffset), getTypeDescr(),
186 reinterpret_cast< uno_AcquireFunc >(cpp_acquire) );
187 return aRet;
189 TYPELIB_DANGER_RELEASE( pObjTD );
191 throw IllegalArgumentException(
192 "expected struct or exception, got " + rObj.getValueType().getTypeName(),
193 static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 0 );
196 void IdlCompFieldImpl::set( const Any & rObj, const Any & rValue )
198 if (rObj.getValueTypeClass() == css::uno::TypeClass_STRUCT ||
199 rObj.getValueTypeClass() == css::uno::TypeClass_EXCEPTION)
201 typelib_TypeDescription * pObjTD = nullptr;
202 TYPELIB_DANGER_GET( &pObjTD, rObj.getValueTypeRef() );
204 typelib_TypeDescription * pTD = pObjTD;
205 typelib_TypeDescription * pDeclTD = getDeclTypeDescr();
206 while (pTD && !typelib_typedescription_equals( pTD, pDeclTD ))
207 pTD = &reinterpret_cast<typelib_CompoundTypeDescription *>(pTD)->pBaseTypeDescription->aBase;
209 OSL_ENSURE( pTD, "### illegal object type!" );
210 if (pTD)
212 TYPELIB_DANGER_RELEASE( pObjTD );
213 if (!coerce_assign( const_cast<char *>(static_cast<char const *>(rObj.getValue()) + _nOffset), getTypeDescr(), rValue, getReflection() ))
215 throw IllegalArgumentException(
216 "cannot assign value to destination",
217 static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 1 );
219 return;
221 TYPELIB_DANGER_RELEASE( pObjTD );
223 throw IllegalArgumentException(
224 "expected struct or exception, got " + rObj.getValueType().getTypeName(),
225 static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 0 );
229 void IdlCompFieldImpl::set( Any & rObj, const Any & rValue )
231 if (rObj.getValueTypeClass() == css::uno::TypeClass_STRUCT ||
232 rObj.getValueTypeClass() == css::uno::TypeClass_EXCEPTION)
234 typelib_TypeDescription * pObjTD = nullptr;
235 TYPELIB_DANGER_GET( &pObjTD, rObj.getValueTypeRef() );
237 typelib_TypeDescription * pTD = pObjTD;
238 typelib_TypeDescription * pDeclTD = getDeclTypeDescr();
239 while (pTD && !typelib_typedescription_equals( pTD, pDeclTD ))
240 pTD = &reinterpret_cast<typelib_CompoundTypeDescription *>(pTD)->pBaseTypeDescription->aBase;
242 OSL_ENSURE( pTD, "### illegal object type!" );
243 if (pTD)
245 TYPELIB_DANGER_RELEASE( pObjTD );
246 if (!coerce_assign( const_cast<char *>(static_cast<char const *>(rObj.getValue()) + _nOffset), getTypeDescr(), rValue, getReflection() ))
248 throw IllegalArgumentException(
249 "cannot assign to destination",
250 static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 1 );
252 return;
254 TYPELIB_DANGER_RELEASE( pObjTD );
256 throw IllegalArgumentException(
257 "expected struct or exception, got " + rObj.getValueType().getTypeName(),
258 static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 0 );
262 CompoundIdlClassImpl::~CompoundIdlClassImpl()
267 sal_Bool CompoundIdlClassImpl::isAssignableFrom( const Reference< XIdlClass > & xType )
269 if (xType.is())
271 TypeClass eTC = xType->getTypeClass();
272 if (eTC == TypeClass_STRUCT || eTC == TypeClass_EXCEPTION)
274 if (equals( xType ))
275 return true;
276 else
278 const Sequence< Reference< XIdlClass > > & rSeq = xType->getSuperclasses();
279 if (rSeq.getLength())
281 OSL_ENSURE( rSeq.getLength() == 1, "### unexpected len of super classes!" );
282 return isAssignableFrom( rSeq[0] );
287 return false;
290 Sequence< Reference< XIdlClass > > CompoundIdlClassImpl::getSuperclasses()
292 if (! _xSuperClass.is())
294 ::osl::MutexGuard aGuard( getMutexAccess() );
295 if (! _xSuperClass.is())
297 typelib_CompoundTypeDescription * pCompTypeDescr = getTypeDescr()->pBaseTypeDescription;
298 if (pCompTypeDescr)
299 _xSuperClass = getReflection()->forType( &pCompTypeDescr->aBase );
302 if (_xSuperClass.is())
303 return Sequence< Reference< XIdlClass > >( &_xSuperClass, 1 );
304 else
305 return Sequence< Reference< XIdlClass > >();
308 Reference< XIdlField > CompoundIdlClassImpl::getField( const OUString & rName )
310 if (! _pFields)
311 getFields(); // init fields
313 const OUString2Field::const_iterator iFind( _aName2Field.find( rName ) );
314 if (iFind != _aName2Field.end())
315 return Reference< XIdlField >( (*iFind).second );
316 else
317 return Reference< XIdlField >();
320 Sequence< Reference< XIdlField > > CompoundIdlClassImpl::getFields()
322 ::osl::MutexGuard aGuard( getMutexAccess() );
323 if (! _pFields)
325 sal_Int32 nAll = 0;
326 typelib_CompoundTypeDescription * pCompTypeDescr = getTypeDescr();
327 for ( ; pCompTypeDescr; pCompTypeDescr = pCompTypeDescr->pBaseTypeDescription )
328 nAll += pCompTypeDescr->nMembers;
330 Sequence< Reference< XIdlField > > * pFields =
331 new Sequence< Reference< XIdlField > >( nAll );
332 Reference< XIdlField > * pSeq = pFields->getArray();
334 for ( pCompTypeDescr = getTypeDescr(); pCompTypeDescr;
335 pCompTypeDescr = pCompTypeDescr->pBaseTypeDescription )
337 typelib_TypeDescriptionReference ** ppTypeRefs = pCompTypeDescr->ppTypeRefs;
338 rtl_uString ** ppNames = pCompTypeDescr->ppMemberNames;
339 sal_Int32 * pMemberOffsets = pCompTypeDescr->pMemberOffsets;
341 for ( sal_Int32 nPos = pCompTypeDescr->nMembers; nPos--; )
343 typelib_TypeDescription * pTD = nullptr;
344 TYPELIB_DANGER_GET( &pTD, ppTypeRefs[nPos] );
345 OSL_ENSURE( pTD, "### cannot get field in struct!" );
346 if (pTD)
348 OUString aName( ppNames[nPos] );
349 _aName2Field[aName] = pSeq[--nAll] = new IdlCompFieldImpl(
350 getReflection(), aName, pTD, IdlClassImpl::getTypeDescr(), pMemberOffsets[nPos] );
351 TYPELIB_DANGER_RELEASE( pTD );
356 _pFields.reset( pFields );
358 return *_pFields;
364 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */