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() ))
215 throw IllegalArgumentException(
216 "cannot assign value to destination",
217 static_cast<XWeak
*>(static_cast<OWeakObject
*>(this)), 1 );
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!" );
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 );
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
)
271 TypeClass eTC
= xType
->getTypeClass();
272 if (eTC
== TypeClass_STRUCT
|| eTC
== TypeClass_EXCEPTION
)
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] );
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
;
299 _xSuperClass
= getReflection()->forType( &pCompTypeDescr
->aBase
);
302 if (_xSuperClass
.is())
303 return Sequence
< Reference
< XIdlClass
> >( &_xSuperClass
, 1 );
305 return Sequence
< Reference
< XIdlClass
> >();
308 Reference
< XIdlField
> CompoundIdlClassImpl::getField( const OUString
& rName
)
311 getFields(); // init fields
313 const OUString2Field::const_iterator
iFind( _aName2Field
.find( rName
) );
314 if (iFind
!= _aName2Field
.end())
315 return Reference
< XIdlField
>( (*iFind
).second
);
317 return Reference
< XIdlField
>();
320 Sequence
< Reference
< XIdlField
> > CompoundIdlClassImpl::getFields()
322 ::osl::MutexGuard
aGuard( getMutexAccess() );
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!" );
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
);
364 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */