LibNRG  0.0.1
Networking for Real-time Games library
 All Classes Files Functions Variables Friends
nrg_bit_io.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_BIT_IO_H
26 #define NRG_BIT_IO_H
27 #include "nrg_core.h"
28 #include "nrg_packet.h"
29 
30 namespace nrg {
31 
32 namespace detail {
33  static const size_t MAX_BYTE_SHIFTS = 7;
34 }
35 
37 class BitWriter {
38 public:
40  BitWriter(Packet& p) : bits(0), count(0), p(p){}
41 
43  void write(bool b){
44  b ? write1() : write0();
45  }
46 
48  void write1(){
49  bits |= 1 << (detail::MAX_BYTE_SHIFTS - count);
50  write0();
51  }
52 
54  void write0(){
55  if(++count > detail::MAX_BYTE_SHIFTS) flush();
56  }
57 
59  template<class T>
60  void writeFunc(int sz, const T& fn){
61  for(int i = 0; i < sz; ++i){
62  write(fn(i));
63  }
64  }
65 
67  void flush(void){
68  if(!count) return;
69  p.write8(bits);
70  clear();
71  }
72 
74  void clear(){
75  bits = count = 0;
76  }
77 
80  flush();
81  }
82 private:
83  uint8_t bits, count;
84  Packet& p;
85 };
86 
88 class BitReader {
89 public:
91  BitReader(Packet& p) : bits(0), count(0), p(p){}
92 
94  bool read(void){
95  if(count == 0) p.read8(bits);
96  bool b = bits & (1 << (detail::MAX_BYTE_SHIFTS - count));
97  count = (count + 1) & detail::MAX_BYTE_SHIFTS;
98  return b;
99  }
100 
102  template<class T>
103  void readFunc(int sz, const T& fn){
104  for(int i = 0; i < sz; ++i){
105  fn(i, read());
106  }
107  }
108 private:
109  uint8_t bits, count;
110  Packet& p;
111 };
112 
113 }
114 
115 #endif
void clear()
Resets the internal set of bits that have not yet been written.
Definition: nrg_bit_io.h:74
Class for storing data to be sent / received across the network.
Definition: nrg_packet.h:58
PacketWritable & write8(const uint8_t &v)
Write a uint8_t, automatically converting endianness.
void flush(void)
Finishes writing bits to the packet, and resets the internal state.
Definition: nrg_bit_io.h:67
void writeFunc(int sz, const T &fn)
Calls BitWriter::write using the bool return value from the function fn sz times. ...
Definition: nrg_bit_io.h:60
void write1()
Write a 1 bit.
Definition: nrg_bit_io.h:48
void write(bool b)
Write a 1 or 0 if b is true or false respectively.
Definition: nrg_bit_io.h:43
~BitWriter()
Destructor, which will call BitWriter::flush.
Definition: nrg_bit_io.h:79
BitReader(Packet &p)
Constructs a BitReader that will read from the Packet p.
Definition: nrg_bit_io.h:91
void write0()
Write a 0 bit.
Definition: nrg_bit_io.h:54
Reads a stream of bytes from a packet more easily.
Definition: nrg_bit_io.h:88
Common defines and includes used by all the other nrg header files.
Writes a stream of bytes to a packet more easily.
Definition: nrg_bit_io.h:37
void readFunc(int sz, const T &fn)
Calls BitReader::read sz times, calling fn with each result.
Definition: nrg_bit_io.h:103
bool read(void)
Reads a bit from the internal state and returns a bool representation of it.
Definition: nrg_bit_io.h:94
PacketReadable & read8(uint8_t &v)
Read a uint8_t, automatically converting endianness.
BitWriter(Packet &p)
Constructor a BitWriter that will read from the Packet p.
Definition: nrg_bit_io.h:40