forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamWriter.h
More file actions
60 lines (52 loc) · 1.73 KB
/
Copy pathStreamWriter.h
File metadata and controls
60 lines (52 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#pragma once
namespace Js
{
//
// A simple stream writer that hides low level stream details.
//
// Note:
// This stream writer uses an internal buffer. Must call Flush() at the end to ensure
// any remained content in the internal buffer is sent to the output stream.
//
class StreamWriter: public ScriptContextHolder
{
private:
HostStream *m_stream;
byte *m_buffer;
size_t m_current;
size_t m_capacity;
public:
StreamWriter(ScriptContext* scriptContext, HostStream* stream)
: ScriptContextHolder(scriptContext),
m_stream(stream),
m_buffer(nullptr),
m_current(0),
m_capacity(0)
{
}
byte* GetBuffer() { return m_buffer; }
size_t GetLength() { return m_current; }
void Write(const void* pv, size_t cb);
void WriteHostObject(void* data);
template <typename T>
void Write(const T& value)
{
if ((m_current + sizeof(T)) < m_capacity)
{
*(T*)(m_buffer + m_current) = value;
m_current += sizeof(T);
}
else
{
Write(&value, sizeof(T));
}
}
//_Post_satisfies_(m_cur == 0)
//void Flush();
scaposition_t GetPosition() const;
};
}