LibNRG  0.0.1
Networking for Real-time Games library
 All Classes Files Functions Variables Friends
nrg_varint.h
Go to the documentation of this file.
1 /*
2  LibNRG - Networking for Real-time Games
3 
4  Copyright (C) 2012-2014 Alex Baines <alex@abaines.me.uk>
5 
6  This software is provided 'as-is', without any express or implied
7  warranty. In no event will the authors be held liable for any damages
8  arising from the use of this software.
9 
10  Permission is granted to anyone to use this software for any purpose,
11  including commercial applications, and to alter it and redistribute it
12  freely, subject to the following restrictions:
13 
14  1. The origin of this software must not be misrepresented; you must not
15  claim that you wrote the original software. If you use this software
16  in a product, an acknowledgment in the product documentation would be
17  appreciated but is not required.
18  2. Altered source versions must be plainly marked as such, and must not be
19  misrepresented as being the original software.
20  3. This notice may not be removed or altered from any source distribution.
21 */
25 #ifndef NRG_VARINT_H
26 #define NRG_VARINT_H
27 #include "nrg_core.h"
28 #include <type_traits>
29 
30 namespace nrg {
31 
32 using namespace std;
33 
34 namespace detail {
35 
36 template<class T>
37 struct varint_bytes {
38  static const size_t value = 1 + ((sizeof(T) * 8) - 1) / 7;
39 };
40 
41 template<class T, class = typename enable_if<is_signed<T>::value>::type>
42 typename make_unsigned<T>::type varint_zigzag(T i){
43  return (i << 1) ^ (i >> numeric_limits<T>::digits);
44 }
45 
46 template<class T, class = typename enable_if<is_unsigned<T>::value>::type>
47 typename make_signed<T>::type varint_zagzig(T i){
48  return (i >> 1) ^ (-(i & 1));
49 }
50 
51 template<typename T>
52 typename enable_if<is_unsigned<T>::value, size_t>::type varint_decode(Packet& p, T& ans){
53  ans = 0;
54  size_t i = 0;
55  uint8_t byte = 0x80;
56 
57  while(i < varint_bytes<T>::value && (byte & 0x80)){
58  if(!p.remaining()) return 0;
59  p.read8(byte);
60  ans |= ((byte & 0x7F) << (i++ * 7));
61  }
62 
63  return i;
64 }
65 
66 template<typename T>
67 typename enable_if<is_unsigned<T>::value, size_t>::type varint_encode(Packet& p, T num){
68  uint8_t enc[varint_bytes<T>::value];
69  size_t i = 0;
70 
71  while(enc[i++] = (num & 0x7F), num >>= 7){
72  enc[i-1] |= 0x80;
73  }
74 
75  p.writeArray(enc, i);
76  return i;
77 }
78 
79 }
80 
82 template<class T, class = void>
83 struct TVarint;
84 
86 template<class T>
87 struct TVarint<T, typename std::enable_if<std::is_unsigned<T>::value>::type> {
88 
89  TVarint() : data(0){}
90  TVarint(const T& t) : data(t){}
91 
92  size_t requiredBytes(void) const {
93  size_t ret = 1;
94  T tmp = data;
95  while(tmp >>= 7) ++ret;
96  return ret;
97  }
98 
99  size_t encode(Packet& p) const {
100  return detail::varint_encode(p, data);
101  }
102 
103  size_t decode(Packet& p){
104  return detail::varint_decode(p, data);
105  }
106 
107  T quickDecode(Packet& p){
108  T t = 0;
109  detail::varint_decode(p, t);
110  return t;
111  }
112 
113  operator T () const { return data; }
114  T get() const { return data; }
115 
116  T data;
117 };
118 
120 template<class T>
121 struct TVarint<T, typename std::enable_if<std::is_signed<T>::value>::type> {
122 
123  TVarint() : data(0){}
124  TVarint(const T& t) : data(t){}
125 
126  size_t requiredBytes(void) const {
127  size_t ret = 1;
128  typename std::make_unsigned<T>::type tmp = detail::varint_zigzag(data);
129  while(tmp >>= 7) ++ret;
130  return ret;
131  }
132 
133  size_t encode(Packet& p) const {
134  return detail::varint_encode(p, detail::varint_zigzag(data));
135  }
136 
137  size_t decode(Packet& p){
138  typename std::make_unsigned<T>::type udata = 0;
139  size_t r = detail::varint_decode(p, udata);
140  data = detail::varint_zagzig(udata);
141  return r;
142  }
143 
144  T quickDecode(Packet& p){
145  typename std::make_unsigned<T>::type udata = 0;
146  detail::varint_decode(p, udata);
147  return detail::varint_zagzig(udata);
148  }
149 
150  operator T () const { return data; }
151  T get() const { return data; }
152 
153  T data;
154 };
155 
157 typedef TVarint<uint64_t> UVarint;
159 typedef TVarint<int64_t> SVarint;
160 
161 }
162 
163 #endif
Default undeclared template instance to cause a compilation error for non-integer types...
Definition: nrg_varint.h:83
Class for storing data to be sent / received across the network.
Definition: nrg_packet.h:58
Common defines and includes used by all the other nrg header files.