LibreOffice Module stoc (master) 1
crcomp.cxx
Go to the documentation of this file.
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
22
23#include <com/sun/star/reflection/XIdlField.hpp>
24#include <com/sun/star/reflection/XIdlField2.hpp>
25#include <com/sun/star/uno/TypeClass.hpp>
26
27#include "base.hxx"
28
29using namespace css::lang;
30using namespace css::reflection;
31using namespace css::uno;
32
33namespace stoc_corefl
34{
35
36namespace {
37
38typedef cppu::ImplInheritanceHelper<IdlMemberImpl, XIdlField, XIdlField2> IdlCompFieldImpl_Base;
39class IdlCompFieldImpl : public IdlCompFieldImpl_Base
40{
41 sal_Int32 _nOffset;
42
43public:
44 IdlCompFieldImpl( IdlReflectionServiceImpl * pReflection, const OUString & rName,
45 typelib_TypeDescription * pTypeDescr, typelib_TypeDescription * pDeclTypeDescr,
46 sal_Int32 nOffset )
47 : IdlCompFieldImpl_Base( pReflection, rName, pTypeDescr, pDeclTypeDescr )
48 , _nOffset( nOffset )
49 {}
50
51 // XIdlMember
52 virtual Reference< XIdlClass > SAL_CALL getDeclaringClass() override;
53 virtual OUString SAL_CALL getName() override;
54 // XIdlField
55 virtual Reference< XIdlClass > SAL_CALL getType() override;
56 virtual FieldAccessMode SAL_CALL getAccessMode() override;
57 virtual Any SAL_CALL get( const Any & rObj ) override;
58 virtual void SAL_CALL set( const Any & rObj, const Any & rValue ) override;
59 // XIdlField2: getType, getAccessMode and get are equal to XIdlField
60 virtual void SAL_CALL set( Any & rObj, const Any & rValue ) override;
61};
62
63}
64
65// XIdlMember
66
67Reference< XIdlClass > IdlCompFieldImpl::getDeclaringClass()
68{
69 if (! _xDeclClass.is())
70 {
71 ::osl::MutexGuard aGuard( getMutexAccess() );
72 if (! _xDeclClass.is())
73 {
74 typelib_CompoundTypeDescription * pTD =
75 reinterpret_cast<typelib_CompoundTypeDescription *>(getDeclTypeDescr());
76 while (pTD)
77 {
78 typelib_TypeDescriptionReference ** ppTypeRefs = pTD->ppTypeRefs;
79 for ( sal_Int32 nPos = pTD->nMembers; nPos--; )
80 {
81 if (td_equals( getTypeDescr(), ppTypeRefs[nPos] ))
82 {
83 _xDeclClass = getReflection()->forType( &pTD->aBase );
84 return _xDeclClass;
85 }
86 }
87 pTD = pTD->pBaseTypeDescription;
88 }
89 }
90 }
91 return _xDeclClass;
92}
93
94OUString IdlCompFieldImpl::getName()
95{
97}
98
99// XIdlField
100
101Reference< XIdlClass > IdlCompFieldImpl::getType()
102{
103 return getReflection()->forType( getTypeDescr() );
104}
105
106FieldAccessMode IdlCompFieldImpl::getAccessMode()
107{
108 return FieldAccessMode_READWRITE;
109}
110
111Any IdlCompFieldImpl::get( const Any & rObj )
112{
113 if (rObj.getValueTypeClass() == css::uno::TypeClass_STRUCT ||
114 rObj.getValueTypeClass() == css::uno::TypeClass_EXCEPTION)
115 {
116 typelib_TypeDescription * pObjTD = nullptr;
117 TYPELIB_DANGER_GET( &pObjTD, rObj.getValueTypeRef() );
118
119 typelib_TypeDescription * pTD = pObjTD;
120 typelib_TypeDescription * pDeclTD = getDeclTypeDescr();
121 while (pTD && !typelib_typedescription_equals( pTD, pDeclTD ))
122 pTD = &reinterpret_cast<typelib_CompoundTypeDescription *>(pTD)->pBaseTypeDescription->aBase;
123
124 OSL_ENSURE( pTD, "### illegal object type!" );
125 if (pTD)
126 {
127 TYPELIB_DANGER_RELEASE( pObjTD );
128 Any aRet;
130 &aRet, reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
132 &aRet, const_cast<char *>(static_cast<char const *>(rObj.getValue()) + _nOffset), getTypeDescr(),
133 reinterpret_cast< uno_AcquireFunc >(cpp_acquire) );
134 return aRet;
135 }
136 TYPELIB_DANGER_RELEASE( pObjTD );
137 }
138 throw IllegalArgumentException(
139 "expected struct or exception, got " + rObj.getValueType().getTypeName(),
140 getXWeak(), 0 );
141}
142
143void IdlCompFieldImpl::set( const Any & rObj, const Any & rValue )
144{
145 if (rObj.getValueTypeClass() == css::uno::TypeClass_STRUCT ||
146 rObj.getValueTypeClass() == css::uno::TypeClass_EXCEPTION)
147 {
148 typelib_TypeDescription * pObjTD = nullptr;
149 TYPELIB_DANGER_GET( &pObjTD, rObj.getValueTypeRef() );
150
151 typelib_TypeDescription * pTD = pObjTD;
152 typelib_TypeDescription * pDeclTD = getDeclTypeDescr();
153 while (pTD && !typelib_typedescription_equals( pTD, pDeclTD ))
154 pTD = &reinterpret_cast<typelib_CompoundTypeDescription *>(pTD)->pBaseTypeDescription->aBase;
155
156 OSL_ENSURE( pTD, "### illegal object type!" );
157 if (pTD)
158 {
159 TYPELIB_DANGER_RELEASE( pObjTD );
160 if (!coerce_assign( const_cast<char *>(static_cast<char const *>(rObj.getValue()) + _nOffset), getTypeDescr(), rValue, getReflection() ))
161 {
162 throw IllegalArgumentException(
163 "cannot assign value to destination",
164 getXWeak(), 1 );
165 }
166 return;
167 }
168 TYPELIB_DANGER_RELEASE( pObjTD );
169 }
170 throw IllegalArgumentException(
171 "expected struct or exception, got " + rObj.getValueType().getTypeName(),
172 getXWeak(), 0 );
173}
174
175
176void IdlCompFieldImpl::set( Any & rObj, const Any & rValue )
177{
178 if (rObj.getValueTypeClass() == css::uno::TypeClass_STRUCT ||
179 rObj.getValueTypeClass() == css::uno::TypeClass_EXCEPTION)
180 {
181 typelib_TypeDescription * pObjTD = nullptr;
182 TYPELIB_DANGER_GET( &pObjTD, rObj.getValueTypeRef() );
183
184 typelib_TypeDescription * pTD = pObjTD;
185 typelib_TypeDescription * pDeclTD = getDeclTypeDescr();
186 while (pTD && !typelib_typedescription_equals( pTD, pDeclTD ))
187 pTD = &reinterpret_cast<typelib_CompoundTypeDescription *>(pTD)->pBaseTypeDescription->aBase;
188
189 OSL_ENSURE( pTD, "### illegal object type!" );
190 if (pTD)
191 {
192 TYPELIB_DANGER_RELEASE( pObjTD );
193 if (!coerce_assign( const_cast<char *>(static_cast<char const *>(rObj.getValue()) + _nOffset), getTypeDescr(), rValue, getReflection() ))
194 {
195 throw IllegalArgumentException(
196 "cannot assign to destination",
197 getXWeak(), 1 );
198 }
199 return;
200 }
201 TYPELIB_DANGER_RELEASE( pObjTD );
202 }
203 throw IllegalArgumentException(
204 "expected struct or exception, got " + rObj.getValueType().getTypeName(),
205 getXWeak(), 0 );
206}
207
208
210{
211}
212
213
214sal_Bool CompoundIdlClassImpl::isAssignableFrom( const Reference< XIdlClass > & xType )
215{
216 if (xType.is())
217 {
218 TypeClass eTC = xType->getTypeClass();
219 if (eTC == TypeClass_STRUCT || eTC == TypeClass_EXCEPTION)
220 {
221 if (equals( xType ))
222 return true;
223 else
224 {
225 const Sequence< Reference< XIdlClass > > & rSeq = xType->getSuperclasses();
226 if (rSeq.hasElements())
227 {
228 OSL_ENSURE( rSeq.getLength() == 1, "### unexpected len of super classes!" );
229 return isAssignableFrom( rSeq[0] );
230 }
231 }
232 }
233 }
234 return false;
235}
236
237Sequence< Reference< XIdlClass > > CompoundIdlClassImpl::getSuperclasses()
238{
239 if (! _xSuperClass.is())
240 {
241 ::osl::MutexGuard aGuard( getMutexAccess() );
242 if (! _xSuperClass.is())
243 {
244 typelib_CompoundTypeDescription * pCompTypeDescr = getTypeDescr()->pBaseTypeDescription;
245 if (pCompTypeDescr)
246 _xSuperClass = getReflection()->forType( &pCompTypeDescr->aBase );
247 }
248 }
249 if (_xSuperClass.is())
250 return Sequence< Reference< XIdlClass > >( &_xSuperClass, 1 );
251 else
252 return Sequence< Reference< XIdlClass > >();
253}
254
255Reference< XIdlField > CompoundIdlClassImpl::getField( const OUString & rName )
256{
257 if (! m_xFields)
258 getFields(); // init fields
259
260 const OUString2Field::const_iterator iFind( _aName2Field.find( rName ) );
261 if (iFind != _aName2Field.end())
262 return Reference< XIdlField >( (*iFind).second );
263 else
264 return Reference< XIdlField >();
265}
266
267Sequence< Reference< XIdlField > > CompoundIdlClassImpl::getFields()
268{
269 ::osl::MutexGuard aGuard( getMutexAccess() );
270 if (! m_xFields)
271 {
272 sal_Int32 nAll = 0;
273 typelib_CompoundTypeDescription * pCompTypeDescr = getTypeDescr();
274 for ( ; pCompTypeDescr; pCompTypeDescr = pCompTypeDescr->pBaseTypeDescription )
275 nAll += pCompTypeDescr->nMembers;
276
277 Sequence< Reference< XIdlField > > aFields( nAll );
278 Reference< XIdlField > * pSeq = aFields.getArray();
279
280 for ( pCompTypeDescr = getTypeDescr(); pCompTypeDescr;
281 pCompTypeDescr = pCompTypeDescr->pBaseTypeDescription )
282 {
283 typelib_TypeDescriptionReference ** ppTypeRefs = pCompTypeDescr->ppTypeRefs;
284 rtl_uString ** ppNames = pCompTypeDescr->ppMemberNames;
285 sal_Int32 * pMemberOffsets = pCompTypeDescr->pMemberOffsets;
286
287 for ( sal_Int32 nPos = pCompTypeDescr->nMembers; nPos--; )
288 {
289 typelib_TypeDescription * pTD = nullptr;
290 TYPELIB_DANGER_GET( &pTD, ppTypeRefs[nPos] );
291 OSL_ENSURE( pTD, "### cannot get field in struct!" );
292 if (pTD)
293 {
294 OUString aName( ppNames[nPos] );
295 _aName2Field[aName] = pSeq[--nAll] = new IdlCompFieldImpl(
296 getReflection(), aName, pTD, IdlClassImpl::getTypeDescr(), pMemberOffsets[nPos] );
297 TYPELIB_DANGER_RELEASE( pTD );
298 }
299 }
300 }
301
302 m_xFields = std::move( aFields );
303 }
304 return *m_xFields;
305}
306
307}
308
309
310/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SAL_CALL uno_any_construct(uno_Any *pDest, void *pSource, typelib_TypeDescription *pTypeDescr, uno_AcquireFunc acquire) SAL_THROW_EXTERN_C()
void SAL_CALL uno_any_destruct(uno_Any *pValue, uno_ReleaseFunc release) SAL_THROW_EXTERN_C()
virtual ~CompoundIdlClassImpl() override
Definition: crcomp.cxx:209
typelib_CompoundTypeDescription * getTypeDescr() const
Definition: base.hxx:230
virtual css::uno::Reference< css::reflection::XIdlField > SAL_CALL getField(const OUString &rName) override
Definition: crcomp.cxx:255
std::optional< css::uno::Sequence< css::uno::Reference< css::reflection::XIdlField > > > m_xFields
Definition: base.hxx:226
virtual sal_Bool SAL_CALL isAssignableFrom(const css::uno::Reference< css::reflection::XIdlClass > &xType) override
Definition: crcomp.cxx:214
css::uno::Reference< css::reflection::XIdlClass > _xSuperClass
Definition: base.hxx:224
virtual css::uno::Sequence< css::uno::Reference< css::reflection::XIdlField > > SAL_CALL getFields() override
Definition: crcomp.cxx:267
virtual css::uno::Sequence< css::uno::Reference< css::reflection::XIdlClass > > SAL_CALL getSuperclasses() override
Definition: crcomp.cxx:237
IdlReflectionServiceImpl * getReflection() const
Definition: base.hxx:142
typelib_TypeDescription * getTypeDescr() const
Definition: base.hxx:140
virtual sal_Bool SAL_CALL equals(const css::uno::Reference< css::reflection::XIdlClass > &xType) override
Definition: crbase.cxx:92
virtual OUString SAL_CALL getName() override
Definition: crbase.cxx:242
css::uno::Reference< css::reflection::XIdlClass > forType(typelib_TypeDescription *pTypeDescr)
Definition: crefl.cxx:235
sal_Int32 _nOffset
Definition: crcomp.cxx:41
OUString aName
Definition: invocation.cxx:689
sal_uInt16 nPos
struct _typelib_TypeDescription typelib_TypeDescription
void set(css::uno::UnoInterfaceReference const &value)
::osl::Mutex & getMutexAccess()
Definition: crbase.cxx:37
bool coerce_assign(void *pDest, typelib_TypeDescription *pTD, const css::uno::Any &rSource, IdlReflectionServiceImpl *pRefl)
Definition: base.hxx:363
bool td_equals(typelib_TypeDescription *pTD, typelib_TypeDescriptionReference *pType)
Definition: base.hxx:65
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
eTC
bool getType(BSTR name, Type &type)
sal_Bool SAL_CALL typelib_typedescription_equals(const typelib_TypeDescription *p1, const typelib_TypeDescription *p2) SAL_THROW_EXTERN_C()
unsigned char sal_Bool