LibreOffice Module cppuhelper (master)  1
implbase_ex.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 
20 #include <osl/diagnose.h>
21 #include <sal/log.hxx>
24 
25 #include <com/sun/star/uno/RuntimeException.hpp>
26 
27 using namespace ::cppu;
28 using namespace ::osl;
29 using namespace ::com::sun::star;
30 using namespace ::com::sun::star::uno;
31 
32 namespace
33 {
34  class theImplHelperInitMutex : public rtl::Static<Mutex, theImplHelperInitMutex>{};
35 }
36 
37 namespace cppu
38 {
39 
43 static ::osl::Mutex & getImplHelperInitMutex()
44 {
45  return theImplHelperInitMutex::get();
46 }
47 
48 
49 static void checkInterface( Type const & rType )
50 {
51  if (TypeClass_INTERFACE != rType.getTypeClass())
52  {
53  OUString msg( "querying for interface \"" + rType.getTypeName() + "\": no interface type!" );
54  SAL_WARN( "cppuhelper", msg );
55  throw RuntimeException( msg );
56  }
57 }
58 
59 static bool isXInterface( rtl_uString * pStr )
60 {
61  return OUString::unacquired(&pStr) == "com.sun.star.uno.XInterface";
62 }
63 
64 static void * makeInterface( sal_IntPtr nOffset, void * that )
65 {
66  return (static_cast<char *>(that) + nOffset);
67 }
68 
69 static bool td_equals(
70  typelib_TypeDescriptionReference const * pTDR1,
71  typelib_TypeDescriptionReference const * pTDR2 )
72 {
73  return ((pTDR1 == pTDR2) ||
74  OUString::unacquired(&pTDR1->pTypeName) == OUString::unacquired(&pTDR2->pTypeName));
75 }
76 
77 static type_entry * getTypeEntries( class_data * cd )
78 {
79  type_entry * pEntries = cd->m_typeEntries;
80  if (! cd->m_storedTypeRefs) // not inited?
81  {
82  MutexGuard guard( getImplHelperInitMutex() );
83  if (! cd->m_storedTypeRefs) // not inited?
84  {
85  // get all types
86  for ( sal_Int32 n = cd->m_nTypes; n--; )
87  {
88  type_entry * pEntry = &pEntries[ n ];
89  Type const & rType = (*pEntry->m_type.getCppuType)( nullptr );
90  OSL_ENSURE( rType.getTypeClass() == TypeClass_INTERFACE, "### wrong helper init: expected interface!" );
91  OSL_ENSURE( ! isXInterface( rType.getTypeLibType()->pTypeName ), "### want to implement XInterface: template argument is XInterface?!?!?!" );
92  if (rType.getTypeClass() != TypeClass_INTERFACE)
93  {
94  OUString msg( "type \"" + rType.getTypeName() + "\" is no interface type!" );
95  SAL_WARN( "cppuhelper", msg );
96  throw RuntimeException( msg );
97  }
98  // ref is statically held by getCppuType()
99  pEntry->m_type.typeRef = rType.getTypeLibType();
100  }
101  OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
102  cd->m_storedTypeRefs = true;
103  }
104  }
105  else
106  {
107  OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
108  }
109  return pEntries;
110 }
111 
112 static void fillTypes( Type * types, class_data * cd )
113 {
114  type_entry * pEntries = getTypeEntries( cd );
115  for ( sal_Int32 n = cd->m_nTypes; n--; )
116  {
117  types[ n ] = pEntries[ n ].m_type.typeRef;
118  }
119 }
120 
121 namespace {
122 
123 bool recursivelyFindType(
124  typelib_TypeDescriptionReference const * demandedType,
125  typelib_InterfaceTypeDescription const * type, sal_IntPtr * offset)
126 {
127  // This code assumes that the vtables of a multiple-inheritance class (the
128  // offset amount by which to adjust the this pointer) follow one another in
129  // the object layout, and that they contain slots for the inherited classes
130  // in a specific order. In theory, that need not hold for any given
131  // platform; in practice, it seems to work well on all supported platforms:
132  next:
133  for (sal_Int32 i = 0; i < type->nBaseTypes; ++i) {
134  if (i > 0) {
135  *offset += sizeof (void *);
136  }
137  typelib_InterfaceTypeDescription const * base = type->ppBaseTypes[i];
138  // ignore XInterface:
139  if (base->nBaseTypes > 0) {
140  if (td_equals(
141  reinterpret_cast<
142  typelib_TypeDescriptionReference const * >(base),
143  demandedType))
144  {
145  return true;
146  }
147  // Profiling showed that it is important to speed up the common case
148  // of only one base:
149  if (type->nBaseTypes == 1) {
150  type = base;
151  goto next;
152  }
153  if (recursivelyFindType(demandedType, base, offset)) {
154  return true;
155  }
156  }
157  }
158  return false;
159 }
160 
161 }
162 
163 static void * queryDeepNoXInterface(
164  typelib_TypeDescriptionReference const * pDemandedTDR, class_data * cd, void * that )
165 {
166  type_entry * pEntries = getTypeEntries( cd );
167  sal_Int32 nTypes = cd->m_nTypes;
168  sal_Int32 n;
169 
170  // try top interfaces without getting td
171  for ( n = 0; n < nTypes; ++n )
172  {
173  if (td_equals( pEntries[ n ].m_type.typeRef, pDemandedTDR ))
174  {
175  return makeInterface( pEntries[ n ].m_offset, that );
176  }
177  }
178  // query deep getting td
179  for ( n = 0; n < nTypes; ++n )
180  {
181  typelib_TypeDescription * pTD = nullptr;
182  TYPELIB_DANGER_GET( &pTD, pEntries[ n ].m_type.typeRef );
183  if (pTD)
184  {
185  // exclude top (already tested) and bottom (XInterface) interface
186  OSL_ENSURE(
187  reinterpret_cast< typelib_InterfaceTypeDescription * >(pTD)->
188  nBaseTypes > 0,
189  "### want to implement XInterface:"
190  " template argument is XInterface?!?!?!" );
191  sal_IntPtr offset = pEntries[n].m_offset;
192  bool found = recursivelyFindType(
193  pDemandedTDR,
194  reinterpret_cast< typelib_InterfaceTypeDescription * >(pTD),
195  &offset);
196  TYPELIB_DANGER_RELEASE( pTD );
197  if (found) {
198  return makeInterface( offset, that );
199  }
200  }
201  else
202  {
203  OUString msg( "cannot get type description for type \"" + OUString::unacquired(&pEntries[ n ].m_type.typeRef->pTypeName) + "\"!" );
204  SAL_WARN( "cppuhelper", msg );
205  throw RuntimeException( msg );
206  }
207  }
208  return nullptr;
209 }
210 
211 // ImplHelper
212 
214  Type const & rType, class_data * cd, void * that )
215 {
216  checkInterface( rType );
217  typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();
218 
219  void * p;
220  // shortcut for XInterface
221  if (isXInterface( pTDR->pTypeName ))
222  {
223  // take first one
224  p = makeInterface( cd->m_typeEntries[ 0 ].m_offset, that );
225  }
226  else
227  {
228  p = queryDeepNoXInterface( pTDR, cd, that );
229  if (! p)
230  {
231  return Any();
232  }
233  }
234  return Any( &p, pTDR );
235 }
236 
238  Type const & rType, class_data * cd, void * that )
239 {
240  checkInterface( rType );
241  typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();
242 
243  void * p = queryDeepNoXInterface( pTDR, cd, that );
244  if (p)
245  {
246  return Any( &p, pTDR );
247  }
248  return Any();
249 }
250 
251 css::uno::Sequence<sal_Int8> ImplHelper_getImplementationId(
252  SAL_UNUSED_PARAMETER class_data *)
253 {
254  return css::uno::Sequence<sal_Int8>();
255 }
256 
257 Sequence< Type > SAL_CALL ImplHelper_getTypes(
258  class_data * cd )
259 {
260  Sequence< Type > types( cd->m_nTypes );
261  Type * pTypes = types.getArray();
262  fillTypes( pTypes, cd );
263  return types;
264 }
265 
266 Sequence< Type > SAL_CALL ImplInhHelper_getTypes(
267  class_data * cd, Sequence< Type > const & rAddTypes )
268 {
269  sal_Int32 nImplTypes = cd->m_nTypes;
270  sal_Int32 nAddTypes = rAddTypes.getLength();
271  Sequence< Type > types( nImplTypes + nAddTypes );
272  Type * pTypes = types.getArray();
273  fillTypes( pTypes, cd );
274  // append base types
275  Type const * pAddTypes = rAddTypes.getConstArray();
276  while (nAddTypes--)
277  {
278  pTypes[ nImplTypes + nAddTypes ] = pAddTypes[ nAddTypes ];
279  }
280  return types;
281 }
282 
283 // WeakImplHelper
284 
286  Type const & rType, class_data * cd, void * that, OWeakObject * pBase )
287 {
288  checkInterface( rType );
289  typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();
290 
291  // shortcut XInterface to OWeakObject
292  if (! isXInterface( pTDR->pTypeName ))
293  {
294  void * p = queryDeepNoXInterface( pTDR, cd, that );
295  if (p)
296  {
297  return Any( &p, pTDR );
298  }
299  }
300  return pBase->OWeakObject::queryInterface( rType );
301 }
302 
303 Sequence< Type > SAL_CALL WeakImplHelper_getTypes(
304  class_data * cd )
305 {
306  sal_Int32 nTypes = cd->m_nTypes;
307  Sequence< Type > types( nTypes +1 );
308  Type * pTypes = types.getArray();
309  fillTypes( pTypes, cd );
310  pTypes[ nTypes ] = cppu::UnoType<XWeak>::get();
311  return types;
312 }
313 
314 // WeakAggImplHelper
315 
317  Type const & rType, class_data * cd, void * that, OWeakAggObject * pBase )
318 {
319  checkInterface( rType );
320  typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();
321 
322  // shortcut XInterface to OWeakAggObject
323  if (! isXInterface( pTDR->pTypeName ))
324  {
325  void * p = queryDeepNoXInterface( pTDR, cd, that );
326  if (p)
327  {
328  return Any( &p, pTDR );
329  }
330  }
331  return pBase->OWeakAggObject::queryAggregation( rType );
332 }
333 
334 Sequence< Type > SAL_CALL WeakAggImplHelper_getTypes(
335  class_data * cd )
336 {
337  sal_Int32 nTypes = cd->m_nTypes;
338  Sequence< Type > types( nTypes +2 );
339  Type * pTypes = types.getArray();
340  fillTypes( pTypes, cd );
341  pTypes[ nTypes++ ] = cppu::UnoType<XWeak>::get();
342  pTypes[ nTypes ] = cppu::UnoType<XAggregation>::get();
343  return types;
344 }
345 
346 // WeakComponentImplHelper
347 
349  Type const & rType, class_data * cd, void * that, WeakComponentImplHelperBase * pBase )
350 {
351  checkInterface( rType );
352  typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();
353 
354  // shortcut XInterface to WeakComponentImplHelperBase
355  if (! isXInterface( pTDR->pTypeName ))
356  {
357  void * p = queryDeepNoXInterface( pTDR, cd, that );
358  if (p)
359  {
360  return Any( &p, pTDR );
361  }
362  }
363  return pBase->WeakComponentImplHelperBase::queryInterface( rType );
364 }
365 
366 Sequence< Type > SAL_CALL WeakComponentImplHelper_getTypes(
367  class_data * cd )
368 {
369  sal_Int32 nTypes = cd->m_nTypes;
370  Sequence< Type > types( nTypes +2 );
371  Type * pTypes = types.getArray();
372  fillTypes( pTypes, cd );
373  pTypes[ nTypes++ ] = cppu::UnoType<XWeak>::get();
374  pTypes[ nTypes ] = cppu::UnoType<lang::XComponent>::get();
375  return types;
376 }
377 
378 // WeakAggComponentImplHelper
379 
381  Type const & rType, class_data * cd, void * that, WeakAggComponentImplHelperBase * pBase )
382 {
383  checkInterface( rType );
384  typelib_TypeDescriptionReference * pTDR = rType.getTypeLibType();
385 
386  // shortcut XInterface to WeakAggComponentImplHelperBase
387  if (! isXInterface( pTDR->pTypeName ))
388  {
389  void * p = queryDeepNoXInterface( pTDR, cd, that );
390  if (p)
391  {
392  return Any( &p, pTDR );
393  }
394  }
395  return pBase->WeakAggComponentImplHelperBase::queryAggregation( rType );
396 }
397 
398 Sequence< Type > SAL_CALL WeakAggComponentImplHelper_getTypes(
399  class_data * cd )
400 {
401  sal_Int32 nTypes = cd->m_nTypes;
402  Sequence< Type > types( nTypes +3 );
403  Type * pTypes = types.getArray();
404  fillTypes( pTypes, cd );
405  pTypes[ nTypes++ ] = cppu::UnoType<XWeak>::get();
406  pTypes[ nTypes++ ] = cppu::UnoType<XAggregation>::get();
407  pTypes[ nTypes ] = cppu::UnoType<lang::XComponent>::get();
408  return types;
409 }
410 
411 }
412 
413 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Type
static void * makeInterface(sal_IntPtr nOffset, void *that)
Definition: implbase_ex.cxx:64
static bool isXInterface(rtl_uString *pStr)
Definition: implbase_ex.cxx:59
Any SAL_CALL WeakComponentImplHelper_query(Type const &rType, class_data *cd, void *that, WeakComponentImplHelperBase *pBase)
Sequence< Type > SAL_CALL ImplInhHelper_getTypes(class_data *cd, Sequence< Type > const &rAddTypes)
Any SAL_CALL WeakAggComponentImplHelper_queryAgg(Type const &rType, class_data *cd, void *that, WeakAggComponentImplHelperBase *pBase)
sal_Int64 n
static bool td_equals(typelib_TypeDescriptionReference const *pTDR1, typelib_TypeDescriptionReference const *pTDR2)
Definition: implbase_ex.cxx:69
Any SAL_CALL WeakImplHelper_query(Type const &rType, class_data *cd, void *that, OWeakObject *pBase)
static void * queryDeepNoXInterface(typelib_TypeDescriptionReference const *pDemandedTDR, class_data *cd, void *that)
Sequence< Type > SAL_CALL WeakImplHelper_getTypes(class_data *cd)
Sequence< Type > SAL_CALL WeakAggComponentImplHelper_getTypes(class_data *cd)
Base class to implement a UNO object supporting weak references, i.e.
Definition: weakagg.hxx:41
Any SAL_CALL ImplHelper_queryNoXInterface(Type const &rType, class_data *cd, void *that)
int i
css::uno::Sequence< sal_Int8 > ImplHelper_getImplementationId(SAL_UNUSED_PARAMETER class_data *)
void const * base
Sequence< Type > SAL_CALL WeakComponentImplHelper_getTypes(class_data *cd)
Sequence< Type > SAL_CALL WeakAggImplHelper_getTypes(class_data *cd)
css::uno::Type const & get()
static void checkInterface(Type const &rType)
Definition: implbase_ex.cxx:49
struct _typelib_TypeDescription typelib_TypeDescription
Base class to implement a UNO object supporting weak references, i.e.
Definition: weak.hxx:43
static void fillTypes(Type *types, class_data *cd)
static type_entry * getTypeEntries(class_data *cd)
Definition: implbase_ex.cxx:77
void * p
Any SAL_CALL WeakAggImplHelper_queryAgg(Type const &rType, class_data *cd, void *that, OWeakAggObject *pBase)
Any SAL_CALL ImplHelper_query(Type const &rType, class_data *cd, void *that)
#define SAL_WARN(area, stream)
Sequence< Type > SAL_CALL ImplHelper_getTypes(class_data *cd)
::osl::Mutex & getImplHelperInitMutex()
Shared mutex for implementation helper initialization.
Definition: implbase_ex.cxx:43
const Type m_type