Skylicht Engine
Loading...
Searching...
No Matches
CMemoryStream.h
1/*
2!@
3MIT License
4
5Copyright (c) 2020 Skylicht Technology CO., LTD
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
8(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
9merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
10subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
20This file is part of the "Skylicht Engine".
21https://github.com/skylicht-lab/skylicht-engine
22!#
23*/
24
25#pragma once
26
27namespace Skylicht
28{
36 class SKYLICHT_API CMemoryStream
37 {
38 protected:
39 unsigned char* m_memory;
40 unsigned int m_size;
41 unsigned int m_totalSize;
42 unsigned int m_pos;
43 bool m_fromMemory;
44 public:
49 CMemoryStream(unsigned int initMem = 512);
50
56 CMemoryStream(unsigned char* fromMem, unsigned int size);
57
67
71 virtual ~CMemoryStream();
72
78 bool autoGrow(unsigned int writeSize);
79
85 void writeData(const void* data, unsigned int size);
91
93 void writeChar(char data);
95 void writeShort(short data);
97 void writeUShort(unsigned short data);
99 void writeInt(int data);
101 void writeUInt(unsigned int data);
103 void writeFloat(float data);
105 void writeDouble(double data);
107 void writeString(const std::string& s);
109 void writeString(const std::wstring& s);
115 void writeFloatArray(const float* f, int count);
116
123 unsigned int readData(void* data, unsigned int size);
124
126 char readChar();
128 short readShort();
130 unsigned short readUShort();
132 int readInt();
134 unsigned int readUInt();
136 float readFloat();
138 double readDouble();
140 std::string readString();
142 std::wstring readWString();
148 void readFloatArray(float* f, int count);
149
154 unsigned char* getData()
155 {
156 return m_memory;
157 }
158
163 unsigned int getSize()
164 {
165 return m_size;
166 }
167
172 unsigned int getPos()
173 {
174 return m_pos;
175 }
176
181 unsigned int getTotalSize()
182 {
183 return m_totalSize;
184 }
185
190 void setPos(unsigned int pos)
191 {
192 m_pos = pos;
193 }
194
199 {
200 m_size = 0;
201 m_pos = 0;
202 }
203
209 void encrypt();
215 void decrypt();
216 };
217
218}
void writeString(const std::string &s)
Append a UTF-8/narrow string with its character count prefix.
void writeShort(short data)
Append a signed 16-bit integer.
unsigned int getTotalSize()
Get the total allocated capacity.
Definition CMemoryStream.h:181
unsigned int getSize()
Get the number of bytes written or available for reading.
Definition CMemoryStream.h:163
unsigned short readUShort()
Read an unsigned 16-bit integer and advance the read position.
CMemoryStream(unsigned int initMem=512)
Create an owning memory stream.
char readChar()
Read a signed 8-bit character and advance the read position.
void writeFloat(float data)
Append a 32-bit floating-point value.
void writeChar(char data)
Append a signed 8-bit character.
double readDouble()
Read a 64-bit floating-point value and advance the read position.
void encrypt()
Encrypt the stream contents.
void decrypt()
Decrypt the stream contents.
void writeString(const std::wstring &s)
Append a wide string with its character count prefix.
void writeData(const void *data, unsigned int size)
Append raw bytes to the stream.
int readInt()
Read a signed 32-bit integer and advance the read position.
void writeDouble(double data)
Append a 64-bit floating-point value.
short readShort()
Read a signed 16-bit integer and advance the read position.
void setPos(unsigned int pos)
Set the current read position.
Definition CMemoryStream.h:190
void readFloatArray(float *f, int count)
Read an array of floats and advance the read position.
float readFloat()
Read a 32-bit floating-point value and advance the read position.
unsigned int readUInt()
Read an unsigned 32-bit integer and advance the read position.
void writeInt(int data)
Append a signed 32-bit integer.
std::string readString()
Read a narrow string written by writeString(const std::string&).
void writeStream(CMemoryStream *stream)
Append the full contents of another memory stream.
void writeUShort(unsigned short data)
Append an unsigned 16-bit integer.
bool autoGrow(unsigned int writeSize)
Ensure the stream has enough capacity for an upcoming write.
virtual ~CMemoryStream()
Destroy the stream and release owned memory.
void resetWrite()
Clear written data and reset the read position.
Definition CMemoryStream.h:198
unsigned int getPos()
Get the current read position.
Definition CMemoryStream.h:172
void writeFloatArray(const float *f, int count)
Append an array of floats.
void writeUInt(unsigned int data)
Append an unsigned 32-bit integer.
CMemoryStream(unsigned char *fromMem, unsigned int size)
Wrap an existing memory block for reading or fixed-size writing.
CMemoryStream(const CMemoryStream &stream)
Copy a stream into a new backing buffer.
std::wstring readWString()
Read a wide string written by writeString(const std::wstring&).
unsigned char * getData()
Get the backing memory pointer.
Definition CMemoryStream.h:154
unsigned int readData(void *data, unsigned int size)
Read raw bytes from the current read position.
Everything in the Skylicht Engine. You can start by looking at the topics.
Definition AudioDebugLog.h:29