merge the formfield patch from ooo-build
[ooovba.git] / embedserv / source / inprocserv / smartpointer.hxx
blob84e55ec013bc1bceda5c7967fd34f4e76089a981
1 /*************************************************************************
3 * OpenOffice.org - a multi-platform office productivity suite
5 * $RCSfile: smartpointer.hxx,v $
7 * $Revision: 1.1.8.2 $
9 * last change: $Author: mav $ $Date: 2008/10/30 11:59:06 $
11 * The Contents of this file are made available subject to
12 * the terms of GNU Lesser General Public License Version 2.1.
15 * GNU Lesser General Public License Version 2.1
16 * =============================================
17 * Copyright 2005 by Sun Microsystems, Inc.
18 * 901 San Antonio Road, Palo Alto, CA 94303, USA
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Lesser General Public
22 * License version 2.1, as published by the Free Software Foundation.
24 * This library is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * Lesser General Public License for more details.
29 * You should have received a copy of the GNU Lesser General Public
30 * License along with this library; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 * MA 02111-1307 USA
34 ************************************************************************/
36 #ifndef _INPROCSERV_SMARTPOINTER_HXX_
37 #define _INPROCSERV_SMARTPOINTER_HXX_
39 // #define OWNDEBUG
41 #ifdef OWNDEBUG
42 #define WRITEDEBUGINFOINTERN( x ) WriteDebugInfo( (DWORD)this, x, sizeof( x ) )
43 #define WRITEDEBUGINFO( x ) WRITEDEBUGINFOINTERN( x ":" MY_STRING_LINE "\n" )
44 #define TO_STRING( x ) #x
45 #define MACRO_VALUE_TO_STRING( x ) TO_STRING( x )
46 #define MY_STRING_LINE MACRO_VALUE_TO_STRING( __LINE__ )
47 #else
48 #define WRITEDEBUGINFO( x ) void()
49 #define MY_STRING_LINE
50 #endif
53 namespace inprocserv{
55 void WriteDebugInfo( DWORD pThis, char* pString, DWORD nToWrite );
57 template< class T > class ComSmart
59 T* m_pInterface;
61 void OwnRelease()
63 if ( m_pInterface )
65 T* pInterface = m_pInterface;
66 m_pInterface = NULL;
67 pInterface->Release();
71 public:
72 ComSmart()
73 : m_pInterface( NULL )
76 ComSmart( const ComSmart<T>& rObj )
77 : m_pInterface( rObj.m_pInterface )
79 if ( m_pInterface != NULL )
80 m_pInterface->AddRef();
83 ComSmart( T* pInterface )
84 : m_pInterface( pInterface )
86 if ( m_pInterface != NULL )
87 m_pInterface->AddRef();
90 ~ComSmart()
92 OwnRelease();
95 ComSmart& operator=( const ComSmart<T>& rObj )
97 OwnRelease();
99 m_pInterface = rObj.m_pInterface;
101 if ( m_pInterface != NULL )
102 m_pInterface->AddRef();
104 return *this;
107 ComSmart<T>& operator=( T* pInterface )
109 OwnRelease();
111 m_pInterface = pInterface;
113 if ( m_pInterface != NULL )
114 m_pInterface->AddRef();
116 return *this;
119 operator T*() const
121 return m_pInterface;
124 T& operator*() const
126 return *m_pInterface;
129 T** operator&()
131 OwnRelease();
133 m_pInterface = NULL;
135 return &m_pInterface;
138 T* operator->() const
140 return m_pInterface;
143 BOOL operator==( const ComSmart<T>& rObj ) const
145 return ( m_pInterface == rObj.m_pInterface );
148 BOOL operator!=( const ComSmart<T>& rObj ) const
150 return ( m_pInterface != rObj.m_pInterface );
153 BOOL operator==( const T* pInterface ) const
155 return ( m_pInterface == pInterface );
158 BOOL operator!=( const T* pInterface ) const
160 return ( m_pInterface != pInterface );
164 class CSGuard
166 CRITICAL_SECTION* m_pCriticalSection;
168 public:
169 CSGuard( CRITICAL_SECTION* pCS )
170 : m_pCriticalSection( pCS )
172 if ( m_pCriticalSection )
173 EnterCriticalSection( m_pCriticalSection );
176 ~CSGuard()
178 if ( m_pCriticalSection )
179 LeaveCriticalSection( m_pCriticalSection );
183 class ULONGGuard
185 ULONG* m_pValue;
187 public:
188 ULONGGuard( ULONG* pValue )
189 : m_pValue( pValue )
191 if ( m_pValue )
192 (*m_pValue)++;
195 ~ULONGGuard()
197 if ( m_pValue )
198 (*m_pValue)--;
202 } // namespace inprocserv
204 #endif