SEGS  0.6.1-develop [d2cac982]
Super Entity Game Server
BitStream.h
Go to the documentation of this file.
1 /*
2  * SEGS - Super Entity Game Server
3  * http://www.segs.io/
4  * Copyright (c) 2006 - 2019 SEGS Team (see AUTHORS.md)
5  * This software is licensed under the terms of the 3-clause BSD License. See LICENSE.md for details.
6  */
7 
8 /************************************************************************
9 Class: BitStream
10 Author: Darawk,nemerle
11 Description: The BitStream class allows it's user to manipulate data in
12  terms of individual bits, thus allowing said data to achieve
13  much greater levels of density. It abstracts the nuts and
14  bolts of this functionality away from it's user.
15 ************************************************************************/
16 
17 #pragma once
18 
19 #include <cereal/macros.hpp>
20 
21 #include "Buffer.h"
22 
23 #include <cstdint>
24 #include <utility>
25 //#define CUSTOM_CLIENT_CODE
26 #ifdef CUSTOM_CLIENT_CODE
27 #define PUTDEBUG(x) bs.StoreString(x);
28 #else
29 #define PUTDEBUG(x) ;
30 #endif
31 
32 class QString;
33 class QByteArray;
34 class BitStream : public GrowingBuffer
35 {
36  template <class Archive>
37  friend void CEREAL_SAVE_FUNCTION_NAME( Archive & ar, const BitStream &buf );
38  template <class Archive>
39  friend void CEREAL_LOAD_FUNCTION_NAME( Archive & ar, BitStream &buf );
40 
41 public:
42 
43 explicit BitStream(size_t size);
44  BitStream(uint8_t *from,size_t bitsize);
45  BitStream(const BitStream &bs);
46  BitStream(BitStream &&bs) noexcept : GrowingBuffer(std::move(bs)) {
47  m_read_bit_off = bs.m_read_bit_off;
48  m_write_bit_off = bs.m_write_bit_off;
49  m_byteAligned = bs.m_byteAligned;
50  }
51  BitStream &operator=(const BitStream &bs);
52  ~BitStream();
53 
54  void StoreBits(uint32_t nBits, uint32_t dataBits);
55  void uStoreBits(uint32_t nBits, uint32_t dataBits);
56  void StoreFloat(float val);
57  void StorePackedBits(uint32_t nBits, uint32_t dataBits);
59  {
60  //TODO: optimize this to partial memcopy in special cases ?
61  uint32_t bits_to_store =src.GetReadableBits();
62  while(bits_to_store>32)
63  {
64  StoreBits(32,src.uGetBits(32));
65  bits_to_store-=32;
66  }
67  StoreBits(bits_to_store,src.uGetBits(bits_to_store));
68  }
69  void StoreBitArray(const uint8_t *array,size_t nBits);
70  void StoreString(const char *str);
71  void StoreString(const QByteArray &str);
72  void StoreString(const QString &str);
73  int32_t GetBits(uint32_t nBits);
74  int32_t uGetBits(uint32_t nBits);
75  int32_t GetPackedBits(uint32_t minbits);
76  void GetBitArray(uint8_t *array, uint32_t nBits);
77  void GetString(QString &str);
78  float GetFloat();
79  int64_t Get64Bits();
80  uint32_t GetWritableBits() const { int64_t bitsleft = int64_t(GetAvailSize() << 3) - m_write_bit_off; return uint32_t(bitsleft > 0 ? bitsleft : 0); }
81  uint32_t GetReadableBits() const { return (GetReadableDataSize()<<3)+(m_write_bit_off-m_read_bit_off);}
82  uint32_t GetAvailSize() const;
83  bool IsByteAligned() const { return m_byteAligned;}
84 
85  void SetReadPos(uint32_t pos) { m_read_off = pos >> 3; m_read_bit_off = uint8_t(pos & 0x7);}
86  uint32_t GetReadPos() const { return (m_read_off<<3) + m_read_bit_off;}
87  void SetWritePos(uint32_t pos) { m_write_off = pos >> 3; m_write_bit_off = uint8_t(pos & 0x7);}
88  uint32_t GetWritePos() const { return (m_write_off<<3) + m_write_bit_off;}
89 
90  void UseByteAlignedMode(bool toggle);
91  void ByteAlign(bool read_part=true,bool write_part=true);
92  void ResetReading() { m_read_bit_off=0; m_read_off=0; }
93  void ResetOffsets();
94 
95  void CompressAndStoreString(const char *str);
96  void GetAndDecompressString(QString &tgt);
97 
98 private:
99  uint8_t m_read_bit_off;
100  uint8_t m_write_bit_off;
101  bool m_byteAligned ;
102 };
void GetBitArray(uint8_t *array, uint32_t nBits)
Definition: BitStream.cpp:321
void StoreBits(uint32_t nBits, uint32_t dataBits)
Definition: BitStream.cpp:114
void SetWritePos(uint32_t pos)
Definition: BitStream.h:87
void SetReadPos(uint32_t pos)
Definition: BitStream.h:85
int32_t uGetBits(uint32_t nBits)
Definition: BitStream.cpp:274
void GetAndDecompressString(QString &tgt)
Definition: BitStream.cpp:454
void CompressAndStoreString(const char *str)
Definition: BitStream.cpp:436
void ResetOffsets()
Definition: BitStream.cpp:408
void ResetReading()
Definition: BitStream.h:92
int32_t GetPackedBits(uint32_t minbits)
Definition: BitStream.cpp:290
void UseByteAlignedMode(bool toggle)
Definition: BitStream.cpp:414
~BitStream()
Definition: BitStream.cpp:96
Definition: BitStream.h:34
void appendBitStream(BitStream &src)
Definition: BitStream.h:58
void StorePackedBits(uint32_t nBits, uint32_t dataBits)
Definition: BitStream.cpp:161
uint32_t GetWritableBits() const
Definition: BitStream.h:80
int32_t GetBits(uint32_t nBits)
Definition: BitStream.cpp:262
uint32_t GetReadPos() const
Definition: BitStream.h:86
uint32_t GetWritePos() const
Definition: BitStream.h:88
Definition: Buffer.h:17
void StoreString(const char *str)
Definition: BitStream.cpp:204
uint32_t GetReadableDataSize() const
Definition: Buffer.h:97
BitStream(BitStream &&bs) noexcept
Definition: BitStream.h:46
BitStream & operator=(const BitStream &bs)
Definition: BitStream.cpp:72
uint32_t m_write_off
Definition: Buffer.h:115
void GetString(QString &str)
Retrieves a null-terminated C-style string from the bit stream.
Definition: BitStream.cpp:331
GrowingBuffer(const GrowingBuffer &)
Definition: Buffer.cpp:56
bool IsByteAligned() const
Definition: BitStream.h:83
int64_t Get64Bits()
Read upto 8 bytes from input stream and return them as 64bit integer.
Definition: BitStream.cpp:359
void StoreBitArray(const uint8_t *array, size_t nBits)
BitStream::StoreBitArray Stores an array of bits in the bit stream buffer. The main difference betwee...
Definition: BitStream.cpp:183
uint32_t GetAvailSize() const
Definition: BitStream.cpp:375
void ByteAlign(bool read_part=true, bool write_part=true)
Definition: BitStream.cpp:421
friend void CEREAL_LOAD_FUNCTION_NAME(Archive &ar, BitStream &buf)
Serialization for non-arithmetic vector types.
Definition: serialization_types.cpp:16
void uStoreBits(uint32_t nBits, uint32_t dataBits)
Definition: BitStream.cpp:138
uint32_t m_read_off
Definition: Buffer.h:116
friend void CEREAL_SAVE_FUNCTION_NAME(Archive &ar, const BitStream &buf)
Definition: serialization_types.cpp:5
BitStream(size_t size)
Definition: BitStream.cpp:39
void StoreFloat(float val)
Definition: BitStream.cpp:398
float GetFloat()
Definition: BitStream.cpp:385
uint32_t GetReadableBits() const
Definition: BitStream.h:81