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 24 #ifndef _TOOLS_WEAKBASE_HXX_ 25 #define _TOOLS_WEAKBASE_HXX_ 26 27 #include <tools/weakbase.h> 28 29 /// see weakbase.h for documentation 30 31 namespace tools 32 { 33 34 template< class reference_type > WeakReference()35inline WeakReference< reference_type >::WeakReference() 36 { 37 mpWeakConnection = new WeakConnection<reference_type>( 0 ); 38 mpWeakConnection->acquire(); 39 } 40 41 template< class reference_type > WeakReference(reference_type * pReference)42inline WeakReference< reference_type >::WeakReference( reference_type* pReference ) 43 { 44 if( pReference ) 45 mpWeakConnection = pReference->getWeakConnection(); 46 else 47 mpWeakConnection = new WeakConnection<reference_type>( 0 ); 48 49 mpWeakConnection->acquire(); 50 } 51 52 template< class reference_type > WeakReference(const WeakReference<reference_type> & rWeakRef)53inline WeakReference< reference_type >::WeakReference( const WeakReference< reference_type >& rWeakRef ) 54 { 55 mpWeakConnection = rWeakRef.mpWeakConnection; 56 mpWeakConnection->acquire(); 57 } 58 59 template< class reference_type > ~WeakReference()60inline WeakReference< reference_type >::~WeakReference() 61 { 62 mpWeakConnection->release(); 63 } 64 65 template< class reference_type > is() const66inline bool WeakReference< reference_type >::is() const 67 { 68 return mpWeakConnection->mpReference != 0; 69 } 70 71 template< class reference_type > get() const72inline reference_type * WeakReference< reference_type >::get() const 73 { 74 return mpWeakConnection->mpReference; 75 } 76 77 template< class reference_type > reset(reference_type * pReference)78inline void WeakReference< reference_type >::reset( reference_type* pReference ) 79 { 80 mpWeakConnection->release(); 81 82 if( pReference ) 83 mpWeakConnection = pReference->getWeakConnection(); 84 else 85 mpWeakConnection = new WeakConnection<reference_type>( 0 ); 86 87 mpWeakConnection->acquire(); 88 } 89 90 template< class reference_type > operator ->() const91inline reference_type * WeakReference< reference_type >::operator->() const 92 { 93 OSL_PRECOND(mpWeakConnection, "tools::WeakReference::operator->() : null body"); 94 return mpWeakConnection->mpReference; 95 } 96 97 template< class reference_type > operator ==(const reference_type * pReferenceObject) const98inline sal_Bool WeakReference< reference_type >::operator==(const reference_type * pReferenceObject) const 99 { 100 return mpWeakConnection->mpReference == pReferenceObject; 101 } 102 103 template< class reference_type > operator ==(const WeakReference<reference_type> & handle) const104inline sal_Bool WeakReference< reference_type >::operator==(const WeakReference<reference_type> & handle) const 105 { 106 return mpWeakConnection == handle.mpWeakConnection; 107 } 108 109 template< class reference_type > operator !=(const WeakReference<reference_type> & handle) const110inline sal_Bool WeakReference< reference_type >::operator!=(const WeakReference<reference_type> & handle) const 111 { 112 return mpWeakConnection != handle.mpWeakConnection; 113 } 114 115 template< class reference_type > operator <(const WeakReference<reference_type> & handle) const116inline sal_Bool WeakReference< reference_type >::operator<(const WeakReference<reference_type> & handle) const 117 { 118 return mpWeakConnection->mpReference < handle.mpWeakConnection->mpReference; 119 } 120 121 template< class reference_type > operator >(const WeakReference<reference_type> & handle) const122inline sal_Bool WeakReference< reference_type >::operator>(const WeakReference<reference_type> & handle) const 123 { 124 return mpWeakConnection->mpReference > handle.mpWeakConnection->mpReference; 125 } 126 127 template< class reference_type > operator =(const WeakReference<reference_type> & rReference)128inline WeakReference<reference_type>& WeakReference<reference_type>::operator= ( 129 const WeakReference<reference_type>& rReference) 130 { 131 if (&rReference != this) 132 { 133 mpWeakConnection->release(); 134 135 mpWeakConnection = rReference.mpWeakConnection; 136 mpWeakConnection->acquire(); 137 } 138 return *this; 139 } 140 141 template< class reference_type > WeakBase()142inline WeakBase< reference_type >::WeakBase() 143 { 144 mpWeakConnection = 0; 145 } 146 147 template< class reference_type > ~WeakBase()148inline WeakBase< reference_type >::~WeakBase() 149 { 150 if( mpWeakConnection ) 151 { 152 mpWeakConnection->mpReference = 0; 153 mpWeakConnection->release(); 154 mpWeakConnection = 0; 155 } 156 } 157 158 template< class reference_type > clearWeak()159inline void WeakBase< reference_type >::clearWeak() 160 { 161 if( mpWeakConnection ) 162 mpWeakConnection->mpReference = 0; 163 } 164 165 template< class reference_type > getWeakConnection()166inline WeakConnection< reference_type >* WeakBase< reference_type >::getWeakConnection() 167 { 168 if( !mpWeakConnection ) 169 { 170 mpWeakConnection = new WeakConnection< reference_type >( static_cast< reference_type* >( this ) ); 171 mpWeakConnection->acquire(); 172 } 173 return mpWeakConnection; 174 } 175 176 } 177 178 #endif // _TOOLS_WEAKBASE_HXX_ 179 180