neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
data_packet.hpp
Go to the documentation of this file.
1// data_packet.hpp
2/*
3 * Copyright (c) 2007 Leigh Johnston.
4 *
5 * All rights reserved.
6 *
7 * Redistribution and use in source and data forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in data form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * * Neither the name of Leigh Johnston nor the names of any
19 * other contributors to this software may be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*/
35
36#pragma once
37
38#include <neolib/neolib.hpp>
39#include <string>
41
42namespace neolib
43{
44 namespace detail
45 {
46 template <typename PacketType, typename T> struct decoder;
47 template <typename PacketType> struct decoder<PacketType, uint8_t> { uint8_t operator()(const PacketType& p) const { return static_cast<uint8_t>(p.decode_integer(sizeof(uint8_t))); } };
48 template <typename PacketType> struct decoder<PacketType, uint16_t> { uint16_t operator()(const PacketType& p) const { return static_cast<uint16_t>(p.decode_integer(sizeof(uint16_t))); } };
49 template <typename PacketType> struct decoder<PacketType, uint32_t> { uint32_t operator()(const PacketType& p) const { return static_cast<uint32_t>(p.decode_integer(sizeof(uint32_t))); } };
50 template <typename PacketType> struct decoder<PacketType, uint64_t> { uint64_t operator()(const PacketType& p) const { return static_cast<uint64_t>(p.decode_integer(sizeof(uint64_t))); } };
51 template <typename PacketType> struct decoder<PacketType, int8_t> { uint8_t operator()(const PacketType& p) const { return static_cast<int8_t>(p.decode_integer(sizeof(int8_t))); } };
52 template <typename PacketType> struct decoder<PacketType, int16_t> { uint8_t operator()(const PacketType& p) const { return static_cast<int16_t>(p.decode_integer(sizeof(int16_t))); } };
53 template <typename PacketType> struct decoder<PacketType, int32_t> { int32_t operator()(const PacketType& p) const { return static_cast<int32_t>(p.decode_integer(sizeof(int32_t))); } };
54 template <typename PacketType> struct decoder<PacketType, int64_t> { int64_t operator()(const PacketType& p) const { return static_cast<int64_t>(p.decode_integer(sizeof(int64_t))); } };
55 template <typename PacketType> struct decoder<PacketType, bool> { bool operator()(const PacketType& p) const { return p.decode_bool(); } };
56 template <typename PacketType> struct decoder<PacketType, typename PacketType::string_type> { typename PacketType::string_type operator()(const PacketType& p) const { return p.decode_string(); } };
57 }
58
59 template <typename CharType>
61 {
62 typedef basic_data_packet<CharType> self_type;
64 // types
65 public:
72 typedef std::basic_string<CharType> string_type;
73 // interface
74 public:
75 void encode(uint8_t aValue)
76 {
77 encode(static_cast<uint64_t>(aValue), sizeof(uint8_t));
78 }
79 void encode(uint16_t aValue)
80 {
81 encode(static_cast<uint64_t>(aValue), sizeof(uint16_t))
82 }
83 void encode(uint32_t aValue)
84 {
85 encode(static_cast<uint64_t>(aValue), sizeof(uint32_t))
86 }
87 void encode(uint64_t aValue)
88 {
89 encode(static_cast<uint64_t>(aValue), sizeof(uint64_t))
90 }
91 void encode(int8_t aValue)
92 {
93 encode(static_cast<uint64_t>(aValue), sizeof(int8_t));
94 }
95 void encode(int16_t aValue)
96 {
97 encode(static_cast<uint64_t>(aValue), sizeof(int16_t))
98 }
99 void encode(int32_t aValue)
100 {
101 encode(static_cast<uint64_t>(aValue), sizeof(int32_t))
102 }
103 void encode(int64_t aValue)
104 {
105 encode(static_cast<uint64_t>(aValue), sizeof(int64_t))
106 }
107 void encode(int64_t aValue, std::size_t aLength)
108 {
109 encode(static_cast<uint64_t>(aValue), aLength);
110 }
111 virtual void encode(uint64_t aValue, std::size_t aLength) = 0;
112 virtual void encode(bool aValue) = 0;
113 virtual void encode(const string_type& aValue) = 0;
114 template <typename T>
115 T decode() const
116 {
117 return detail::decoder<self_type, T>()(*this);
118 }
119 virtual uint64_t decode_integer(std::size_t aLength) const = 0;
120 virtual bool decode_bool() const = 0;
121 virtual string_type decode_string() const = 0;
122 };
123
125}
base_type::character_type character_type
void encode(int32_t aValue)
void encode(uint64_t aValue)
void encode(int8_t aValue)
void encode(int64_t aValue)
void encode(int64_t aValue, std::size_t aLength)
base_type::size_type size_type
virtual void encode(const string_type &aValue)=0
void encode(uint32_t aValue)
virtual uint64_t decode_integer(std::size_t aLength) const =0
virtual void encode(uint64_t aValue, std::size_t aLength)=0
virtual string_type decode_string() const =0
void encode(uint8_t aValue)
void encode(uint16_t aValue)
base_type::pointer pointer
base_type::iterator iterator
virtual bool decode_bool() const =0
void encode(int16_t aValue)
virtual void encode(bool aValue)=0
std::basic_string< CharType > string_type
base_type::const_pointer const_pointer
base_type::const_iterator const_iterator
character_type * pointer
Definition i_packet.hpp:51
const character_type * const_pointer
Definition i_packet.hpp:50
std::size_t size_type
Definition i_packet.hpp:52
const_pointer const_iterator
Definition i_packet.hpp:53
basic_data_packet< char > data_packet
bool operator()(const PacketType &p) const
uint8_t operator()(const PacketType &p) const
int32_t operator()(const PacketType &p) const
int64_t operator()(const PacketType &p) const
uint8_t operator()(const PacketType &p) const
PacketType::string_type operator()(const PacketType &p) const
uint16_t operator()(const PacketType &p) const
uint32_t operator()(const PacketType &p) const
uint64_t operator()(const PacketType &p) const
uint8_t operator()(const PacketType &p) const