Line data Source code
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 :
20 : #include <rtl/strbuf.hxx>
21 :
22 : #include <com/sun/star/reflection/XIdlField.hpp>
23 : #include <com/sun/star/reflection/XIdlField2.hpp>
24 : #include <com/sun/star/uno/TypeClass.hpp>
25 :
26 : #include "base.hxx"
27 :
28 : using namespace css::lang;
29 : using namespace css::reflection;
30 : using namespace css::uno;
31 :
32 : namespace stoc_corefl
33 : {
34 :
35 :
36 328 : class IdlCompFieldImpl
37 : : public IdlMemberImpl
38 : , public XIdlField
39 : , public XIdlField2
40 : {
41 : sal_Int32 _nOffset;
42 :
43 : public:
44 306 : IdlCompFieldImpl( IdlReflectionServiceImpl * pReflection, const OUString & rName,
45 : typelib_TypeDescription * pTypeDescr, typelib_TypeDescription * pDeclTypeDescr,
46 : sal_Int32 nOffset )
47 : : IdlMemberImpl( pReflection, rName, pTypeDescr, pDeclTypeDescr )
48 306 : , _nOffset( nOffset )
49 306 : {}
50 :
51 : // XInterface
52 : virtual Any SAL_CALL queryInterface( const Type & rType ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
53 : virtual void SAL_CALL acquire() throw () SAL_OVERRIDE;
54 : virtual void SAL_CALL release() throw () SAL_OVERRIDE;
55 :
56 : // XTypeProvider
57 : virtual Sequence< Type > SAL_CALL getTypes() throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
58 : virtual Sequence< sal_Int8 > SAL_CALL getImplementationId() throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
59 :
60 : // XIdlMember
61 : virtual Reference< XIdlClass > SAL_CALL getDeclaringClass() throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
62 : virtual OUString SAL_CALL getName() throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
63 : // XIdlField
64 : virtual Reference< XIdlClass > SAL_CALL getType() throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
65 : virtual FieldAccessMode SAL_CALL getAccessMode() throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
66 : virtual Any SAL_CALL get( const Any & rObj ) throw(css::lang::IllegalArgumentException, css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
67 : virtual void SAL_CALL set( const Any & rObj, const Any & rValue ) throw(css::lang::IllegalArgumentException, css::lang::IllegalAccessException, css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
68 : // XIdlField2: getType, getAccessMode and get are equal to XIdlField
69 : virtual void SAL_CALL set( Any & rObj, const Any & rValue ) throw(css::lang::IllegalArgumentException, css::lang::IllegalAccessException, css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
70 : };
71 :
72 : // XInterface
73 :
74 552 : Any IdlCompFieldImpl::queryInterface( const Type & rType )
75 : throw(css::uno::RuntimeException, std::exception)
76 : {
77 : Any aRet( ::cppu::queryInterface( rType,
78 : static_cast< XIdlField * >( this ),
79 552 : static_cast< XIdlField2 * >( this ) ) );
80 552 : return (aRet.hasValue() ? aRet : IdlMemberImpl::queryInterface( rType ));
81 : }
82 :
83 2838 : void IdlCompFieldImpl::acquire() throw()
84 : {
85 2838 : IdlMemberImpl::acquire();
86 2838 : }
87 :
88 2554 : void IdlCompFieldImpl::release() throw()
89 : {
90 2554 : IdlMemberImpl::release();
91 2554 : }
92 :
93 : // XTypeProvider
94 :
95 0 : Sequence< Type > IdlCompFieldImpl::getTypes()
96 : throw (css::uno::RuntimeException, std::exception)
97 : {
98 : static ::cppu::OTypeCollection * s_pTypes = 0;
99 0 : if (! s_pTypes)
100 : {
101 0 : ::osl::MutexGuard aGuard( getMutexAccess() );
102 0 : if (! s_pTypes)
103 : {
104 : static ::cppu::OTypeCollection s_aTypes(
105 0 : cppu::UnoType<XIdlField2>::get(),
106 0 : cppu::UnoType<XIdlField>::get(),
107 0 : IdlMemberImpl::getTypes() );
108 0 : s_pTypes = &s_aTypes;
109 0 : }
110 : }
111 0 : return s_pTypes->getTypes();
112 : }
113 :
114 0 : Sequence< sal_Int8 > IdlCompFieldImpl::getImplementationId()
115 : throw (css::uno::RuntimeException, std::exception)
116 : {
117 0 : return css::uno::Sequence<sal_Int8>();
118 : }
119 :
120 : // XIdlMember
121 :
122 0 : Reference< XIdlClass > IdlCompFieldImpl::getDeclaringClass()
123 : throw(css::uno::RuntimeException, std::exception)
124 : {
125 0 : if (! _xDeclClass.is())
126 : {
127 0 : ::osl::MutexGuard aGuard( getMutexAccess() );
128 0 : if (! _xDeclClass.is())
129 : {
130 : typelib_CompoundTypeDescription * pTD =
131 0 : (typelib_CompoundTypeDescription *)getDeclTypeDescr();
132 0 : while (pTD)
133 : {
134 0 : typelib_TypeDescriptionReference ** ppTypeRefs = pTD->ppTypeRefs;
135 0 : for ( sal_Int32 nPos = pTD->nMembers; nPos--; )
136 : {
137 0 : if (td_equals( (typelib_TypeDescription *)getTypeDescr(), ppTypeRefs[nPos] ))
138 : {
139 0 : _xDeclClass = getReflection()->forType( (typelib_TypeDescription *)pTD );
140 0 : return _xDeclClass;
141 : }
142 : }
143 0 : pTD = pTD->pBaseTypeDescription;
144 : }
145 0 : }
146 : }
147 0 : return _xDeclClass;
148 : }
149 :
150 306 : OUString IdlCompFieldImpl::getName()
151 : throw(css::uno::RuntimeException, std::exception)
152 : {
153 306 : return IdlMemberImpl::getName();
154 : }
155 :
156 : // XIdlField
157 :
158 306 : Reference< XIdlClass > IdlCompFieldImpl::getType()
159 : throw(css::uno::RuntimeException, std::exception)
160 : {
161 306 : return getReflection()->forType( getTypeDescr() );
162 : }
163 :
164 306 : FieldAccessMode IdlCompFieldImpl::getAccessMode()
165 : throw(css::uno::RuntimeException, std::exception)
166 : {
167 306 : return FieldAccessMode_READWRITE;
168 : }
169 :
170 876 : Any IdlCompFieldImpl::get( const Any & rObj )
171 : throw(css::lang::IllegalArgumentException, css::uno::RuntimeException, std::exception)
172 : {
173 876 : if (rObj.getValueTypeClass() == css::uno::TypeClass_STRUCT ||
174 0 : rObj.getValueTypeClass() == css::uno::TypeClass_EXCEPTION)
175 : {
176 876 : typelib_TypeDescription * pObjTD = 0;
177 876 : TYPELIB_DANGER_GET( &pObjTD, rObj.getValueTypeRef() );
178 :
179 876 : typelib_TypeDescription * pTD = pObjTD;
180 876 : typelib_TypeDescription * pDeclTD = getDeclTypeDescr();
181 1752 : while (pTD && !typelib_typedescription_equals( pTD, pDeclTD ))
182 0 : pTD = (typelib_TypeDescription *)((typelib_CompoundTypeDescription *)pTD)->pBaseTypeDescription;
183 :
184 : OSL_ENSURE( pTD, "### illegal object type!" );
185 876 : if (pTD)
186 : {
187 876 : TYPELIB_DANGER_RELEASE( pObjTD );
188 876 : Any aRet;
189 : uno_any_destruct(
190 876 : &aRet, reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
191 : uno_any_construct(
192 1752 : &aRet, (char *)rObj.getValue() + _nOffset, getTypeDescr(),
193 2628 : reinterpret_cast< uno_AcquireFunc >(cpp_acquire) );
194 1752 : return aRet;
195 : }
196 0 : TYPELIB_DANGER_RELEASE( pObjTD );
197 : }
198 : throw IllegalArgumentException(
199 : "illegal object given!",
200 0 : (XWeak *)(OWeakObject *)this, 0 );
201 : }
202 :
203 0 : void IdlCompFieldImpl::set( const Any & rObj, const Any & rValue )
204 : throw(css::lang::IllegalArgumentException, css::lang::IllegalAccessException, css::uno::RuntimeException, std::exception)
205 : {
206 0 : if (rObj.getValueTypeClass() == css::uno::TypeClass_STRUCT ||
207 0 : rObj.getValueTypeClass() == css::uno::TypeClass_EXCEPTION)
208 : {
209 0 : typelib_TypeDescription * pObjTD = 0;
210 0 : TYPELIB_DANGER_GET( &pObjTD, rObj.getValueTypeRef() );
211 :
212 0 : typelib_TypeDescription * pTD = pObjTD;
213 0 : typelib_TypeDescription * pDeclTD = getDeclTypeDescr();
214 0 : while (pTD && !typelib_typedescription_equals( pTD, pDeclTD ))
215 0 : pTD = (typelib_TypeDescription *)((typelib_CompoundTypeDescription *)pTD)->pBaseTypeDescription;
216 :
217 : OSL_ENSURE( pTD, "### illegal object type!" );
218 0 : if (pTD)
219 : {
220 0 : TYPELIB_DANGER_RELEASE( pObjTD );
221 0 : if (coerce_assign( (char *)rObj.getValue() + _nOffset, getTypeDescr(), rValue, getReflection() ))
222 : {
223 0 : return;
224 : }
225 : else
226 : {
227 : throw IllegalArgumentException(
228 : "illegal value given!",
229 0 : (XWeak *)(OWeakObject *)this, 1 );
230 : }
231 : }
232 0 : TYPELIB_DANGER_RELEASE( pObjTD );
233 : }
234 : throw IllegalArgumentException(
235 : "illegal object given!",
236 0 : (XWeak *)(OWeakObject *)this, 0 );
237 : }
238 :
239 :
240 246 : void IdlCompFieldImpl::set( Any & rObj, const Any & rValue )
241 : throw(css::lang::IllegalArgumentException, css::lang::IllegalAccessException, css::uno::RuntimeException, std::exception)
242 : {
243 246 : if (rObj.getValueTypeClass() == css::uno::TypeClass_STRUCT ||
244 0 : rObj.getValueTypeClass() == css::uno::TypeClass_EXCEPTION)
245 : {
246 246 : typelib_TypeDescription * pObjTD = 0;
247 246 : TYPELIB_DANGER_GET( &pObjTD, rObj.getValueTypeRef() );
248 :
249 246 : typelib_TypeDescription * pTD = pObjTD;
250 246 : typelib_TypeDescription * pDeclTD = getDeclTypeDescr();
251 492 : while (pTD && !typelib_typedescription_equals( pTD, pDeclTD ))
252 0 : pTD = (typelib_TypeDescription *)((typelib_CompoundTypeDescription *)pTD)->pBaseTypeDescription;
253 :
254 : OSL_ENSURE( pTD, "### illegal object type!" );
255 246 : if (pTD)
256 : {
257 246 : TYPELIB_DANGER_RELEASE( pObjTD );
258 246 : if (coerce_assign( (char *)rObj.getValue() + _nOffset, getTypeDescr(), rValue, getReflection() ))
259 : {
260 492 : return;
261 : }
262 : else
263 : {
264 : throw IllegalArgumentException(
265 : "illegal value given!",
266 0 : (XWeak *)(OWeakObject *)this, 1 );
267 : }
268 : }
269 0 : TYPELIB_DANGER_RELEASE( pObjTD );
270 : }
271 : throw IllegalArgumentException(
272 : "illegal object given!",
273 0 : (XWeak *)(OWeakObject *)this, 0 );
274 : }
275 :
276 :
277 :
278 :
279 :
280 :
281 :
282 1566 : CompoundIdlClassImpl::~CompoundIdlClassImpl()
283 : {
284 522 : delete _pFields;
285 1044 : }
286 :
287 :
288 34 : sal_Bool CompoundIdlClassImpl::isAssignableFrom( const Reference< XIdlClass > & xType )
289 : throw(css::uno::RuntimeException, std::exception)
290 : {
291 34 : if (xType.is())
292 : {
293 34 : TypeClass eTC = xType->getTypeClass();
294 34 : if (eTC == TypeClass_STRUCT || eTC == TypeClass_EXCEPTION)
295 : {
296 14 : if (equals( xType ))
297 14 : return sal_True;
298 : else
299 : {
300 0 : const Sequence< Reference< XIdlClass > > & rSeq = xType->getSuperclasses();
301 0 : if (rSeq.getLength())
302 : {
303 : OSL_ENSURE( rSeq.getLength() == 1, "### unexpected len of super classes!" );
304 0 : return isAssignableFrom( rSeq[0] );
305 0 : }
306 : }
307 : }
308 : }
309 20 : return sal_False;
310 : }
311 :
312 0 : Sequence< Reference< XIdlClass > > CompoundIdlClassImpl::getSuperclasses()
313 : throw(css::uno::RuntimeException, std::exception)
314 : {
315 0 : if (! _xSuperClass.is())
316 : {
317 0 : ::osl::MutexGuard aGuard( getMutexAccess() );
318 0 : if (! _xSuperClass.is())
319 : {
320 0 : typelib_CompoundTypeDescription * pCompTypeDescr = getTypeDescr()->pBaseTypeDescription;
321 0 : if (pCompTypeDescr)
322 0 : _xSuperClass = getReflection()->forType( (typelib_TypeDescription *)pCompTypeDescr );
323 0 : }
324 : }
325 0 : if (_xSuperClass.is())
326 0 : return Sequence< Reference< XIdlClass > >( &_xSuperClass, 1 );
327 : else
328 0 : return Sequence< Reference< XIdlClass > >();
329 : }
330 :
331 0 : Reference< XIdlField > CompoundIdlClassImpl::getField( const OUString & rName )
332 : throw(css::uno::RuntimeException, std::exception)
333 : {
334 0 : if (! _pFields)
335 0 : getFields(); // init fields
336 :
337 0 : const OUString2Field::const_iterator iFind( _aName2Field.find( rName ) );
338 0 : if (iFind != _aName2Field.end())
339 0 : return Reference< XIdlField >( (*iFind).second );
340 : else
341 0 : return Reference< XIdlField >();
342 : }
343 :
344 66 : Sequence< Reference< XIdlField > > CompoundIdlClassImpl::getFields()
345 : throw(css::uno::RuntimeException, std::exception)
346 : {
347 66 : ::osl::MutexGuard aGuard( getMutexAccess() );
348 66 : if (! _pFields)
349 : {
350 66 : sal_Int32 nAll = 0;
351 66 : typelib_CompoundTypeDescription * pCompTypeDescr = getTypeDescr();
352 154 : for ( ; pCompTypeDescr; pCompTypeDescr = pCompTypeDescr->pBaseTypeDescription )
353 88 : nAll += pCompTypeDescr->nMembers;
354 :
355 : Sequence< Reference< XIdlField > > * pFields =
356 66 : new Sequence< Reference< XIdlField > >( nAll );
357 66 : Reference< XIdlField > * pSeq = pFields->getArray();
358 :
359 154 : for ( pCompTypeDescr = getTypeDescr(); pCompTypeDescr;
360 : pCompTypeDescr = pCompTypeDescr->pBaseTypeDescription )
361 : {
362 88 : typelib_TypeDescriptionReference ** ppTypeRefs = pCompTypeDescr->ppTypeRefs;
363 88 : rtl_uString ** ppNames = pCompTypeDescr->ppMemberNames;
364 88 : sal_Int32 * pMemberOffsets = pCompTypeDescr->pMemberOffsets;
365 :
366 482 : for ( sal_Int32 nPos = pCompTypeDescr->nMembers; nPos--; )
367 : {
368 306 : typelib_TypeDescription * pTD = 0;
369 306 : TYPELIB_DANGER_GET( &pTD, ppTypeRefs[nPos] );
370 : OSL_ENSURE( pTD, "### cannot get field in struct!" );
371 306 : if (pTD)
372 : {
373 306 : OUString aName( ppNames[nPos] );
374 1224 : _aName2Field[aName] = pSeq[--nAll] = new IdlCompFieldImpl(
375 1530 : getReflection(), aName, pTD, IdlClassImpl::getTypeDescr(), pMemberOffsets[nPos] );
376 306 : TYPELIB_DANGER_RELEASE( pTD );
377 : }
378 : }
379 : }
380 :
381 66 : _pFields = pFields;
382 : }
383 66 : return *_pFields;
384 : }
385 :
386 : }
387 :
388 :
389 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|