xref: /AOO41X/main/dtrans/source/win32/dtobj/DTransHelper.hxx (revision fbcf0fe955ab8df0a2f1d76ae10cf7d5c87503b1)
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 
25 #ifndef _DTRANSHELPER_HXX_
26 #define _DTRANSHELPER_HXX_
27 
28 //------------------------------------------------------------------------
29 // includes
30 //------------------------------------------------------------------------
31 
32 #if defined _MSC_VER
33 #pragma warning(push,1)
34 #endif
35 #include <windows.h>
36 #if defined _MSC_VER
37 #pragma warning(pop)
38 #endif
39 #include "..\misc\WinClip.hxx"
40 
41 //------------------------------------------------------------------------
42 // defines
43 //------------------------------------------------------------------------
44 
45 #define AUTO_INIT                 TRUE
46 #define NO_AUTO_INIT              FALSE
47 #define MEM_DESTROY_ON_RELEASE    TRUE
48 #define NO_MEM_DESTROY_ON_RELEASE FALSE
49 
50 //------------------------------------------------------------------------
51 // deklarations
52 //------------------------------------------------------------------------
53 
54 
55 //-------------------------------------------------------------------------
56 // a helper class to manage a global memory area, the clients can write
57 // into the global memory area and extract the handle to the global mem
58 // note: not thread-safe
59 //-------------------------------------------------------------------------
60 
61 class CStgTransferHelper
62 {
63 public:
64     // will be thrown in case of failures
65     class CStgTransferException
66     {
67     public:
68         HRESULT m_hr;
CStgTransferException(HRESULT hr)69         CStgTransferException( HRESULT hr ) : m_hr( hr ) {};
70     };
71 
72 public:
73     CStgTransferHelper(
74         sal_Bool bAutoInit = sal_False,
75         HGLOBAL  hGlob = NULL,
76         sal_Bool bDelStgOnRelease = sal_False );
77 
78     ~CStgTransferHelper( );
79 
80     void SAL_CALL write( const void* lpData, ULONG cb, ULONG* cbWritten = NULL );
81     void SAL_CALL read( LPVOID pv, ULONG cb, ULONG* pcbRead = NULL );
82 
83     HGLOBAL SAL_CALL getHGlobal( ) const;
84     void    SAL_CALL getIStream( LPSTREAM* ppStream );
85 
86     void SAL_CALL init(
87         SIZE_T newSize,
88         sal_uInt32 uiFlags = GHND,
89         sal_Bool bDelStgOnRelease = sal_False );
90 
91     void SAL_CALL init(
92         HGLOBAL hGlob,
93         sal_Bool bDelStgOnRelease = sal_False );
94 
95     // returns the size of the managed memory
96     sal_uInt32 SAL_CALL memSize( CLIPFORMAT cf = CF_INVALID ) const;
97 
98     // free the global memory and necessary
99     // release the internal stream pointer
100     void SAL_CALL cleanup( );
101 
102 private:
103     LPSTREAM m_lpStream;
104     sal_Bool m_bDelStgOnRelease;
105 
106 private:
107     CStgTransferHelper( const CStgTransferHelper& );
108     CStgTransferHelper& operator=( const CStgTransferHelper& );
109 };
110 
111 //-------------------------------------------------------------------------
112 // something like an auto-pointer - allows access to the memory belonging
113 // to a HGLOBAL and automatically unlocks a global memory at destruction
114 // time
115 //-------------------------------------------------------------------------
116 
117 class CRawHGlobalPtr
118 {
119 public:
120 
121     //---------------------------------------------
122     // ctor
123     //---------------------------------------------
124 
CRawHGlobalPtr(HGLOBAL hGlob)125     CRawHGlobalPtr( HGLOBAL hGlob ) :
126         m_hGlob( hGlob ),
127         m_bIsLocked( FALSE ),
128         m_pGlobMem( NULL )
129     {
130     }
131 
132 
133     //---------------------------------------------
134     // ctor
135     //---------------------------------------------
136 
CRawHGlobalPtr(const CStgTransferHelper & theHGlobalHelper)137     CRawHGlobalPtr( const CStgTransferHelper& theHGlobalHelper ) :
138         m_hGlob( theHGlobalHelper.getHGlobal( ) ),
139         m_bIsLocked( FALSE ),
140         m_pGlobMem( NULL )
141     {
142     }
143 
144     //---------------------------------------------
145     // dtor
146     //---------------------------------------------
147 
~CRawHGlobalPtr()148     ~CRawHGlobalPtr( )
149     {
150         if ( m_bIsLocked )
151             GlobalUnlock( m_hGlob );
152     }
153 
154     //---------------------------------------------
155     // lock the global memory (initializes a
156     // pointer to this memory)
157     //---------------------------------------------
158 
Lock()159     BOOL Lock( )
160     {
161         if ( !m_bIsLocked && ( NULL != m_hGlob ) )
162         {
163             m_pGlobMem = GlobalLock( m_hGlob );
164             m_bIsLocked = ( NULL != m_pGlobMem );
165         }
166 
167         return m_bIsLocked;
168     }
169 
170     //---------------------------------------------
171     // unlock the global memory (invalidates the
172     // pointer to this memory)
173     //---------------------------------------------
174 
Unlock()175     BOOL Unlock( )
176     {
177         GlobalUnlock( m_hGlob );
178         m_bIsLocked = FALSE;
179         m_pGlobMem = NULL;
180 
181         return ( NO_ERROR == GetLastError( ) );
182     }
183 
184     //---------------------------------------------
185     // locks the global memory and returns a
186     // pointer to this memory
187     //---------------------------------------------
188 
GetMemPtr()189     LPVOID GetMemPtr( )
190     {
191         Lock( );
192         return m_pGlobMem;
193     }
194 
195     //---------------------------------------------
196     // size of mem we point to
197     //---------------------------------------------
198 
MemSize() const199     int MemSize( ) const
200     {
201         return GlobalSize( m_hGlob );
202     }
203 
204 private:
205     HGLOBAL m_hGlob;
206     BOOL    m_bIsLocked;
207     LPVOID  m_pGlobMem;
208 };
209 
210 #endif
211