SEDNL  .
Simple Event Driven Network Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Packet.ipp
1 // SEDNL - Copyright (c) 2013 Jeremy S. Cochoy
2 //
3 // This software is provided 'as-is', without any express or implied warranty.
4 // In no event will the authors be held liable for any damages arising from
5 // the use of this software.
6 //
7 // Permission is granted to anyone to use this software for any purpose,
8 // including commercial applications, and to alter it and redistribute it
9 // freely, subject to the following restrictions:
10 //
11 // 1. The origin of this software must not be misrepresented; you must not
12 // claim that you wrote the original software. If you use this software
13 // in a product, an acknowledgment in the product documentation would
14 // be appreciated but is not required.
15 //
16 // 2. Altered source versions must be plainly marked as such, and must not
17 // be misrepresented as being the original software.
18 //
19 // 3. This notice may not be removed or altered from any source
20 // distribution.
21 
22 #ifndef PACKET_IPP_
23 #define PACKET_IPP_
24 
25 namespace SedNL
26 {
27 
28 inline
29 void Packet::swap(Packet& p) noexcept
30 {
31  std::swap(m_data, p.m_data);
32 }
33 
34 inline
35 void swap(Packet& a, Packet& b) noexcept
36 {
37  a.swap(b);
38 }
39 
41  :m_p(&p), m_idx(0)
42 {}
43 
44 template<typename T>
45 inline
46 PacketReader& operator>>(Packet &p, T &dt)
47 {
48  return (PacketReader(p) >> (Int8&)dt);
49 }
50 
51 PacketReader::operator bool() const noexcept
52 {
53  return !(m_idx >= m_p->m_data.size());
54 }
55 
57 {
58  if (!*this)
59  return Packet::Type::Unknown;
60  return static_cast<Packet::Type>(m_p->m_data[m_idx]);
61 }
62 
63 template<typename... Args>
64 Packet make_packet(Args... args)
65 {
66  Packet p;
67  write_to_packet(p, args...);
68  return p;
69 }
70 
71 template<typename T, typename... Args>
72 void write_to_packet(Packet& p, T arg, Args... args)
73 {
74  write_to_packet(p << arg, args...);
75 }
76 
77 inline void write_to_packet(Packet&)
78 {}
79 
80 template<typename T, typename... Args>
81 void read_from_packet(PacketReader& p, T& arg, Args&... args)
82 {
83  read_from_packet(p >> arg, args...);
84 }
85 
86 inline void read_from_packet(PacketReader&)
87 {}
88 
89 inline
90 unsigned short number_of_args_aux()
91 {
92  return 0;
93 }
94 
95 template<typename T, typename... Args>
96 inline
97 unsigned short number_of_args_aux(T, Args... args)
98 {
99  return 1 + number_of_args_aux(args...);
100 }
101 
102 template<typename... Args>
103 inline
104 unsigned short number_of_args(Args... args)
105 {
106  return number_of_args_aux(args...);
107 }
108 
109 template<typename... Args>
110 void write_as_object(Packet& packet, Args&... args)
111 {
112  const unsigned short nb_args = number_of_args(args...);
113 
114  packet.write_object_header(nb_args);
115  write_to_packet(packet, args...);
116 }
117 
118 template<typename... Args>
119 void read_as_object(PacketReader& packet_reader, Args&... args)
120 {
121  const unsigned short nb_args = number_of_args(args...);
122 
123  PacketReader tmp_reader = packet_reader;
124  tmp_reader.read_object_header(nb_args);
125  read_from_packet(tmp_reader, args...);
126  //If everything gone well, just modify the reader
127  using std::swap;
128  swap(packet_reader, tmp_reader);
129 }
130 
131 template<typename T>
132 inline
134 {
135  dt.serialize(*this);
136  return *this;
137 }
138 
139 template<typename T>
140 inline
142 {
143  dt.unserialize(*this);
144  return *this;
145 }
146 
147 } // namespace SedNL
148 
149 namespace std
150 {
151  template<>
152  inline
153  void swap(SedNL::Packet& lhs, SedNL::Packet& rhs) noexcept
154  {
155  SedNL::swap(lhs, rhs);
156  }
157 }
158 
159 #endif /* !PACKET_IPP_ */