-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSerializeBuffer.h
152 lines (124 loc) · 3.28 KB
/
SerializeBuffer.h
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* MacroQuest: The extension platform for EverQuest
* Copyright (C) 2002-present MacroQuest Authors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2, as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
namespace eqlib {
namespace detail
{
template <typename, typename T>
struct has_unserialize {
static_assert(
std::integral_constant<T, false>::value,
"Second template parameter needs to be of function type.");
};
// specialization that does the checking
template <typename C, typename Ret, typename... Args>
struct has_unserialize<C, Ret(Args...)> {
private:
template <typename T>
static constexpr auto check(T*)
-> typename std::is_same<
decltype(std::declval<T>().UnSerialize(std::declval<Args>()...)),
Ret>::type;
template <typename>
static constexpr std::false_type check(...);
using type = decltype(check<C>(0));
public:
static constexpr bool value = type::value;
};
}
class CUnSerializeBuffer
{
public:
const char* m_pBuffer = nullptr;
uint32_t m_uLength = 0;
uint32_t m_uReadOffset = 0;
inline CUnSerializeBuffer() = default;
inline CUnSerializeBuffer(const CUnSerializeBuffer& other)
: m_pBuffer(other.m_pBuffer)
, m_uLength(other.m_uLength)
, m_uReadOffset(other.m_uReadOffset)
{}
inline CUnSerializeBuffer(const char* buffer, uint32_t length)
: m_pBuffer(buffer)
, m_uLength(length)
{}
inline void Reset() { m_uReadOffset = 0; }
void Read(CXStr& str)
{
int length = 0;
Read(length);
if (length > 0)
{
str.assign(m_pBuffer + m_uReadOffset, length);
m_uReadOffset += length;
}
else
{
str.clear();
}
}
template <typename T>
std::enable_if_t<detail::has_unserialize<T, void(CUnSerializeBuffer&)>::value, void> Read(T& obj)
{
obj.UnSerialize(*this);
}
template <typename T>
std::enable_if_t<!detail::has_unserialize<T, void(CUnSerializeBuffer&)>::value, void> Read(T& r)
{
r = *(T*)(m_pBuffer + m_uReadOffset);
m_uReadOffset += sizeof(T);
}
void ReadString(std::string& out)
{
int len = 0;
while (m_uReadOffset < m_uLength)
{
size_t offset = m_uReadOffset++;
if (m_pBuffer[offset] == '\0')
break;
out.append(1, (char)(m_pBuffer[offset]));
}
}
template <typename T>
void Read(T* r, uint32_t size)
{
uint32_t savedSize;
Read(savedSize);
for (size_t i = 0; i < savedSize && i < size; i++)
{
Read(r[i]);
}
}
bool ReadString(char* buffer, size_t bufferSize)
{
uint32_t size = (uint32_t)strnlen(m_pBuffer + m_uReadOffset, m_uLength - m_uReadOffset) + 1;
uint32_t readAmount = std::min((uint32_t)bufferSize - 1, size);
if (!ValidateRead(readAmount))
{
*buffer = 0;
return false;
}
memcpy(buffer, m_pBuffer + m_uReadOffset, readAmount);
buffer[readAmount] = 0;
m_uReadOffset += size;
return true;
}
private:
bool ValidateRead(uint32_t amount)
{
return (m_uReadOffset + amount <= m_uLength);
}
};
//============================================================================
} // namespace eqlib