LibreOffice
LibreOffice 5.0 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 INCLUDED_COM_SUN_STAR_UNO_REFERENCE_H
20 #define INCLUDED_COM_SUN_STAR_UNO_REFERENCE_H
21 
22 #include <sal/config.h>
23 
24 #include <cassert>
25 
26 #include <rtl/alloc.h>
27 
28 namespace com
29 {
30 namespace sun
31 {
32 namespace star
33 {
34 namespace uno
35 {
36 
37 class RuntimeException;
38 class XInterface;
39 class Type;
40 class Any;
41 
47 {
52 };
53 
58 {
59 protected:
62  XInterface * _pInterface;
63 
70  inline static XInterface * SAL_CALL iquery( XInterface * pInterface, const Type & rType );
78  inline static XInterface * SAL_CALL iquery_throw( XInterface * pInterface, const Type & rType );
79 
80 public:
85  inline XInterface * SAL_CALL get() const
86  { return _pInterface; }
87 
92  inline bool SAL_CALL is() const
93  { return (0 != _pInterface); }
94 
101  inline bool SAL_CALL operator == ( XInterface * pInterface ) const;
108  inline bool SAL_CALL operator != ( XInterface * pInterface ) const;
109 
116  inline bool SAL_CALL operator == ( const BaseReference & rRef ) const;
123  inline bool SAL_CALL operator != ( const BaseReference & rRef ) const;
124 
130  inline bool SAL_CALL operator < ( const BaseReference & rRef ) const;
131 };
132 
136 {
140 };
145 {
149 };
156 {
158 };
159 
161 namespace detail {
162 
163 // A mechanism to enable up-casts, used by the Reference conversion constructor,
164 // but at the same time disable up-casts to XInterface, so that the conversion
165 // operator for that special case is used in an expression like
166 // Reference< XInterface >(x); heavily borrowed from boost::is_base_and_derived
167 // (which manages to avoid compilation problems with ambiguous bases and cites
168 // comp.lang.c++.moderated mail <http://groups.google.com/groups?
169 // selm=df893da6.0301280859.522081f7%40posting.google.com> "SuperSubclass
170 // (is_base_and_derived) complete implementation!" by Rani Sharoni and cites
171 // Aleksey Gurtovoy for the workaround for MSVC), to avoid including Boost
172 // headers in URE headers (could ultimately be based on C++11 std::is_base_of):
173 
174 template< typename T1, typename T2 > struct UpCast {
175 private:
176  template< bool, typename U1, typename > struct C
177  { typedef U1 t; };
178 
179  template< typename U1, typename U2 > struct C< false, U1, U2 >
180  { typedef U2 t; };
181 
182  struct S { char c[2]; };
183 
184 #if defined _MSC_VER
185  static char f(T2 *, long);
186  static S f(T1 * const &, int);
187 #else
188  template< typename U > static char f(T2 *, U);
189  static S f(T1 *, int);
190 #endif
191 
192  struct H {
193  H(); // avoid C2514 "class has no constructors" from MSVC 2008
194 #if defined _MSC_VER
195  operator T1 * const & () const;
196 #else
197  operator T1 * () const;
198 #endif
199  operator T2 * ();
200  };
201 
202 public:
203  typedef typename C< sizeof (f(H(), 0)) == 1, void *, void >::t t;
204 };
205 
206 template< typename T2 > struct UpCast< XInterface, T2 > {};
207 
208 }
210 
215 template< class interface_type >
216 class SAL_DLLPUBLIC_RTTI Reference : public BaseReference
217 {
223  inline static XInterface * SAL_CALL iquery( XInterface * pInterface );
230  inline static XInterface * SAL_CALL iquery_throw( XInterface * pInterface );
236  inline static interface_type * SAL_CALL iset_throw( interface_type * pInterface );
237 
249  static inline interface_type * castFromXInterface(XInterface * p) {
250  return static_cast< interface_type * >(static_cast< void * >(p));
251  }
252 
264  static inline XInterface * castToXInterface(interface_type * p) {
265  return static_cast< XInterface * >(static_cast< void * >(p));
266  }
267 
268 public:
270  // these are here to force memory de/allocation to sal lib.
271  inline static void * SAL_CALL operator new ( ::size_t nSize )
272  { return ::rtl_allocateMemory( nSize ); }
273  inline static void SAL_CALL operator delete ( void * pMem )
274  { ::rtl_freeMemory( pMem ); }
275  inline static void * SAL_CALL operator new ( ::size_t, void * pMem )
276  { return pMem; }
277  inline static void SAL_CALL operator delete ( void *, void * )
278  {}
280 
283  inline ~Reference();
284 
287  inline Reference();
288 
293  inline Reference( const Reference< interface_type > & rRef );
294 
303  template< class derived_type >
304  inline Reference(
305  const Reference< derived_type > & rRef,
306  typename detail::UpCast< interface_type, derived_type >::t = 0 );
307 
312  inline Reference( interface_type * pInterface );
313 
319  inline Reference( interface_type * pInterface, __sal_NoAcquire dummy);
327  inline Reference( interface_type * pInterface, UnoReference_NoAcquire dummy );
328 
334  inline Reference( const BaseReference & rRef, UnoReference_Query dummy );
340  inline Reference( XInterface * pInterface, UnoReference_Query dummy);
346  inline Reference( const Any & rAny, UnoReference_Query dummy);
354  inline Reference( const BaseReference & rRef, UnoReference_QueryThrow dummy );
362  inline Reference( XInterface * pInterface, UnoReference_QueryThrow dummy );
370  inline Reference( const Any & rAny, UnoReference_QueryThrow dummy );
379  inline Reference( const Reference< interface_type > & rRef, UnoReference_SetThrow dummy );
388  inline Reference( interface_type * pInterface, UnoReference_SetThrow dummy );
389 
394  inline SAL_CALL operator const Reference< XInterface > & () const
395  { return * reinterpret_cast< const Reference< XInterface > * >( this ); }
396 
401  inline interface_type * SAL_CALL operator -> () const {
402  assert(_pInterface != 0);
403  return castFromXInterface(_pInterface);
404  }
405 
410  inline interface_type * SAL_CALL get() const
411  { return castFromXInterface(_pInterface); }
412 
415  inline void SAL_CALL clear();
416 
422  inline bool SAL_CALL set( const Reference< interface_type > & rRef );
428  inline bool SAL_CALL set( interface_type * pInterface );
429 
436  inline bool SAL_CALL set( interface_type * pInterface, __sal_NoAcquire dummy);
445  inline bool SAL_CALL set( interface_type * pInterface, UnoReference_NoAcquire dummy);
446 
454  inline bool SAL_CALL set( XInterface * pInterface, UnoReference_Query dummy );
462  inline bool SAL_CALL set( const BaseReference & rRef, UnoReference_Query dummy);
463 
475  inline bool set( Any const & rAny, UnoReference_Query dummy );
476 
485  inline void SAL_CALL set( XInterface * pInterface, UnoReference_QueryThrow dummy );
494  inline void SAL_CALL set( const BaseReference & rRef, UnoReference_QueryThrow dummy );
495 
505  inline void set( Any const & rAny, UnoReference_QueryThrow dummy);
515  inline void SAL_CALL set( interface_type * pInterface, UnoReference_SetThrow dummy);
525  inline void SAL_CALL set( const Reference< interface_type > & rRef, UnoReference_SetThrow dummy);
526 
527 
534  inline Reference< interface_type > & SAL_CALL operator = ( interface_type * pInterface );
541  inline Reference< interface_type > & SAL_CALL operator = ( const Reference< interface_type > & rRef );
542 
548  inline static SAL_WARN_UNUSED_RESULT Reference< interface_type > SAL_CALL query( const BaseReference & rRef );
554  inline static SAL_WARN_UNUSED_RESULT Reference< interface_type > SAL_CALL query( XInterface * pInterface );
555 };
556 
558 
560 template <typename T>
561 inline T * get_pointer( Reference<T> const& r )
562 {
563  return r.get();
564 }
566 
567 }
568 }
569 }
570 }
571 
572 #endif
573 
574 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */