35 #include <type_traits>
45 char const*
what()
const throw()
override {
return msg_.c_str(); }
48 std::string &
message() throw() {
return msg_; }
73 static size_t const DEFAULT_SIZE = 0x1000;
74 static uint8 const InitialBitPos = 8;
77 ByteBuffer() : _rpos(0), _wpos(0), _bitpos(InitialBitPos), _curbitval(0)
79 _storage.reserve(DEFAULT_SIZE);
82 ByteBuffer(
size_t reserve) : _rpos(0), _wpos(0), _bitpos(InitialBitPos), _curbitval(0)
84 _storage.reserve(reserve);
88 _bitpos(buf._bitpos), _curbitval(buf._curbitval), _storage(buf.Move()) { }
91 _bitpos(right._bitpos), _curbitval(right._curbitval), _storage(right._storage) { }
95 std::vector<uint8>&&
Move()
99 _bitpos = InitialBitPos;
101 return std::move(_storage);
124 _bitpos = right._bitpos;
125 _curbitval = right._curbitval;
126 _storage = right.Move();
138 _bitpos = InitialBitPos;
174 _curbitval |= (1 << (_bitpos));
191 _curbitval = read<uint8>();
195 return ((_curbitval >> (7-_bitpos)) & 1) != 0;
200 for (
int32 i = bits - 1; i >= 0; --i)
201 WriteBit((value >> i) & 1);
207 for (
int32 i = bits - 1; i >= 0; --i)
224 append<uint8>(b ^ 1);
227 template <
typename T>
void put(
size_t pos, T
value)
231 put(pos, (
uint8 *)&value,
sizeof(value));
251 if (pos + bitCount > size() * 8)
254 for (
uint32 i = 0; i < bitCount; ++i)
256 size_t wp = (pos + i) / 8;
257 size_t bit = (pos + i) % 8;
258 if ((value >> (bitCount - i - 1)) & 1)
259 _storage[wp] |= 1 << (7 - bit);
261 _storage[wp] &= ~(1 << (7 - bit));
267 append<uint8>(
value);
273 append<uint16>(
value);
279 append<uint32>(
value);
285 append<uint64>(
value);
298 append<int16>(
value);
304 append<int32>(
value);
310 append<int64>(
value);
317 append<float>(
value);
323 append<double>(
value);
329 if (
size_t len = value.length())
337 if (
size_t len = (str ? strlen(str) : 0))
345 value = read<char>() > 0 ?
true :
false;
351 value = read<uint8>();
357 value = read<uint16>();
363 value = read<uint32>();
369 value = read<uint64>();
376 value = read<int8>();
382 value = read<int16>();
388 value = read<int32>();
394 value = read<int64>();
400 value = read<float>();
401 if (!std::isfinite(value))
408 value = read<double>();
409 if (!std::isfinite(value))
417 while (rpos() < size())
419 char c = read<char>();
431 return _storage[pos];
438 return _storage[pos];
441 size_t rpos()
const {
return _rpos; }
454 size_t wpos()
const {
return _wpos; }
463 size_t bitwpos()
const {
return _wpos * 8 + 8 - _bitpos; }
468 _bitpos = 8 - (newPos % 8);
469 return _wpos * 8 + 8 - _bitpos;
477 if (_rpos + skip > size())
487 T r = read<T>(_rpos);
492 template <
typename T> T
read(
size_t pos)
const
494 if (pos +
sizeof(T) > size())
496 T val = *((T
const*)&_storage[pos]);
503 if (_rpos + len > size())
507 std::memcpy(dest, &_storage[_rpos], len);
514 ReadPackedUInt64(read<uint8>(), guid);
519 for (
uint32 i = 0; i < 8; ++i)
520 if (mask & (
uint8(1) << i))
521 value |= (
uint64(read<uint8>()) << (i * 8));
526 if (_rpos + length > size())
531 return std::string();
533 std::string str((
char const*)&_storage[_rpos], length);
542 if (
size_t len = str.length())
554 uint32 packedDate = read<uint32>();
557 lt.tm_min = packedDate & 0x3F;
558 lt.tm_hour = (packedDate >> 6) & 0x1F;
560 lt.tm_mday = ((packedDate >> 14) & 0x3F) + 1;
561 lt.tm_mon = (packedDate >> 20) & 0xF;
562 lt.tm_year = ((packedDate >> 24) & 0x1F) + 100;
564 return uint32(mktime(<));
569 time = ReadPackedTime();
575 if (_storage.empty())
577 return _storage.data();
582 if (_storage.empty())
584 return _storage.data();
587 size_t size()
const {
return _storage.size(); }
588 bool empty()
const {
return _storage.empty(); }
592 _storage.resize(newsize, 0);
599 if (ressize > size())
600 _storage.reserve(ressize);
608 template<
class T>
void append(
const T *src,
size_t cnt)
610 return append((
const uint8 *)src, cnt *
sizeof(T));
621 ASSERT(size() < 10000000);
625 if (_storage.size() < _wpos + cnt)
626 _storage.resize(_wpos + cnt);
627 std::memcpy(&_storage[_wpos], src, cnt);
641 packed |= ((int)(x / 0.25f) & 0x7FF);
642 packed |= ((int)(y / 0.25f) & 0x7FF) << 11;
643 packed |= ((int)(z / 0.25f) & 0x3FF) << 22;
651 *
this <<
uint8(mask);
654 if (
size_t packedSize = PackUInt64(guid, &mask, packed))
655 append(packed, packedSize);
657 put<uint8>(pos, mask);
662 size_t resultSize = 0;
664 memset(result, 0, 8);
666 for (
uint8 i = 0; value != 0; ++i)
670 *mask |=
uint8(1 << i);
671 result[resultSize++] =
uint8(value & 0xFF);
684 append<uint32>((lt.tm_year - 100) << 24 | lt.tm_mon << 20 | (lt.tm_mday - 1) << 14 | lt.tm_wday << 11 | lt.tm_hour << 6 | lt.tm_min);
689 if (pos + cnt > size())
695 std::memcpy(&_storage[pos], src, cnt);
698 void print_storage()
const;
700 void textlike()
const;
702 void hexlike()
const;
710 template <
typename T>
711 inline ByteBuffer &operator<<(ByteBuffer &b, std::vector<T> v)
714 for (
typename std::vector<T>::iterator i = v.begin(); i != v.end(); ++i)
721 template <
typename T>
736 template <
typename T>
740 for (
typename std::list<T>::iterator i = v.begin(); i != v.end(); ++i)
747 template <
typename T>
762 template <
typename K,
typename V>
763 inline ByteBuffer &operator<<(ByteBuffer &b, std::map<K, V> &m)
766 for (
typename std::map<K, V>::iterator i = m.begin(); i != m.end(); ++i)
768 b << i->first << i->second;
773 template <
typename K,
typename V>
784 m.insert(make_pair(k, v));
790 template<>
inline std::string ByteBuffer::read<std::string>()
798 inline void ByteBuffer::read_skip<char*>()
805 inline void ByteBuffer::read_skip<char const*>()
811 inline void ByteBuffer::read_skip<std::string>()
ByteBuffer & operator<<(int32 value)
Definition: ByteBuffer.h:302
ByteBuffer & operator<<(int64 value)
Definition: ByteBuffer.h:308
ByteBuffer & operator=(ByteBuffer const &right)
Definition: ByteBuffer.h:104
uint8 * contents()
Definition: ByteBuffer.h:573
void read_skip()
Definition: ByteBuffer.h:473
Definition: ByteBuffer.h:70
int8_t int8
Definition: Define.h:148
ByteBuffer & operator<<(uint32 value)
Definition: ByteBuffer.h:277
ByteBuffer & operator>>(uint64 &value)
Definition: ByteBuffer.h:367
void append(const T *src, size_t cnt)
Definition: ByteBuffer.h:608
int64_t int64
Definition: Define.h:145
void ReadPackedUInt64(uint64 &guid)
Definition: ByteBuffer.h:511
ByteBuffer & operator>>(uint32 &value)
Definition: ByteBuffer.h:361
void read_skip(size_t skip)
Definition: ByteBuffer.h:475
ByteBuffer & operator<<(const char *str)
Definition: ByteBuffer.h:335
Definition: ByteBuffer.h:62
uint32 ReadBits(int32 bits)
Definition: ByteBuffer.h:204
void resize(size_t newsize)
Definition: ByteBuffer.h:590
void EndianConvert(T &val)
Definition: ByteConverter.h:48
std::string & message()
Definition: ByteBuffer.h:48
char const * what() const override
Definition: ByteBuffer.h:45
size_t wpos(size_t wpos_)
Definition: ByteBuffer.h:456
void ResetBitPos()
Definition: ByteBuffer.h:161
ByteBuffer(ByteBuffer const &right)
Definition: ByteBuffer.h:90
ByteBuffer & operator>>(int32 &value)
Definition: ByteBuffer.h:386
ByteBuffer & operator<<(int16 value)
Definition: ByteBuffer.h:296
ByteBuffer & operator<<(float value)
Definition: ByteBuffer.h:315
void FlushBits()
Definition: ByteBuffer.h:150
uint64_t uint64
Definition: g3dmath.h:170
ByteBuffer()
Definition: ByteBuffer.h:77
ByteBuffer & operator>>(int16 &value)
Definition: ByteBuffer.h:380
size_t bitwpos() const
Returns position of last written bit.
Definition: ByteBuffer.h:463
uint8 _curbitval
Definition: ByteBuffer.h:706
std::vector< uint8 > _storage
Definition: ByteBuffer.h:707
bool WriteBit(uint32 bit)
Definition: ByteBuffer.h:170
ByteBuffer(size_t reserve)
Definition: ByteBuffer.h:82
void appendPackXYZ(float x, float y, float z)
Definition: ByteBuffer.h:638
uint8 const & operator[](size_t const pos) const
Definition: ByteBuffer.h:434
void put(size_t pos, const uint8 *src, size_t cnt)
Definition: ByteBuffer.h:687
Definition: ByteBuffer.h:54
uint32 ReadPackedTime()
Definition: ByteBuffer.h:552
ByteBuffer & operator>>(ByteBuffer &b, std::vector< T > &v)
Definition: ByteBuffer.h:722
void ReadPackedUInt64(uint8 mask, uint64 &value)
Definition: ByteBuffer.h:517
ByteBuffer & operator=(ByteBuffer &&right)
Definition: ByteBuffer.h:118
std::string msg_
Definition: ByteBuffer.h:51
size_t wpos() const
Definition: ByteBuffer.h:454
void read(uint8 *dest, size_t len)
Definition: ByteBuffer.h:501
void PutBits(size_t pos, T value, uint32 bitCount)
Definition: ByteBuffer.h:246
ByteBuffer & operator>>(std::string &value)
Definition: ByteBuffer.h:414
static size_t PackUInt64(uint64 value, uint8 *mask, uint8 *result)
Definition: ByteBuffer.h:660
std::string ReadString(uint32 length)
Definition: ByteBuffer.h:524
void WriteString(std::string const &str)
Definition: ByteBuffer.h:540
ByteBuffer & operator>>(float &value)
Definition: ByteBuffer.h:398
ByteBuffer & ReadPackedTime(uint32 &time)
Definition: ByteBuffer.h:567
bool empty() const
Definition: ByteBuffer.h:588
size_t size() const
Definition: ByteBuffer.h:587
void AppendPackedUInt64(uint64 guid)
Definition: ByteBuffer.h:647
G3D::int16 z
Definition: Vector3int16.h:46
void put(size_t pos, T value)
Definition: ByteBuffer.h:227
int32_t int32
Definition: Define.h:146
uint32_t uint32
Definition: Define.h:150
uint64_t uint64
Definition: Define.h:149
ByteBuffer & operator<<(uint64 value)
Definition: ByteBuffer.h:283
G3D::int16 y
Definition: Vector2int16.h:38
#define TC_SHARED_API
Definition: Define.h:128
uint16_t uint16
Definition: Define.h:151
~ByteBufferPositionException()
Definition: ByteBuffer.h:59
uint16 bits() const
Returns the underlying bits in this representation. Equivalent to:
Definition: unorm16.h:89
void rfinish()
Definition: ByteBuffer.h:449
void AppendPackedTime(time_t time)
Definition: ByteBuffer.h:680
ByteBuffer & operator>>(uint8 &value)
Definition: ByteBuffer.h:349
uint8 const * contents() const
Definition: ByteBuffer.h:580
void WriteByteSeq(uint8 b)
Definition: ByteBuffer.h:221
void reserve(size_t ressize)
Definition: ByteBuffer.h:597
ByteBuffer(ByteBuffer &&buf)
Definition: ByteBuffer.h:87
float length(float v)
Definition: vectorMath.h:208
void append(T value)
Definition: ByteBuffer.h:143
TC_COMMON_API struct tm * localtime_r(const time_t *time, struct tm *result)
uint8_t uint8
Definition: g3dmath.h:164
T read(size_t pos) const
Definition: ByteBuffer.h:492
ByteBuffer & operator>>(bool &value)
Definition: ByteBuffer.h:343
size_t bitwpos(size_t newPos)
Definition: ByteBuffer.h:465
uint8 & operator[](size_t const pos)
Definition: ByteBuffer.h:427
std::vector< uint8 > && Move()
Definition: ByteBuffer.h:95
size_t _bitpos
Definition: ByteBuffer.h:705
~ByteBufferException()
Definition: ByteBuffer.h:43
void WriteString(char const *str, size_t len)
Definition: ByteBuffer.h:546
Definition: ByteBuffer.h:40
ByteBuffer & operator<<(double value)
Definition: ByteBuffer.h:321
uint8_t uint8
Definition: Define.h:152
void ReadByteSeq(uint8 &b)
Definition: ByteBuffer.h:215
#define ASSERT
Definition: Errors.h:55
void append(const uint8 *src, size_t cnt)
Definition: ByteBuffer.h:613
virtual ~ByteBuffer()
Definition: ByteBuffer.h:132
ByteBuffer & operator>>(int8 &value)
Definition: ByteBuffer.h:374
const FieldDescriptor value
Definition: descriptor.h:1522
int16_t int16
Definition: Define.h:147
uint32_t uint32
Definition: g3dmath.h:168
G3D::int16 x
Definition: Vector2int16.h:37
ByteBuffer & operator<<(uint8 value)
Definition: ByteBuffer.h:265
size_t rpos() const
Definition: ByteBuffer.h:441
ByteBuffer & operator<<(uint16 value)
Definition: ByteBuffer.h:271
~ByteBufferSourceException()
Definition: ByteBuffer.h:67
ByteBuffer & operator>>(uint16 &value)
Definition: ByteBuffer.h:355
ByteBuffer & operator<<(int8 value)
Definition: ByteBuffer.h:290
bool ReadBit()
Definition: ByteBuffer.h:186
void clear()
Definition: ByteBuffer.h:134
size_t _wpos
Definition: ByteBuffer.h:705
ByteBuffer & operator<<(const std::string &value)
Definition: ByteBuffer.h:327
size_t rpos(size_t rpos_)
Definition: ByteBuffer.h:443
void append(const ByteBuffer &buffer)
Definition: ByteBuffer.h:631
ByteBuffer & operator>>(int64 &value)
Definition: ByteBuffer.h:392
Definition: MessageBuffer.h:24
ByteBuffer & operator>>(double &value)
Definition: ByteBuffer.h:406
void WriteBits(T value, int32 bits)
Definition: ByteBuffer.h:198
T read()
Definition: ByteBuffer.h:484
octet_iterator append(uint32_t cp, octet_iterator result)
The library API - functions intended to be called by the users.
Definition: checked.h:73
void append(const char *src, size_t cnt)
Definition: ByteBuffer.h:603
size_t _rpos
Definition: ByteBuffer.h:705