28 #include <type_traits>
38 static const size_t value = 1 + ((
sizeof(T) * 8) - 1) / 7;
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);
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));
52 typename enable_if<is_unsigned<T>::value,
size_t>::type varint_decode(Packet& p, T& ans){
57 while(i < varint_bytes<T>::value && (byte & 0x80)){
58 if(!p.remaining())
return 0;
60 ans |= ((byte & 0x7F) << (i++ * 7));
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];
71 while(enc[i++] = (num & 0x7F), num >>= 7){
82 template<
class T,
class =
void>
87 struct TVarint<T, typename std::enable_if<std::is_unsigned<T>::value>::type> {
90 TVarint(
const T& t) : data(t){}
92 size_t requiredBytes(
void)
const {
95 while(tmp >>= 7) ++ret;
99 size_t encode(
Packet& p)
const {
100 return detail::varint_encode(p, data);
104 return detail::varint_decode(p, data);
109 detail::varint_decode(p, t);
113 operator T ()
const {
return data; }
114 T
get()
const {
return data; }
121 struct TVarint<T, typename std::enable_if<std::is_signed<T>::value>::type> {
124 TVarint(
const T& t) : data(t){}
126 size_t requiredBytes(
void)
const {
128 typename std::make_unsigned<T>::type tmp = detail::varint_zigzag(data);
129 while(tmp >>= 7) ++ret;
133 size_t encode(
Packet& p)
const {
134 return detail::varint_encode(p, detail::varint_zigzag(data));
138 typename std::make_unsigned<T>::type udata = 0;
139 size_t r = detail::varint_decode(p, udata);
140 data = detail::varint_zagzig(udata);
145 typename std::make_unsigned<T>::type udata = 0;
146 detail::varint_decode(p, udata);
147 return detail::varint_zagzig(udata);
150 operator T ()
const {
return data; }
151 T
get()
const {
return data; }
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.