LibreOffice
LibreOffice 4.1 SDK C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Reference.h
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 #ifndef _COM_SUN_STAR_UNO_REFERENCE_H_
20 #define _COM_SUN_STAR_UNO_REFERENCE_H_
21 
22 #include <rtl/alloc.h>
23 
24 
25 namespace com
26 {
27 namespace sun
28 {
29 namespace star
30 {
31 namespace uno
32 {
33 
34 class RuntimeException;
35 class XInterface;
36 class Type;
37 class Any;
38 
44 {
49 };
50 
55 {
56 protected:
59  XInterface * _pInterface;
60 
67  inline static XInterface * SAL_CALL iquery( XInterface * pInterface, const Type & rType )
68  SAL_THROW( (RuntimeException) );
69 #ifndef EXCEPTIONS_OFF
70 
77  inline static XInterface * SAL_CALL iquery_throw( XInterface * pInterface, const Type & rType )
78  SAL_THROW( (RuntimeException) );
79 #endif
80 
81 public:
86  inline XInterface * SAL_CALL get() const SAL_THROW(())
87  { return _pInterface; }
88 
93  inline sal_Bool SAL_CALL is() const SAL_THROW(())
94  { return (0 != _pInterface); }
95 
102  inline sal_Bool SAL_CALL operator == ( XInterface * pInterface ) const SAL_THROW(());
109  inline sal_Bool SAL_CALL operator != ( XInterface * pInterface ) const SAL_THROW(());
110 
117  inline sal_Bool SAL_CALL operator == ( const BaseReference & rRef ) const SAL_THROW(());
124  inline sal_Bool SAL_CALL operator != ( const BaseReference & rRef ) const SAL_THROW(());
125 
131  inline sal_Bool SAL_CALL operator < ( const BaseReference & rRef ) const SAL_THROW(());
132 };
133 
137 {
141 };
142 #ifndef EXCEPTIONS_OFF
143 
147 {
151 };
158 {
160 };
161 #endif
162 
164 namespace detail {
165 
166 // A mechanism to enable up-casts, used by the Reference conversion constructor,
167 // but at the same time disable up-casts to XInterface, so that the conversion
168 // operator for that special case is used in an expression like
169 // Reference< XInterface >(x); heavily borrowed from boost::is_base_and_derived
170 // (which manages to avoid compilation problems with ambiguous bases and cites
171 // comp.lang.c++.moderated mail <http://groups.google.com/groups?
172 // selm=df893da6.0301280859.522081f7%40posting.google.com> "SuperSubclass
173 // (is_base_and_derived) complete implementation!" by Rani Sharoni and cites
174 // Aleksey Gurtovoy for the workaround for MSVC), to avoid including Boost
175 // headers in URE headers (could ultimately be based on C++11 std::is_base_of):
176 
177 template< typename T1, typename T2 > struct UpCast {
178 private:
179  template< bool, typename U1, typename > struct C
180  { typedef U1 t; };
181 
182  template< typename U1, typename U2 > struct C< false, U1, U2 >
183  { typedef U2 t; };
184 
185  struct S { char c[2]; };
186 
187 #if defined _MSC_VER
188  static char f(T2 *, long);
189  static S f(T1 * const &, int);
190 #else
191  template< typename U > static char f(T2 *, U);
192  static S f(T1 *, int);
193 #endif
194 
195  struct H {
196  H(); // avoid C2514 "class has no constructors" from MSVC 2008
197 #if defined _MSC_VER
198  operator T1 * const & () const;
199 #else
200  operator T1 * () const;
201 #endif
202  operator T2 * ();
203  };
204 
205 public:
206  typedef typename C< sizeof (f(H(), 0)) == 1, void *, void >::t t;
207 };
208 
209 template< typename T2 > struct UpCast< XInterface, T2 > {};
210 
211 }
213 
218 template< class interface_type >
219 class Reference : public BaseReference
220 {
226  inline static XInterface * SAL_CALL iquery( XInterface * pInterface )
227  SAL_THROW( (RuntimeException) );
228 #ifndef EXCEPTIONS_OFF
229 
235  inline static XInterface * SAL_CALL iquery_throw( XInterface * pInterface )
236  SAL_THROW( (RuntimeException) );
242  inline static interface_type * SAL_CALL iset_throw( interface_type * pInterface )
243  SAL_THROW( (RuntimeException) );
244 #endif
245 
257  static inline interface_type * castFromXInterface(XInterface * p) {
258  return static_cast< interface_type * >(static_cast< void * >(p));
259  }
260 
272  static inline XInterface * castToXInterface(interface_type * p) {
273  return static_cast< XInterface * >(static_cast< void * >(p));
274  }
275 
276 public:
278  // these are here to force memory de/allocation to sal lib.
279  inline static void * SAL_CALL operator new ( ::size_t nSize ) SAL_THROW(())
280  { return ::rtl_allocateMemory( nSize ); }
281  inline static void SAL_CALL operator delete ( void * pMem ) SAL_THROW(())
282  { ::rtl_freeMemory( pMem ); }
283  inline static void * SAL_CALL operator new ( ::size_t, void * pMem ) SAL_THROW(())
284  { return pMem; }
285  inline static void SAL_CALL operator delete ( void *, void * ) SAL_THROW(())
286  {}
288 
291  inline ~Reference() SAL_THROW(());
292 
295  inline Reference() SAL_THROW(());
296 
301  inline Reference( const Reference< interface_type > & rRef ) SAL_THROW(());
302 
311  template< class derived_type >
312  inline Reference(
313  const Reference< derived_type > & rRef,
314  typename detail::UpCast< interface_type, derived_type >::t = 0 )
315  SAL_THROW(());
316 
321  inline Reference( interface_type * pInterface ) SAL_THROW(());
322 
328  inline Reference( interface_type * pInterface, __sal_NoAcquire dummy) SAL_THROW(());
336  inline Reference( interface_type * pInterface, UnoReference_NoAcquire dummy ) SAL_THROW(());
337 
343  inline Reference( const BaseReference & rRef, UnoReference_Query dummy ) SAL_THROW( (RuntimeException) );
349  inline Reference( XInterface * pInterface, UnoReference_Query dummy) SAL_THROW( (RuntimeException) );
355  inline Reference( const Any & rAny, UnoReference_Query dummy) SAL_THROW( (RuntimeException) );
356 #ifndef EXCEPTIONS_OFF
357 
364  inline Reference( const BaseReference & rRef, UnoReference_QueryThrow dummy ) SAL_THROW( (RuntimeException) );
372  inline Reference( XInterface * pInterface, UnoReference_QueryThrow dummy ) SAL_THROW( (RuntimeException) );
380  inline Reference( const Any & rAny, UnoReference_QueryThrow dummy ) SAL_THROW( (RuntimeException) );
389  inline Reference( const Reference< interface_type > & rRef, UnoReference_SetThrow dummy ) SAL_THROW( (RuntimeException) );
398  inline Reference( interface_type * pInterface, UnoReference_SetThrow dummy ) SAL_THROW( (RuntimeException) );
399 #endif
400 
405  inline SAL_CALL operator const Reference< XInterface > & () const SAL_THROW(())
406  { return * reinterpret_cast< const Reference< XInterface > * >( this ); }
407 
412  inline interface_type * SAL_CALL operator -> () const SAL_THROW(())
413  { return castFromXInterface(_pInterface); }
414 
419  inline interface_type * SAL_CALL get() const SAL_THROW(())
420  { return castFromXInterface(_pInterface); }
421 
424  inline void SAL_CALL clear() SAL_THROW(());
425 
431  inline sal_Bool SAL_CALL set( const Reference< interface_type > & rRef ) SAL_THROW(());
437  inline sal_Bool SAL_CALL set( interface_type * pInterface ) SAL_THROW(());
438 
445  inline sal_Bool SAL_CALL set( interface_type * pInterface, __sal_NoAcquire dummy) SAL_THROW(());
454  inline sal_Bool SAL_CALL set( interface_type * pInterface, UnoReference_NoAcquire dummy) SAL_THROW(());
455 
463  inline sal_Bool SAL_CALL set( XInterface * pInterface, UnoReference_Query dummy ) SAL_THROW( (RuntimeException) );
471  inline sal_Bool SAL_CALL set( const BaseReference & rRef, UnoReference_Query dummy) SAL_THROW( (RuntimeException) );
472 
484  inline bool set( Any const & rAny, UnoReference_Query dummy );
485 
486 #ifndef EXCEPTIONS_OFF
487 
495  inline void SAL_CALL set( XInterface * pInterface, UnoReference_QueryThrow dummy ) SAL_THROW( (RuntimeException) );
504  inline void SAL_CALL set( const BaseReference & rRef, UnoReference_QueryThrow dummy ) SAL_THROW( (RuntimeException) );
505 
515  inline void set( Any const & rAny, UnoReference_QueryThrow dummy);
525  inline void SAL_CALL set( interface_type * pInterface, UnoReference_SetThrow dummy) SAL_THROW( (RuntimeException) );
535  inline void SAL_CALL set( const Reference< interface_type > & rRef, UnoReference_SetThrow dummy) SAL_THROW( (RuntimeException) );
536 
537 #endif
538 
545  inline Reference< interface_type > & SAL_CALL operator = ( interface_type * pInterface ) SAL_THROW(());
553 
559  inline static Reference< interface_type > SAL_CALL query( const BaseReference & rRef ) SAL_THROW( (RuntimeException) );
565  inline static Reference< interface_type > SAL_CALL query( XInterface * pInterface ) SAL_THROW( (RuntimeException) );
566 };
567 
569 
571 template <typename T>
572 inline T * get_pointer( Reference<T> const& r )
573 {
574  return r.get();
575 }
577 
578 }
579 }
580 }
581 }
582 
583 #endif
584 
585 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */