Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / stoc / source / corereflection / crcomp.cxx
blob0a0572676a73287d1a15918abf6d651996a38c91
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 return;
217 else
219 throw IllegalArgumentException(
220 "cannot assign value to destination",
221 static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 1 );
224 TYPELIB_DANGER_RELEASE( pObjTD );
226 throw IllegalArgumentException(
227 "expected struct or exception, got " + rObj.getValueType().getTypeName(),
228 static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 0 );
232 void IdlCompFieldImpl::set( Any & rObj, const Any & rValue )
234 if (rObj.getValueTypeClass() == css::uno::TypeClass_STRUCT ||
235 rObj.getValueTypeClass() == css::uno::TypeClass_EXCEPTION)
237 typelib_TypeDescription * pObjTD = nullptr;
238 TYPELIB_DANGER_GET( &pObjTD, rObj.getValueTypeRef() );
240 typelib_TypeDescription * pTD = pObjTD;
241 typelib_TypeDescription * pDeclTD = getDeclTypeDescr();
242 while (pTD && !typelib_typedescription_equals( pTD, pDeclTD ))
243 pTD = &reinterpret_cast<typelib_CompoundTypeDescription *>(pTD)->pBaseTypeDescription->aBase;
245 OSL_ENSURE( pTD, "### illegal object type!" );
246 if (pTD)
248 TYPELIB_DANGER_RELEASE( pObjTD );
249 if (coerce_assign( const_cast<char *>(static_cast<char const *>(rObj.getValue()) + _nOffset), getTypeDescr(), rValue, getReflection() ))
251 return;
253 else
255 throw IllegalArgumentException(
256 "cannot assign to destination",
257 static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 1 );
260 TYPELIB_DANGER_RELEASE( pObjTD );
262 throw IllegalArgumentException(
263 "expected struct or exception, got " + rObj.getValueType().getTypeName(),
264 static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 0 );
268 CompoundIdlClassImpl::~CompoundIdlClassImpl()
273 sal_Bool CompoundIdlClassImpl::isAssignableFrom( const Reference< XIdlClass > & xType )
275 if (xType.is())
277 TypeClass eTC = xType->getTypeClass();
278 if (eTC == TypeClass_STRUCT || eTC == TypeClass_EXCEPTION)
280 if (equals( xType ))
281 return true;
282 else
284 const Sequence< Reference< XIdlClass > > & rSeq = xType->getSuperclasses();
285 if (rSeq.getLength())
287 OSL_ENSURE( rSeq.getLength() == 1, "### unexpected len of super classes!" );
288 return isAssignableFrom( rSeq[0] );
293 return false;
296 Sequence< Reference< XIdlClass > > CompoundIdlClassImpl::getSuperclasses()
298 if (! _xSuperClass.is())
300 ::osl::MutexGuard aGuard( getMutexAccess() );
301 if (! _xSuperClass.is())
303 typelib_CompoundTypeDescription * pCompTypeDescr = getTypeDescr()->pBaseTypeDescription;
304 if (pCompTypeDescr)
305 _xSuperClass = getReflection()->forType( &pCompTypeDescr->aBase );
308 if (_xSuperClass.is())
309 return Sequence< Reference< XIdlClass > >( &_xSuperClass, 1 );
310 else
311 return Sequence< Reference< XIdlClass > >();
314 Reference< XIdlField > CompoundIdlClassImpl::getField( const OUString & rName )
316 if (! _pFields)
317 getFields(); // init fields
319 const OUString2Field::const_iterator iFind( _aName2Field.find( rName ) );
320 if (iFind != _aName2Field.end())
321 return Reference< XIdlField >( (*iFind).second );
322 else
323 return Reference< XIdlField >();
326 Sequence< Reference< XIdlField > > CompoundIdlClassImpl::getFields()
328 ::osl::MutexGuard aGuard( getMutexAccess() );
329 if (! _pFields)
331 sal_Int32 nAll = 0;
332 typelib_CompoundTypeDescription * pCompTypeDescr = getTypeDescr();
333 for ( ; pCompTypeDescr; pCompTypeDescr = pCompTypeDescr->pBaseTypeDescription )
334 nAll += pCompTypeDescr->nMembers;
336 Sequence< Reference< XIdlField > > * pFields =
337 new Sequence< Reference< XIdlField > >( nAll );
338 Reference< XIdlField > * pSeq = pFields->getArray();
340 for ( pCompTypeDescr = getTypeDescr(); pCompTypeDescr;
341 pCompTypeDescr = pCompTypeDescr->pBaseTypeDescription )
343 typelib_TypeDescriptionReference ** ppTypeRefs = pCompTypeDescr->ppTypeRefs;
344 rtl_uString ** ppNames = pCompTypeDescr->ppMemberNames;
345 sal_Int32 * pMemberOffsets = pCompTypeDescr->pMemberOffsets;
347 for ( sal_Int32 nPos = pCompTypeDescr->nMembers; nPos--; )
349 typelib_TypeDescription * pTD = nullptr;
350 TYPELIB_DANGER_GET( &pTD, ppTypeRefs[nPos] );
351 OSL_ENSURE( pTD, "### cannot get field in struct!" );
352 if (pTD)
354 OUString aName( ppNames[nPos] );
355 _aName2Field[aName] = pSeq[--nAll] = new IdlCompFieldImpl(
356 getReflection(), aName, pTD, IdlClassImpl::getTypeDescr(), pMemberOffsets[nPos] );
357 TYPELIB_DANGER_RELEASE( pTD );
362 _pFields.reset( pFields );
364 return *_pFields;
370 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */