FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_packet.inl
1/*
2 * Copyright 2024 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17namespace fge
18{
19namespace net
20{
21
22fge::net::Packet& Packet::operator<<(bool data)
23{
24 uint8_t a = data ? 1 : 0;
25 return this->append(&a, sizeof(uint8_t));
26}
27
28fge::net::Packet& Packet::operator<<(int8_t data)
29{
30 return this->append(&data, sizeof(int8_t));
31}
32fge::net::Packet& Packet::operator<<(int16_t data)
33{
34 return this->pack(&data, sizeof(int16_t));
35}
36fge::net::Packet& Packet::operator<<(int32_t data)
37{
38 return this->pack(&data, sizeof(int32_t));
39}
40fge::net::Packet& Packet::operator<<(int64_t data)
41{
42 return this->pack(&data, sizeof(int64_t));
43}
44
45fge::net::Packet& Packet::operator<<(uint8_t data)
46{
47 return this->append(&data, sizeof(uint8_t));
48}
49fge::net::Packet& Packet::operator<<(uint16_t data)
50{
51 return this->pack(&data, sizeof(uint16_t));
52}
53fge::net::Packet& Packet::operator<<(uint32_t data)
54{
55 return this->pack(&data, sizeof(uint32_t));
56}
57fge::net::Packet& Packet::operator<<(uint64_t data)
58{
59 return this->pack(&data, sizeof(uint64_t));
60}
61
62fge::net::Packet& Packet::operator<<(float data)
63{
64 return this->pack(&data, sizeof(float));
65}
66fge::net::Packet& Packet::operator<<(double data)
67{
68 return this->pack(&data, sizeof(double));
69}
70fge::net::Packet& Packet::operator<<(long double data)
71{
72 return this->pack(&data, sizeof(long double));
73}
74
75template<typename T>
76fge::net::Packet& Packet::operator<<(std::forward_list<T> const& data)
77{
78 *this << static_cast<fge::net::SizeType>(data.size());
79 for (auto it = data.cbegin(); it != data.cend(); ++it)
80 {
81 *this << (*it);
82 }
83 return *this;
84}
85template<typename T>
86fge::net::Packet& Packet::operator<<(std::list<T> const& data)
87{
88 *this << static_cast<fge::net::SizeType>(data.size());
89 for (auto it = data.cbegin(); it != data.cend(); ++it)
90 {
91 *this << (*it);
92 }
93 return *this;
94}
95template<typename T>
96fge::net::Packet& Packet::operator<<(std::vector<T> const& data)
97{
98 *this << static_cast<fge::net::SizeType>(data.size());
99 for (std::size_t i = 0; i < data.size(); ++i)
100 {
101 *this << data[i];
102 }
103 return *this;
104}
105
106template<typename T>
107fge::net::Packet& Packet::operator<<(fge::Vector2<T> const& data)
108{
109 return *this << data.x << data.y;
110}
111template<typename T>
112fge::net::Packet& Packet::operator<<(fge::Vector3<T> const& data)
113{
114 return *this << data.x << data.y << data.z;
115}
116
117template<typename T>
118fge::net::Packet& Packet::operator<<(fge::Matrix<T> const& data)
119{
120 *this << static_cast<fge::net::SizeType>(data.getSizeX()) << static_cast<fge::net::SizeType>(data.getSizeY());
121 for (std::size_t x = 0; x < data.getSizeX(); ++x)
122 {
123 for (std::size_t y = 0; y < data.getSizeY(); ++y)
124 {
125 *this << data[x][y];
126 }
127 }
128 return *this;
129}
130
131fge::net::Packet& Packet::operator<<(fge::Color const& data)
132{
133 return *this << static_cast<uint32_t>(data.toInteger());
134}
135
136template<class TEnum, typename>
137fge::net::Packet& Packet::operator<<(TEnum const& data)
138{
139 using TTEnum = std::underlying_type_t<TEnum>;
140 return this->pack(&reinterpret_cast<TTEnum const&>(data), sizeof(TTEnum));
141}
142
143template<class TData>
144fge::net::Packet& Packet::operator<<(std::unique_ptr<TData> const& data)
145{
146 return *this << *data;
147}
148
150
151fge::net::Packet const& Packet::operator>>(bool& data) const
152{
153 uint8_t a;
154 this->read(&a, sizeof(uint8_t));
155 data = (a > 0);
156 return *this;
157}
158
159fge::net::Packet const& Packet::operator>>(int8_t& data) const
160{
161 return this->read(&data, sizeof(int8_t));
162}
163fge::net::Packet const& Packet::operator>>(int16_t& data) const
164{
165 return this->unpack(&data, sizeof(int16_t));
166}
167fge::net::Packet const& Packet::operator>>(int32_t& data) const
168{
169 return this->unpack(&data, sizeof(int32_t));
170}
171fge::net::Packet const& Packet::operator>>(int64_t& data) const
172{
173 return this->unpack(&data, sizeof(int64_t));
174}
175
176fge::net::Packet const& Packet::operator>>(uint8_t& data) const
177{
178 return this->read(&data, sizeof(uint8_t));
179}
180fge::net::Packet const& Packet::operator>>(uint16_t& data) const
181{
182 return this->unpack(&data, sizeof(uint16_t));
183}
184fge::net::Packet const& Packet::operator>>(uint32_t& data) const
185{
186 return this->unpack(&data, sizeof(uint32_t));
187}
188fge::net::Packet const& Packet::operator>>(uint64_t& data) const
189{
190 return this->unpack(&data, sizeof(uint64_t));
191}
192
193fge::net::Packet const& Packet::operator>>(float& data) const
194{
195 return this->unpack(&data, sizeof(float));
196}
197fge::net::Packet const& Packet::operator>>(double& data) const
198{
199 return this->unpack(&data, sizeof(double));
200}
201fge::net::Packet const& Packet::operator>>(long double& data) const
202{
203 return this->unpack(&data, sizeof(long double));
204}
205
206template<typename T>
207fge::net::Packet const& Packet::operator>>(std::forward_list<T>& data) const
208{
209 fge::net::SizeType length = 0;
210 *this >> length;
211
212 data.resize(length);
213 auto it = data.begin();
214
215 for (fge::net::SizeType i = 0; i < length; ++i)
216 {
217 *this >> (*it);
218 ++it;
219 }
220 return *this;
221}
222template<typename T>
223fge::net::Packet const& Packet::operator>>(std::list<T>& data) const
224{
225 fge::net::SizeType length = 0;
226 *this >> length;
227
228 data.resize(length);
229 auto it = data.begin();
230
231 for (fge::net::SizeType i = 0; i < length; ++i)
232 {
233 *this >> (*it);
234 ++it;
235 }
236 return *this;
237}
238template<typename T>
239fge::net::Packet const& Packet::operator>>(std::vector<T>& data) const
240{
241 fge::net::SizeType length = 0;
242 *this >> length;
243
244 data.resize(length);
245
246 for (fge::net::SizeType i = 0; i < length; ++i)
247 {
248 *this >> data[i];
249 }
250 return *this;
251}
252
253template<typename T>
254fge::net::Packet const& Packet::operator>>(fge::Vector2<T>& data) const
255{
256 return *this >> data.x >> data.y;
257}
258template<typename T>
259fge::net::Packet const& Packet::operator>>(fge::Vector3<T>& data) const
260{
261 return *this >> data.x >> data.y >> data.z;
262}
263
264template<typename T>
265fge::net::Packet const& Packet::operator>>(fge::Matrix<T>& data) const
266{
267 fge::net::SizeType sizeX = 0, sizeY = 0;
268 *this >> sizeX >> sizeY;
269
270 data.setSize(sizeX, sizeY);
271
272 for (fge::net::SizeType x = 0; x < sizeX; ++x)
273 {
274 for (fge::net::SizeType y = 0; y < sizeY; ++y)
275 {
276 *this >> data[x][y];
277 }
278 }
279 return *this;
280}
281
282fge::net::Packet const& Packet::operator>>(fge::Color& data) const
283{
284 uint32_t buff;
285 *this >> buff;
286 data = fge::Color(buff);
287 return *this;
288}
289
290template<class TEnum, typename>
291fge::net::Packet const& Packet::operator>>(TEnum& data) const
292{
293 using TTEnum = std::underlying_type_t<TEnum>;
294 return this->unpack(&reinterpret_cast<TTEnum&>(data), sizeof(TTEnum));
295}
296
297template<class TData>
298fge::net::Packet const& Packet::operator>>(std::unique_ptr<TData>& data) const
299{
300 if (!data)
301 {
302 data = std::make_unique<TData>();
303 }
304 return *this >> *data;
305}
306
307} // namespace net
308} // namespace fge
Definition C_color.hpp:35
A container to store a 2D matrix of any type.
Definition C_matrix.hpp:40
void setSize(fge::Vector2< Tvec > const &msize)
Set the size of the matrix.
Definition C_matrix.inl:309
std::size_t getSizeY() const
Get the y size of the matrix.
Definition C_matrix.inl:270
std::size_t getSizeX() const
Get the x size of the matrix.
Definition C_matrix.inl:265
Definition C_packet.hpp:70