1 // OutMemStream.h 2 3 #ifndef ZIP7_INC_OUT_MEM_STREAM_H 4 #define ZIP7_INC_OUT_MEM_STREAM_H 5 6 #include "../../Common/MyCom.h" 7 8 #include "MemBlocks.h" 9 10 Z7_CLASS_IMP_NOQIB_1( 11 COutMemStream 12 , IOutStream 13 ) 14 Z7_IFACE_COM7_IMP(ISequentialOutStream) 15 16 CMemBlockManagerMt *_memManager; 17 size_t _curBlockPos; 18 unsigned _curBlockIndex; 19 bool _realStreamMode; 20 21 bool _unlockEventWasSent; 22 NWindows::NSynchronization::CAutoResetEvent_WFMO StopWritingEvent; 23 NWindows::NSynchronization::CAutoResetEvent_WFMO WriteToRealStreamEvent; 24 // NWindows::NSynchronization::CAutoResetEvent NoLockEvent; 25 26 HRESULT StopWriteResult; 27 CMemLockBlocks Blocks; 28 29 CMyComPtr<ISequentialOutStream> OutSeqStream; 30 CMyComPtr<IOutStream> OutStream; 31 GetPos()32 UInt64 GetPos() const { return (UInt64)_curBlockIndex * _memManager->GetBlockSize() + _curBlockPos; } 33 34 public: 35 CreateEvents(SYNC_PARAM_DECL (synchro))36 HRESULT CreateEvents(SYNC_PARAM_DECL(synchro)) 37 { 38 WRes wres = StopWritingEvent.CreateIfNotCreated_Reset(SYNC_WFMO(synchro)); 39 if (wres == 0) 40 wres = WriteToRealStreamEvent.CreateIfNotCreated_Reset(SYNC_WFMO(synchro)); 41 return HRESULT_FROM_WIN32(wres); 42 } 43 SetOutStream(IOutStream * outStream)44 void SetOutStream(IOutStream *outStream) 45 { 46 OutStream = outStream; 47 OutSeqStream = outStream; 48 } 49 SetSeqOutStream(ISequentialOutStream * outStream)50 void SetSeqOutStream(ISequentialOutStream *outStream) 51 { 52 OutStream = NULL; 53 OutSeqStream = outStream; 54 } 55 ReleaseOutStream()56 void ReleaseOutStream() 57 { 58 OutStream.Release(); 59 OutSeqStream.Release(); 60 } 61 COutMemStream(CMemBlockManagerMt * memManager)62 COutMemStream(CMemBlockManagerMt *memManager): 63 _memManager(memManager) 64 { 65 /* 66 #ifndef _WIN32 67 StopWritingEvent._sync = 68 WriteToRealStreamEvent._sync = &memManager->Synchro; 69 #endif 70 */ 71 } 72 ~COutMemStream()73 ~COutMemStream() { Free(); } 74 void Free(); 75 76 void Init(); 77 HRESULT WriteToRealStream(); 78 79 void DetachData(CMemLockBlocks &blocks); 80 WasUnlockEventSent()81 bool WasUnlockEventSent() const { return _unlockEventWasSent; } 82 SetRealStreamMode()83 void SetRealStreamMode() 84 { 85 _unlockEventWasSent = true; 86 WriteToRealStreamEvent.Set(); 87 } 88 89 /* 90 void SetNoLockMode() 91 { 92 _unlockEventWasSent = true; 93 NoLockEvent.Set(); 94 } 95 */ 96 StopWriting(HRESULT res)97 void StopWriting(HRESULT res) 98 { 99 StopWriteResult = res; 100 StopWritingEvent.Set(); 101 } 102 }; 103 104 #endif 105