1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 #ifndef _CPPUHELPER_WEAKREF_HXX_ 24 #define _CPPUHELPER_WEAKREF_HXX_ 25 26 #include <com/sun/star/uno/XInterface.hpp> 27 28 #include "cppuhelper/cppuhelperdllapi.h" 29 30 31 namespace com 32 { 33 namespace sun 34 { 35 namespace star 36 { 37 namespace uno 38 { 39 40 /** @internal */ 41 class OWeakRefListener; 42 43 /** The WeakReferenceHelper holds a weak reference to an object. This object must implement 44 the ::com::sun::star::uno::XWeak interface. The implementation is thread safe. 45 */ 46 class CPPUHELPER_DLLPUBLIC WeakReferenceHelper 47 { 48 public: 49 /** Default ctor. Creates an empty weak reference. 50 */ 51 inline WeakReferenceHelper() SAL_THROW( () ) 52 : m_pImpl( 0 ) 53 {} 54 55 /** Copy ctor. Initialize this reference with the same interface as in rWeakRef. 56 57 @param rWeakRef another weak ref 58 */ 59 WeakReferenceHelper( const WeakReferenceHelper & rWeakRef ) SAL_THROW( () ); 60 /** Initialize this reference with the hard interface reference xInt. If the implementation 61 behind xInt does not support XWeak or XInt is null then this reference will be null. 62 63 @param xInt another hard interface reference 64 */ 65 WeakReferenceHelper( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & xInt ) 66 SAL_THROW( () ); 67 /** Releases this reference. 68 */ 69 ~WeakReferenceHelper() SAL_THROW( () ); 70 71 /** Releases this reference and takes over rWeakRef. 72 73 @param rWeakRef another weak ref 74 */ 75 WeakReferenceHelper & SAL_CALL operator = ( const WeakReferenceHelper & rWeakRef ) SAL_THROW( () ); 76 77 /** Releases this reference and takes over hard reference xInt. 78 If the implementation behind xInt does not support XWeak 79 or XInt is null, then this reference is null. 80 81 @param xInt another hard reference 82 */ 83 WeakReferenceHelper & SAL_CALL operator = ( 84 const ::com::sun::star::uno::Reference< 85 ::com::sun::star::uno::XInterface > & xInt ) SAL_THROW( () ); 86 87 /** Returns true if both weak refs reference to the same object. 88 89 @param rObj another weak ref 90 @return true, if both weak refs reference to the same object. 91 */ operator ==(const WeakReferenceHelper & rObj) const92 inline sal_Bool SAL_CALL operator == ( const WeakReferenceHelper & rObj ) const SAL_THROW( () ) 93 { return (get() == rObj.get()); } 94 95 /** Gets a hard reference to the object. 96 97 @return hard reference or null, if the weakly referenced interface has gone 98 */ 99 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL get() const SAL_THROW( () ); 100 /** Gets a hard reference to the object. 101 102 @return hard reference or null, if the weakly referenced interface has gone 103 */ operator Reference<XInterface>() const104 inline SAL_CALL operator Reference< XInterface > () const SAL_THROW( () ) 105 { return get(); } 106 107 /** Releases this reference. 108 109 @since UDK 3.2.12 110 */ 111 void SAL_CALL clear() SAL_THROW( () ); 112 113 protected: 114 /** @internal */ 115 OWeakRefListener * m_pImpl; 116 }; 117 118 /** The WeakReference<> holds a weak reference to an object. This object must implement 119 the ::com::sun::star::uno::XWeak interface. The implementation is thread safe. 120 121 @tplparam interface_type type of interface 122 */ 123 template< class interface_type > 124 class WeakReference : public WeakReferenceHelper 125 { 126 public: 127 /** Default ctor. Creates an empty weak reference. 128 */ 129 inline WeakReference() SAL_THROW( () ) 130 : WeakReferenceHelper() 131 {} 132 133 /** Copy ctor. Initialize this reference with a hard reference. 134 135 @param rRef another hard ref 136 */ 137 inline WeakReference( const Reference< interface_type > & rRef ) SAL_THROW( () ) 138 : WeakReferenceHelper( rRef ) 139 {} 140 141 /** Releases this reference and takes over hard reference xInt. 142 If the implementation behind xInt does not support XWeak 143 or XInt is null, then this reference is null. 144 145 @param xInt another hard reference 146 147 @since UDK 3.2.12 148 */ operator =(const::com::sun::star::uno::Reference<interface_type> & xInt)149 WeakReference & SAL_CALL operator = ( 150 const ::com::sun::star::uno::Reference< interface_type > & xInt ) 151 SAL_THROW( () ) 152 { WeakReferenceHelper::operator=(xInt); return *this; } 153 154 /** Gets a hard reference to the object. 155 156 @return hard reference or null, if the weakly referenced interface has gone 157 */ operator Reference<interface_type>() const158 inline SAL_CALL operator Reference< interface_type > () const SAL_THROW( () ) 159 { return Reference< interface_type >::query( get() ); } 160 }; 161 162 } 163 } 164 } 165 } 166 167 #endif 168