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 .
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>
29 using namespace css::lang
;
30 using namespace css::reflection
;
31 using namespace css::uno
;
37 class IdlCompFieldImpl
38 : public IdlMemberImpl
45 IdlCompFieldImpl( IdlReflectionServiceImpl
* pReflection
, const OUString
& rName
,
46 typelib_TypeDescription
* pTypeDescr
, typelib_TypeDescription
* pDeclTypeDescr
,
48 : IdlMemberImpl( pReflection
, rName
, pTypeDescr
, pDeclTypeDescr
)
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
;
58 virtual Sequence
< Type
> SAL_CALL
getTypes() override
;
59 virtual Sequence
< sal_Int8
> SAL_CALL
getImplementationId() override
;
62 virtual Reference
< XIdlClass
> SAL_CALL
getDeclaringClass() override
;
63 virtual OUString SAL_CALL
getName() override
;
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
;
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();
95 Sequence
< Type
> IdlCompFieldImpl::getTypes()
97 static ::cppu::OTypeCollection
* s_pTypes
= nullptr;
100 ::osl::MutexGuard
aGuard( getMutexAccess() );
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
>();
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());
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
);
140 pTD
= pTD
->pBaseTypeDescription
;
147 OUString
IdlCompFieldImpl::getName()
149 return IdlMemberImpl::getName();
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!" );
180 TYPELIB_DANGER_RELEASE( pObjTD
);
183 &aRet
, reinterpret_cast< uno_ReleaseFunc
>(cpp_release
) );
185 &aRet
, const_cast<char *>(static_cast<char const *>(rObj
.getValue()) + _nOffset
), getTypeDescr(),
186 reinterpret_cast< uno_AcquireFunc
>(cpp_acquire
) );
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!" );
212 TYPELIB_DANGER_RELEASE( pObjTD
);
213 if (coerce_assign( const_cast<char *>(static_cast<char const *>(rObj
.getValue()) + _nOffset
), getTypeDescr(), rValue
, getReflection() ))
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!" );
248 TYPELIB_DANGER_RELEASE( pObjTD
);
249 if (coerce_assign( const_cast<char *>(static_cast<char const *>(rObj
.getValue()) + _nOffset
), getTypeDescr(), rValue
, getReflection() ))
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
)
277 TypeClass eTC
= xType
->getTypeClass();
278 if (eTC
== TypeClass_STRUCT
|| eTC
== TypeClass_EXCEPTION
)
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] );
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
;
305 _xSuperClass
= getReflection()->forType( &pCompTypeDescr
->aBase
);
308 if (_xSuperClass
.is())
309 return Sequence
< Reference
< XIdlClass
> >( &_xSuperClass
, 1 );
311 return Sequence
< Reference
< XIdlClass
> >();
314 Reference
< XIdlField
> CompoundIdlClassImpl::getField( const OUString
& rName
)
317 getFields(); // init fields
319 const OUString2Field::const_iterator
iFind( _aName2Field
.find( rName
) );
320 if (iFind
!= _aName2Field
.end())
321 return Reference
< XIdlField
>( (*iFind
).second
);
323 return Reference
< XIdlField
>();
326 Sequence
< Reference
< XIdlField
> > CompoundIdlClassImpl::getFields()
328 ::osl::MutexGuard
aGuard( getMutexAccess() );
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!" );
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
);
370 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */