-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMemStream.h
More file actions
103 lines (83 loc) · 3.41 KB
/
MemStream.h
File metadata and controls
103 lines (83 loc) · 3.41 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#pragma once
NS_BEGIN
// Forward declarations
class CMemBuffer;
//#################################################################################################
class CMemStream final
{
public:
CMemStream(void);
CMemStream(const CMemStream &src);
CMemStream(CMemStream &&src) noexcept;
explicit CMemStream(const CMemBuffer &buf);
explicit CMemStream(CMemBuffer &&buf) noexcept;
explicit CMemStream(const size_t nBufSize);
CMemStream(PCBYTE pBuf, const size_t nDataSize, const size_t nBufSize = 0);
~CMemStream(void);
CMemStream &Attach(PBYTE pBuf, const size_t nDataSize, const size_t nBufSize = 0);
PBYTE Detach(void) noexcept;
CMemStream &operator=(const CMemStream &src);
CMemStream &operator=(CMemStream &&src) noexcept;
CMemStream &operator=(const CMemBuffer &buf);
CMemStream &operator=(CMemBuffer &&buf) noexcept;
bool operator==(const CMemStream &buf) const;
bool operator!=(const CMemStream &buf) const;
bool Compare(const CMemStream &buf) const;
bool Compare(PCBYTE pBuf, const size_t nDataSize) const;
operator PBYTE(void) noexcept;
operator PCBYTE(void) const noexcept;
//#################################################################################################
template<typename DATATYPE>
bool Read(DATATYPE &data, const bool bAdvanceStream = true)
{ // Read simple data types from the stream
bool bResult = false;
data = 0;
if(m_nReadPosition + sizeof(data) <= m_nDataSize)
{
std::memcpy(&data, m_pBuf + m_nReadPosition, sizeof(data));
if(bAdvanceStream)
m_nReadPosition += sizeof(data);
bResult = true;
}
return bResult;
}
// Read data from the stream
bool Read(CStr8 &strData, const size_t nSize, const bool bAdvanceStream = true);
bool Read(CStrW &strData, const size_t nSize, const bool bAdvanceStream = true);
bool Read(CStr16 &strData, const size_t nSize, const bool bAdvanceStream = true);
bool Read(CMemBuffer &bufData, const size_t nSize, const bool bAdvanceStream = true);
bool Read(PBYTE pBuf, const size_t nSize, const bool bAdvanceStream = true);
//#################################################################################################
template<typename DATATYPE>
bool Write(const DATATYPE data)
{ // Write simple data types to the stream
if(m_nDataSize + sizeof(data) > m_nBufSize)
SetBufferSize(m_nDataSize + sizeof(data)); // Existing buffer is not large enough
std::memcpy(m_pBuf + m_nDataSize, &data, sizeof(data));
m_nDataSize += sizeof(data);
return true;
}
// Write data from the stream
bool Write(const CStr8 &strData, const bool bIncludeNullTerm = true);
bool Write(const CStrW &strData, const bool bIncludeNullTerm = true);
bool Write(const CStr16 &strData, const bool bIncludeNullTerm = true);
bool Write(const CMemBuffer &bufData);
bool Write(PCBYTE pBuf, const size_t nSize);
bool IsEmpty(void) const noexcept;
void Empty(void);
void ResetReadPosition(void) noexcept;
PBYTE GetBuffer(void) noexcept;
PCBYTE GetBuffer(void) const noexcept;
size_t GetDataSize(void) const noexcept;
void SetDataSize(const size_t nDataSize) noexcept;
size_t GetBufferSize(void) const noexcept;
bool SetBufferSize(const size_t nBufSize);
size_t GetReadPosition(void) const noexcept;
void SetReadPosition(const size_t nReadPosition) noexcept;
private:
PBYTE m_pBuf;
size_t m_nDataSize;
size_t m_nBufSize;
size_t m_nReadPosition;
};
NS_END