Imported Upstream version 9.20
[platform/upstream/7zip.git] / CPP / 7zip / Common / OutMemStream.h
1 // OutMemStream.h\r
2 \r
3 #ifndef __OUTMEMSTREAM_H\r
4 #define __OUTMEMSTREAM_H\r
5 \r
6 #include "Common/MyCom.h"\r
7 #include "MemBlocks.h"\r
8 \r
9 class COutMemStream:\r
10   public IOutStream,\r
11   public CMyUnknownImp\r
12 {\r
13   CMemBlockManagerMt *_memManager;\r
14   size_t _curBlockIndex;\r
15   size_t _curBlockPos;\r
16   bool _realStreamMode;\r
17 \r
18   bool _unlockEventWasSent;\r
19   NWindows::NSynchronization::CAutoResetEvent StopWritingEvent;\r
20   NWindows::NSynchronization::CAutoResetEvent WriteToRealStreamEvent;\r
21   // NWindows::NSynchronization::CAutoResetEvent NoLockEvent;\r
22 \r
23   HRESULT StopWriteResult;\r
24   CMemLockBlocks Blocks;\r
25 \r
26   UInt64 GetPos() const { return (UInt64)_curBlockIndex * _memManager->GetBlockSize() + _curBlockPos; }\r
27 \r
28   CMyComPtr<ISequentialOutStream> OutSeqStream;\r
29   CMyComPtr<IOutStream> OutStream;\r
30 \r
31 public:\r
32 \r
33   HRes CreateEvents()\r
34   {\r
35     RINOK(StopWritingEvent.CreateIfNotCreated());\r
36     return WriteToRealStreamEvent.CreateIfNotCreated();\r
37   }\r
38 \r
39   void SetOutStream(IOutStream *outStream)\r
40   {\r
41     OutStream = outStream;\r
42     OutSeqStream = outStream;\r
43   }\r
44 \r
45   void SetSeqOutStream(ISequentialOutStream *outStream)\r
46   {\r
47     OutStream = NULL;\r
48     OutSeqStream = outStream;\r
49   }\r
50 \r
51   void ReleaseOutStream()\r
52   {\r
53     OutStream.Release();\r
54     OutSeqStream.Release();\r
55   }\r
56 \r
57   COutMemStream(CMemBlockManagerMt *memManager): _memManager(memManager)  { }\r
58 \r
59   ~COutMemStream() { Free(); }\r
60   void Free();\r
61 \r
62   void Init();\r
63   HRESULT WriteToRealStream();\r
64 \r
65   void DetachData(CMemLockBlocks &blocks);\r
66 \r
67   bool WasUnlockEventSent() const { return _unlockEventWasSent; }\r
68 \r
69   void SetRealStreamMode()\r
70   {\r
71     _unlockEventWasSent = true;\r
72     WriteToRealStreamEvent.Set();\r
73   }\r
74 \r
75   /*\r
76   void SetNoLockMode()\r
77   {\r
78     _unlockEventWasSent = true;\r
79     NoLockEvent.Set();\r
80   }\r
81   */\r
82 \r
83   void StopWriting(HRESULT res)\r
84   {\r
85     StopWriteResult = res;\r
86     StopWritingEvent.Set();\r
87   }\r
88 \r
89   MY_UNKNOWN_IMP\r
90 \r
91   STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize);\r
92   STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition);\r
93   STDMETHOD(SetSize)(UInt64 newSize);\r
94 };\r
95 \r
96 #endif\r