-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbytebuffer.h
More file actions
60 lines (52 loc) · 1.68 KB
/
bytebuffer.h
File metadata and controls
60 lines (52 loc) · 1.68 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
#ifndef __BYTE_BUFFER_HEADER__
#define __BYTE_BUFFER_HEADER__
#include <cstdlib>
#include <ctime>
#include <string>
#include <vector>
class ByteBuffer {
private:
std::vector<unsigned char> buffer;
std::vector<unsigned char>::const_iterator it;
public:
ByteBuffer() : it(buffer.begin()) { }
// Initial capacity in bytes
ByteBuffer(int initialCapacity) {
buffer.reserve(initialCapacity);
it = buffer.begin();
}
// The input here must have been produced by ByteBuffer::Serialized()
ByteBuffer(char *serialized);
// Returned byte array must be deleted
char *Serialized();
// The size of the array returned by ByteBuffer::Serialized()
unsigned int SerializedSize() { return buffer.size() + sizeof(unsigned int); }
void SerializeBool(bool x);
void SerializeChar(char x);
void SerializeShort(short x);
void SerializeInt(int x);
void SerializeLong(long x);
void SerializeLongLong(long long x);
void SerializeFloat(float x);
void SerializeDouble(double x);
void SerializeLongDouble(long double x);
void SerializeTime_t(time_t x);
void SerializeSize_t(size_t x);
void SerializeString(const std::string &str);
// Must be called if any Serialize methods have been called
// between the constructor and deserialization
void StartDeserialize() { it = buffer.begin(); }
bool DeserializeBool();
char DeserializeChar();
short DeserializeShort();
int DeserializeInt();
long DeserializeLong();
long long DeserializeLongLong();
float DeserializeFloat();
double DeserializeDouble();
long double DeserializeLongDouble();
time_t DeserializeTime_t();
size_t DeserializeSize_t();
std::string DeserializeString();
};
#endif // __BYTE_BUFFER_HEADER__