bump product version to 4.1.6.2
[LibreOffice.git] / dtrans / source / win32 / dtobj / DTransHelper.hxx
blob8f5a8956ccc5a4ae573ea0ac676a29ea8120991d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #ifndef _DTRANSHELPER_HXX_
22 #define _DTRANSHELPER_HXX_
24 #if defined _MSC_VER
25 #pragma warning(push,1)
26 #endif
27 #include <windows.h>
28 #if defined _MSC_VER
29 #pragma warning(pop)
30 #endif
31 #include "../misc/WinClip.hxx"
33 #define AUTO_INIT TRUE
34 #define NO_AUTO_INIT FALSE
35 #define MEM_DESTROY_ON_RELEASE TRUE
36 #define NO_MEM_DESTROY_ON_RELEASE FALSE
38 //-------------------------------------------------------------------------
39 // a helper class to manage a global memory area, the clients can write
40 // into the global memory area and extract the handle to the global mem
41 // note: not thread-safe
42 //-------------------------------------------------------------------------
44 class CStgTransferHelper
46 public:
47 // will be thrown in case of failures
48 class CStgTransferException
50 public:
51 HRESULT m_hr;
52 CStgTransferException( HRESULT hr ) : m_hr( hr ) {};
55 public:
56 CStgTransferHelper(
57 sal_Bool bAutoInit = sal_False,
58 HGLOBAL hGlob = NULL,
59 sal_Bool bDelStgOnRelease = sal_False );
61 ~CStgTransferHelper( );
63 void SAL_CALL write( const void* lpData, ULONG cb, ULONG* cbWritten = NULL );
64 void SAL_CALL read( LPVOID pv, ULONG cb, ULONG* pcbRead = NULL );
66 HGLOBAL SAL_CALL getHGlobal( ) const;
67 void SAL_CALL getIStream( LPSTREAM* ppStream );
69 void SAL_CALL init(
70 SIZE_T newSize,
71 sal_uInt32 uiFlags = GHND,
72 sal_Bool bDelStgOnRelease = sal_False );
74 void SAL_CALL init(
75 HGLOBAL hGlob,
76 sal_Bool bDelStgOnRelease = sal_False );
78 // returns the size of the managed memory
79 sal_uInt32 SAL_CALL memSize( CLIPFORMAT cf = CF_INVALID ) const;
81 // free the global memory and necessary
82 // release the internal stream pointer
83 void SAL_CALL cleanup( );
85 private:
86 LPSTREAM m_lpStream;
87 sal_Bool m_bDelStgOnRelease;
89 private:
90 CStgTransferHelper( const CStgTransferHelper& );
91 CStgTransferHelper& operator=( const CStgTransferHelper& );
94 //-------------------------------------------------------------------------
95 // something like an auto-pointer - allows access to the memory belonging
96 // to a HGLOBAL and automatically unlocks a global memory at destruction
97 // time
98 //-------------------------------------------------------------------------
100 class CRawHGlobalPtr
102 public:
104 //---------------------------------------------
105 // ctor
106 //---------------------------------------------
108 CRawHGlobalPtr( HGLOBAL hGlob ) :
109 m_hGlob( hGlob ),
110 m_bIsLocked( FALSE ),
111 m_pGlobMem( NULL )
116 //---------------------------------------------
117 // ctor
118 //---------------------------------------------
120 CRawHGlobalPtr( const CStgTransferHelper& theHGlobalHelper ) :
121 m_hGlob( theHGlobalHelper.getHGlobal( ) ),
122 m_bIsLocked( FALSE ),
123 m_pGlobMem( NULL )
127 //---------------------------------------------
128 // dtor
129 //---------------------------------------------
131 ~CRawHGlobalPtr( )
133 if ( m_bIsLocked )
134 GlobalUnlock( m_hGlob );
137 //---------------------------------------------
138 // lock the global memory (initializes a
139 // pointer to this memory)
140 //---------------------------------------------
142 BOOL Lock( )
144 if ( !m_bIsLocked && ( NULL != m_hGlob ) )
146 m_pGlobMem = GlobalLock( m_hGlob );
147 m_bIsLocked = ( NULL != m_pGlobMem );
150 return m_bIsLocked;
153 //---------------------------------------------
154 // unlock the global memory (invalidates the
155 // pointer to this memory)
156 //---------------------------------------------
158 BOOL Unlock( )
160 GlobalUnlock( m_hGlob );
161 m_bIsLocked = FALSE;
162 m_pGlobMem = NULL;
164 return ( NO_ERROR == GetLastError( ) );
167 //---------------------------------------------
168 // locks the global memory and returns a
169 // pointer to this memory
170 //---------------------------------------------
172 LPVOID GetMemPtr( )
174 Lock( );
175 return m_pGlobMem;
178 //---------------------------------------------
179 // size of mem we point to
180 //---------------------------------------------
182 int MemSize( ) const
184 return GlobalSize( m_hGlob );
187 private:
188 HGLOBAL m_hGlob;
189 BOOL m_bIsLocked;
190 LPVOID m_pGlobMem;
193 #endif
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */