1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 #ifndef _INPROCSERV_SMARTPOINTER_HXX_ 29 #define _INPROCSERV_SMARTPOINTER_HXX_ 30 31 // #define OWNDEBUG 32 33 #ifdef OWNDEBUG 34 #define WRITEDEBUGINFOINTERN( x ) WriteDebugInfo( (DWORD)this, x, sizeof( x ) ) 35 #define WRITEDEBUGINFO( x ) WRITEDEBUGINFOINTERN( x ":" MY_STRING_LINE "\n" ) 36 #define TO_STRING( x ) #x 37 #define MACRO_VALUE_TO_STRING( x ) TO_STRING( x ) 38 #define MY_STRING_LINE MACRO_VALUE_TO_STRING( __LINE__ ) 39 #else 40 #define WRITEDEBUGINFO( x ) void() 41 #define MY_STRING_LINE 42 #endif 43 44 45 namespace inprocserv{ 46 47 void WriteDebugInfo( DWORD pThis, char* pString, DWORD nToWrite ); 48 49 template< class T > class ComSmart 50 { 51 T* m_pInterface; 52 53 void OwnRelease() 54 { 55 if ( m_pInterface ) 56 { 57 T* pInterface = m_pInterface; 58 m_pInterface = NULL; 59 pInterface->Release(); 60 } 61 } 62 63 public: 64 ComSmart() 65 : m_pInterface( NULL ) 66 {} 67 68 ComSmart( const ComSmart<T>& rObj ) 69 : m_pInterface( rObj.m_pInterface ) 70 { 71 if ( m_pInterface != NULL ) 72 m_pInterface->AddRef(); 73 } 74 75 ComSmart( T* pInterface ) 76 : m_pInterface( pInterface ) 77 { 78 if ( m_pInterface != NULL ) 79 m_pInterface->AddRef(); 80 } 81 82 ~ComSmart() 83 { 84 OwnRelease(); 85 } 86 87 ComSmart& operator=( const ComSmart<T>& rObj ) 88 { 89 OwnRelease(); 90 91 m_pInterface = rObj.m_pInterface; 92 93 if ( m_pInterface != NULL ) 94 m_pInterface->AddRef(); 95 96 return *this; 97 } 98 99 ComSmart<T>& operator=( T* pInterface ) 100 { 101 OwnRelease(); 102 103 m_pInterface = pInterface; 104 105 if ( m_pInterface != NULL ) 106 m_pInterface->AddRef(); 107 108 return *this; 109 } 110 111 operator T*() const 112 { 113 return m_pInterface; 114 } 115 116 T& operator*() const 117 { 118 return *m_pInterface; 119 } 120 121 T** operator&() 122 { 123 OwnRelease(); 124 125 m_pInterface = NULL; 126 127 return &m_pInterface; 128 } 129 130 T* operator->() const 131 { 132 return m_pInterface; 133 } 134 135 BOOL operator==( const ComSmart<T>& rObj ) const 136 { 137 return ( m_pInterface == rObj.m_pInterface ); 138 } 139 140 BOOL operator!=( const ComSmart<T>& rObj ) const 141 { 142 return ( m_pInterface != rObj.m_pInterface ); 143 } 144 145 BOOL operator==( const T* pInterface ) const 146 { 147 return ( m_pInterface == pInterface ); 148 } 149 150 BOOL operator!=( const T* pInterface ) const 151 { 152 return ( m_pInterface != pInterface ); 153 } 154 }; 155 156 class CSGuard 157 { 158 CRITICAL_SECTION* m_pCriticalSection; 159 160 public: 161 CSGuard( CRITICAL_SECTION* pCS ) 162 : m_pCriticalSection( pCS ) 163 { 164 if ( m_pCriticalSection ) 165 EnterCriticalSection( m_pCriticalSection ); 166 } 167 168 ~CSGuard() 169 { 170 if ( m_pCriticalSection ) 171 LeaveCriticalSection( m_pCriticalSection ); 172 } 173 }; 174 175 class ULONGGuard 176 { 177 ULONG* m_pValue; 178 179 public: 180 ULONGGuard( ULONG* pValue ) 181 : m_pValue( pValue ) 182 { 183 if ( m_pValue ) 184 (*m_pValue)++; 185 } 186 187 ~ULONGGuard() 188 { 189 if ( m_pValue ) 190 (*m_pValue)--; 191 } 192 }; 193 194 } // namespace inprocserv 195 196 #endif 197 198