LibreOffice
LibreOffice 4.2 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 <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) );
76  inline static XInterface * SAL_CALL iquery_throw( XInterface * pInterface, const Type & rType )
77  SAL_THROW( (RuntimeException) );
78 
79 public:
84  inline XInterface * SAL_CALL get() const SAL_THROW(())
85  { return _pInterface; }
86 
91  inline sal_Bool SAL_CALL is() const SAL_THROW(())
92  { return (0 != _pInterface); }
93 
100  inline sal_Bool SAL_CALL operator == ( XInterface * pInterface ) const SAL_THROW(());
107  inline sal_Bool SAL_CALL operator != ( XInterface * pInterface ) const SAL_THROW(());
108 
115  inline sal_Bool SAL_CALL operator == ( const BaseReference & rRef ) const SAL_THROW(());
122  inline sal_Bool SAL_CALL operator != ( const BaseReference & rRef ) const SAL_THROW(());
123 
129  inline sal_Bool SAL_CALL operator < ( const BaseReference & rRef ) const SAL_THROW(());
130 };
131 
135 {
139 };
144 {
148 };
155 {
157 };
158 
160 namespace detail {
161 
162 // A mechanism to enable up-casts, used by the Reference conversion constructor,
163 // but at the same time disable up-casts to XInterface, so that the conversion
164 // operator for that special case is used in an expression like
165 // Reference< XInterface >(x); heavily borrowed from boost::is_base_and_derived
166 // (which manages to avoid compilation problems with ambiguous bases and cites
167 // comp.lang.c++.moderated mail <http://groups.google.com/groups?
168 // selm=df893da6.0301280859.522081f7%40posting.google.com> "SuperSubclass
169 // (is_base_and_derived) complete implementation!" by Rani Sharoni and cites
170 // Aleksey Gurtovoy for the workaround for MSVC), to avoid including Boost
171 // headers in URE headers (could ultimately be based on C++11 std::is_base_of):
172 
173 template< typename T1, typename T2 > struct UpCast {
174 private:
175  template< bool, typename U1, typename > struct C
176  { typedef U1 t; };
177 
178  template< typename U1, typename U2 > struct C< false, U1, U2 >
179  { typedef U2 t; };
180 
181  struct S { char c[2]; };
182 
183 #if defined _MSC_VER
184  static char f(T2 *, long);
185  static S f(T1 * const &, int);
186 #else
187  template< typename U > static char f(T2 *, U);
188  static S f(T1 *, int);
189 #endif
190 
191  struct H {
192  H(); // avoid C2514 "class has no constructors" from MSVC 2008
193 #if defined _MSC_VER
194  operator T1 * const & () const;
195 #else
196  operator T1 * () const;
197 #endif
198  operator T2 * ();
199  };
200 
201 public:
202  typedef typename C< sizeof (f(H(), 0)) == 1, void *, void >::t t;
203 };
204 
205 template< typename T2 > struct UpCast< XInterface, T2 > {};
206 
207 }
209 
214 template< class interface_type >
215 class Reference : public BaseReference
216 {
222  inline static XInterface * SAL_CALL iquery( XInterface * pInterface )
223  SAL_THROW( (RuntimeException) );
230  inline static XInterface * SAL_CALL iquery_throw( XInterface * pInterface )
231  SAL_THROW( (RuntimeException) );
237  inline static interface_type * SAL_CALL iset_throw( interface_type * pInterface )
238  SAL_THROW( (RuntimeException) );
239 
251  static inline interface_type * castFromXInterface(XInterface * p) {
252  return static_cast< interface_type * >(static_cast< void * >(p));
253  }
254 
266  static inline XInterface * castToXInterface(interface_type * p) {
267  return static_cast< XInterface * >(static_cast< void * >(p));
268  }
269 
270 public:
272  // these are here to force memory de/allocation to sal lib.
273  inline static void * SAL_CALL operator new ( ::size_t nSize ) SAL_THROW(())
274  { return ::rtl_allocateMemory( nSize ); }
275  inline static void SAL_CALL operator delete ( void * pMem ) SAL_THROW(())
276  { ::rtl_freeMemory( pMem ); }
277  inline static void * SAL_CALL operator new ( ::size_t, void * pMem ) SAL_THROW(())
278  { return pMem; }
279  inline static void SAL_CALL operator delete ( void *, void * ) SAL_THROW(())
280  {}
282 
285  inline ~Reference() SAL_THROW(());
286 
289  inline Reference() SAL_THROW(());
290 
295  inline Reference( const Reference< interface_type > & rRef ) SAL_THROW(());
296 
305  template< class derived_type >
306  inline Reference(
307  const Reference< derived_type > & rRef,
308  typename detail::UpCast< interface_type, derived_type >::t = 0 )
309  SAL_THROW(());
310 
315  inline Reference( interface_type * pInterface ) SAL_THROW(());
316 
322  inline Reference( interface_type * pInterface, __sal_NoAcquire dummy) SAL_THROW(());
330  inline Reference( interface_type * pInterface, UnoReference_NoAcquire dummy ) SAL_THROW(());
331 
337  inline Reference( const BaseReference & rRef, UnoReference_Query dummy ) SAL_THROW( (RuntimeException) );
343  inline Reference( XInterface * pInterface, UnoReference_Query dummy) SAL_THROW( (RuntimeException) );
349  inline Reference( const Any & rAny, UnoReference_Query dummy) SAL_THROW( (RuntimeException) );
357  inline Reference( const BaseReference & rRef, UnoReference_QueryThrow dummy ) SAL_THROW( (RuntimeException) );
365  inline Reference( XInterface * pInterface, UnoReference_QueryThrow dummy ) SAL_THROW( (RuntimeException) );
373  inline Reference( const Any & rAny, UnoReference_QueryThrow dummy ) SAL_THROW( (RuntimeException) );
382  inline Reference( const Reference< interface_type > & rRef, UnoReference_SetThrow dummy ) SAL_THROW( (RuntimeException) );
391  inline Reference( interface_type * pInterface, UnoReference_SetThrow dummy ) SAL_THROW( (RuntimeException) );
392 
397  inline SAL_CALL operator const Reference< XInterface > & () const SAL_THROW(())
398  { return * reinterpret_cast< const Reference< XInterface > * >( this ); }
399 
404  inline interface_type * SAL_CALL operator -> () const SAL_THROW(())
405  { return castFromXInterface(_pInterface); }
406 
411  inline interface_type * SAL_CALL get() const SAL_THROW(())
412  { return castFromXInterface(_pInterface); }
413 
416  inline void SAL_CALL clear() SAL_THROW(());
417 
423  inline sal_Bool SAL_CALL set( const Reference< interface_type > & rRef ) SAL_THROW(());
429  inline sal_Bool SAL_CALL set( interface_type * pInterface ) SAL_THROW(());
430 
437  inline sal_Bool SAL_CALL set( interface_type * pInterface, __sal_NoAcquire dummy) SAL_THROW(());
446  inline sal_Bool SAL_CALL set( interface_type * pInterface, UnoReference_NoAcquire dummy) SAL_THROW(());
447 
455  inline sal_Bool SAL_CALL set( XInterface * pInterface, UnoReference_Query dummy ) SAL_THROW( (RuntimeException) );
463  inline sal_Bool SAL_CALL set( const BaseReference & rRef, UnoReference_Query dummy) SAL_THROW( (RuntimeException) );
464 
476  inline bool set( Any const & rAny, UnoReference_Query dummy );
477 
486  inline void SAL_CALL set( XInterface * pInterface, UnoReference_QueryThrow dummy ) SAL_THROW( (RuntimeException) );
495  inline void SAL_CALL set( const BaseReference & rRef, UnoReference_QueryThrow dummy ) SAL_THROW( (RuntimeException) );
496 
506  inline void set( Any const & rAny, UnoReference_QueryThrow dummy);
516  inline void SAL_CALL set( interface_type * pInterface, UnoReference_SetThrow dummy) SAL_THROW( (RuntimeException) );
526  inline void SAL_CALL set( const Reference< interface_type > & rRef, UnoReference_SetThrow dummy) SAL_THROW( (RuntimeException) );
527 
528 
535  inline Reference< interface_type > & SAL_CALL operator = ( interface_type * pInterface ) SAL_THROW(());
542  inline Reference< interface_type > & SAL_CALL operator = ( const Reference< interface_type > & rRef ) SAL_THROW(());
543 
549  inline static Reference< interface_type > SAL_CALL query( const BaseReference & rRef ) SAL_THROW( (RuntimeException) );
555  inline static Reference< interface_type > SAL_CALL query( XInterface * pInterface ) SAL_THROW( (RuntimeException) );
556 };
557 
559 
561 template <typename T>
562 inline T * get_pointer( Reference<T> const& r )
563 {
564  return r.get();
565 }
567 
568 }
569 }
570 }
571 }
572 
573 #endif
574 
575 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference()
Default Constructor: Sets null reference.
Definition: Reference.hxx:110
C++ class representing an IDL meta type.
Definition: Type.h:55
SAL_DLLPUBLIC void * rtl_allocateMemory(sal_Size Bytes) SAL_THROW_EXTERN_C()
Allocate memory.
static Reference< interface_type > query(const BaseReference &rRef)
Queries given interface reference for type interface_type.
Definition: Reference.hxx:343
sal_Bool operator!=(XInterface *pInterface) const
Unequality operator: compares two interfaces Checks if both references are null or refer to the same ...
Definition: Reference.hxx:398
This base class serves as a base class for all template reference classes and has been introduced due...
Definition: Reference.h:54
UnoReference_Query
Enum defining UNO_QUERY for implicit interface query.
Definition: Reference.h:134
sal_Bool operator==(XInterface *pInterface) const
Equality operator: compares two interfaces Checks if both references are null or refer to the same ob...
Definition: Reference.hxx:361
~Reference()
Destructor: Releases interface if set.
Definition: Reference.hxx:103
sal_Bool set(const Reference< interface_type > &rRef)
Sets the given interface.
Definition: Reference.hxx:250
sal_Bool operator<(const BaseReference &rRef) const
Needed by some STL containers.
Definition: Reference.hxx:379
Definition: Reference.h:156
sal_Bool is() const
Checks if reference is null.
Definition: Reference.h:91
Template reference class for interface type derived from BaseReference.
Definition: unotype.hxx:32
UnoReference_NoAcquire
Enum defining UNO_REF_NO_ACQUIRE for setting reference without acquiring a given interface.
Definition: Reference.h:43
This enum value can be used for implicit interface query.
Definition: Reference.h:147
C++ class representing an IDL any.
Definition: Any.h:46
This enum value can be used for implicit interface query.
Definition: Reference.h:138
UnoReference_QueryThrow
Enum defining UNO_QUERY_THROW for implicit interface query.
Definition: Reference.h:143
void clear()
Clears reference, i.e.
Definition: Reference.hxx:207
unsigned char sal_Bool
Definition: types.h:46
__sal_NoAcquire
Definition: types.h:372
static XInterface * iquery(XInterface *pInterface, const Type &rType)
Queries given interface for type rType.
Definition: Reference.hxx:37
UnoReference_SetThrow
Enum defining UNO_SET_THROW for throwing if attempts are made to assign a null interface.
Definition: Reference.h:154
static XInterface * iquery_throw(XInterface *pInterface, const Type &rType)
Queries given interface for type rType.
Definition: Reference.hxx:67
XInterface * _pInterface
the interface pointer
Definition: Reference.h:59
This enum value can be used for creating a reference granting a given interface, i.e.
Definition: Reference.h:48
SAL_DLLPUBLIC void rtl_freeMemory(void *Ptr) SAL_THROW_EXTERN_C()
Free memory.
interface_type * operator->() const
Dereference operator: Used to call interface methods.
Definition: Reference.h:404
#define SAL_THROW(exc)
Definition of function throw clause macros.
Definition: types.h:358