Update ooo320-m1
[ooovba.git] / dtrans / source / win32 / dtobj / DTransHelper.hxx
blob5439f8703ba6846a9ec9eb3f2eca022f469e998d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: DTransHelper.hxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 #ifndef _DTRANSHELPER_HXX_
33 #define _DTRANSHELPER_HXX_
35 //------------------------------------------------------------------------
36 // includes
37 //------------------------------------------------------------------------
39 #if defined _MSC_VER
40 #pragma warning(push,1)
41 #endif
42 #include <windows.h>
43 #if defined _MSC_VER
44 #pragma warning(pop)
45 #endif
46 #include "..\misc\WinClip.hxx"
48 //------------------------------------------------------------------------
49 // defines
50 //------------------------------------------------------------------------
52 #define AUTO_INIT TRUE
53 #define NO_AUTO_INIT FALSE
54 #define MEM_DESTROY_ON_RELEASE TRUE
55 #define NO_MEM_DESTROY_ON_RELEASE FALSE
57 //------------------------------------------------------------------------
58 // deklarations
59 //------------------------------------------------------------------------
62 //-------------------------------------------------------------------------
63 // a helper class to manage a global memory area, the clients can write
64 // into the global memory area and extract the handle to the global mem
65 // note: not thread-safe
66 //-------------------------------------------------------------------------
68 class CStgTransferHelper
70 public:
71 // will be thrown in case of failures
72 class CStgTransferException
74 public:
75 HRESULT m_hr;
76 CStgTransferException( HRESULT hr ) : m_hr( hr ) {};
79 public:
80 CStgTransferHelper(
81 sal_Bool bAutoInit = sal_False,
82 HGLOBAL hGlob = NULL,
83 sal_Bool bDelStgOnRelease = sal_False );
85 ~CStgTransferHelper( );
87 void SAL_CALL write( const void* lpData, ULONG cb, ULONG* cbWritten = NULL );
88 void SAL_CALL read( LPVOID pv, ULONG cb, ULONG* pcbRead = NULL );
90 HGLOBAL SAL_CALL getHGlobal( ) const;
91 void SAL_CALL getIStream( LPSTREAM* ppStream );
93 void SAL_CALL init(
94 SIZE_T newSize,
95 sal_uInt32 uiFlags = GHND,
96 sal_Bool bDelStgOnRelease = sal_False );
98 void SAL_CALL init(
99 HGLOBAL hGlob,
100 sal_Bool bDelStgOnRelease = sal_False );
102 // returns the size of the managed memory
103 sal_uInt32 SAL_CALL memSize( CLIPFORMAT cf = CF_INVALID ) const;
105 // free the global memory and necessary
106 // release the internal stream pointer
107 void SAL_CALL cleanup( );
109 private:
110 LPSTREAM m_lpStream;
111 sal_Bool m_bDelStgOnRelease;
113 private:
114 CStgTransferHelper( const CStgTransferHelper& );
115 CStgTransferHelper& operator=( const CStgTransferHelper& );
118 //-------------------------------------------------------------------------
119 // something like an auto-pointer - allows access to the memory belonging
120 // to a HGLOBAL and automatically unlocks a global memory at destruction
121 // time
122 //-------------------------------------------------------------------------
124 class CRawHGlobalPtr
126 public:
128 //---------------------------------------------
129 // ctor
130 //---------------------------------------------
132 CRawHGlobalPtr( HGLOBAL hGlob ) :
133 m_hGlob( hGlob ),
134 m_bIsLocked( FALSE ),
135 m_pGlobMem( NULL )
140 //---------------------------------------------
141 // ctor
142 //---------------------------------------------
144 CRawHGlobalPtr( const CStgTransferHelper& theHGlobalHelper ) :
145 m_hGlob( theHGlobalHelper.getHGlobal( ) ),
146 m_bIsLocked( FALSE ),
147 m_pGlobMem( NULL )
151 //---------------------------------------------
152 // dtor
153 //---------------------------------------------
155 ~CRawHGlobalPtr( )
157 if ( m_bIsLocked )
158 GlobalUnlock( m_hGlob );
161 //---------------------------------------------
162 // lock the global memory (initializes a
163 // pointer to this memory)
164 //---------------------------------------------
166 BOOL Lock( )
168 if ( !m_bIsLocked && ( NULL != m_hGlob ) )
170 m_pGlobMem = GlobalLock( m_hGlob );
171 m_bIsLocked = ( NULL != m_pGlobMem );
174 return m_bIsLocked;
177 //---------------------------------------------
178 // unlock the global memory (invalidates the
179 // pointer to this memory)
180 //---------------------------------------------
182 BOOL Unlock( )
184 GlobalUnlock( m_hGlob );
185 m_bIsLocked = FALSE;
186 m_pGlobMem = NULL;
188 return ( NO_ERROR == GetLastError( ) );
191 //---------------------------------------------
192 // locks the global memory and returns a
193 // pointer to this memory
194 //---------------------------------------------
196 LPVOID GetMemPtr( )
198 Lock( );
199 return m_pGlobMem;
202 //---------------------------------------------
203 // size of mem we point to
204 //---------------------------------------------
206 int MemSize( ) const
208 return GlobalSize( m_hGlob );
211 private:
212 HGLOBAL m_hGlob;
213 BOOL m_bIsLocked;
214 LPVOID m_pGlobMem;
217 #endif