ByteStream.h
Go to the documentation of this file.
1 /*--------------------------------------------------------------------------------------+
2 |
3 | Supplied under applicable software license agreement.
4 |
5 | Copyright (c) 2018 Bentley Systems, Incorporated. All rights reserved.
6 |
7 +---------------------------------------------------------------------------------------*/
8 #pragma once
9 
10 #include "Bentley.h"
11 #include "BeAssert.h"
12 #include <utility>
13 #include <string.h>
14 
16 
17 //=======================================================================================
20 // @bsiclass Bentley Systems
21 //=======================================================================================
22 struct ByteStream
23 {
24 private:
25  uint32_t m_size;
26  uint32_t m_allocSize;
27  uint8_t* m_data;
28  void swap(ByteStream& rhs) {std::swap(m_size,rhs.m_size); std::swap(m_allocSize,rhs.m_allocSize); std::swap(m_data,rhs.m_data);}
29 
30 public:
31  void Init() {m_size=m_allocSize=0; m_data=nullptr;}
32  ByteStream() {Init();}
33  explicit ByteStream(uint32_t size) {Init(); Resize(size);}
34  ByteStream(uint8_t const* data, uint32_t size) {Init(); SaveData(data, size);}
35  ByteStream(ByteStream const& other) {Init(); SaveData(other.m_data, other.m_size);}
36  ~ByteStream() {Clear();}
37  ByteStream(ByteStream&& rhs) : m_size(rhs.m_size), m_allocSize(rhs.m_allocSize), m_data(rhs.m_data) {rhs.m_size=rhs.m_allocSize=0; rhs.m_data=nullptr;}
38  ByteStream& operator=(ByteStream const& other) {if (this != &other) SaveData(other.m_data, other.m_size); return *this;}
39  ByteStream& operator=(ByteStream&& rhs) {ByteStream(std::move(rhs)).swap(*this); return *this;}
40 
43  uint32_t GetAllocSize() const {return m_allocSize;}
44  uint32_t GetSize() const {return m_size;}
45  uint8_t const* GetData() const {return m_data;}
46  uint8_t* GetDataP() const {return m_data;}
47  bool HasData() const {return 0 != m_size;}
48  void Clear() { if(nullptr != m_data) {free(m_data);m_data=nullptr;} m_size = m_allocSize = 0;}
49  uint8_t* ExtractData() {uint8_t* data = m_data; m_data = nullptr; m_size = m_allocSize = 0; return data;}
50 
53  void Reserve(uint32_t size) {if (size<=m_allocSize) return; m_data = (uint8_t*) realloc(m_data, size); m_allocSize = size;}
54 
57  void Resize(uint32_t newSize) {Reserve(newSize); m_size = newSize;}
58 
62  void SaveData(uint8_t const* data, uint32_t size) {m_size = 0; Append(data, size);}
63 
67  void Append(uint8_t const* data, uint32_t size)
68  {
69  if (data)
70  {
71  Reserve(m_size + size);
72  memcpy(m_data + m_size, data, size);
73  m_size += size;
74  }
75  }
76 
77  bool empty() const {return !HasData();}
78  size_t size() const {return GetSize();}
79  size_t capacity() const {return GetAllocSize();}
80  void reserve(size_t size) {Reserve(static_cast<uint32_t>(size));}
81  void resize(size_t newSize) {Resize(static_cast<uint32_t>(newSize));}
82  void clear() {Clear();}
83  uint8_t const* data() const {return GetData();}
84  uint8_t* data() {return GetDataP();}
85 
86  typedef uint8_t* iterator;
87  typedef uint8_t const* const_iterator;
88 
89  iterator begin() {return data();}
90  iterator end() {return data() + size();}
91  const_iterator begin() const {return data();}
92  const_iterator end() const {return data() + size();}
93  uint8_t const& operator[](size_t i) const {return data()[i];}
94  uint8_t& operator[](size_t i) {return data()[i];}
95 };
96 
const_iterator begin() const
Definition: ByteStream.h:91
uint8_t * iterator
Definition: ByteStream.h:86
ByteStream()
Definition: ByteStream.h:32
Open for both read and write.
void Resize(uint32_t newSize)
Resize the stream.
Definition: ByteStream.h:57
void Reserve(uint32_t size)
Reserve memory for this ByteStream.
Definition: ByteStream.h:53
ByteStream(uint32_t size)
Definition: ByteStream.h:33
Provides Bentley specific assert functions (Bentley/BeAssert.h).
void swap(basic_string< _CharT, _Traits, _Allocator > &__a, basic_string< _CharT, _Traits, _Allocator > &__b)
Definition: basic_string.h:1396
~ByteStream()
Definition: ByteStream.h:36
void Append(uint8_t const *data, uint32_t size)
Append a stream of byes to the current end of this ByteStream.
Definition: ByteStream.h:67
iterator end()
Definition: ByteStream.h:90
void reserve(size_t size)
Definition: ByteStream.h:80
#define BEGIN_BENTLEY_NAMESPACE
Definition: Bentley.r.h:24
uint8_t const & operator[](size_t i) const
Definition: ByteStream.h:93
uint8_t const * GetData() const
Get a const pointer to the ByteStream.
Definition: ByteStream.h:45
ByteStream & operator=(ByteStream const &other)
Definition: ByteStream.h:38
ByteStream(ByteStream const &other)
Definition: ByteStream.h:35
uint8_t const * const_iterator
Definition: ByteStream.h:87
A stream of bytes in a resizeable buffer.
Definition: ByteStream.h:22
ByteStream(ByteStream &&rhs)
Definition: ByteStream.h:37
Defines typedefs and constants that can be used across other namespaces. All Bentley-authored C++ sou...
uint8_t const * data() const
Definition: ByteStream.h:83
ByteStream & operator=(ByteStream &&rhs)
Definition: ByteStream.h:39
uint8_t * data()
Definition: ByteStream.h:84
bool HasData() const
return false if this ByteStream is empty.
Definition: ByteStream.h:47
size_t size() const
Definition: ByteStream.h:78
ByteStream(uint8_t const *data, uint32_t size)
Definition: ByteStream.h:34
uint8_t & operator[](size_t i)
Definition: ByteStream.h:94
uint8_t * ExtractData()
Definition: ByteStream.h:49
iterator begin()
Definition: ByteStream.h:89
uint32_t GetAllocSize() const
Get the size, in bytes, of the memory allocated for this ByteStream.
Definition: ByteStream.h:43
#define END_BENTLEY_NAMESPACE
Definition: Bentley.r.h:25
unsigned int uint32_t
Definition: Bentley.r.h:93
void resize(size_t newSize)
Definition: ByteStream.h:81
const_iterator end() const
Definition: ByteStream.h:92
void Init()
Definition: ByteStream.h:31
bool empty() const
Definition: ByteStream.h:77
size_type size() const
Definition: stdcxx/bstdmap.h:214
uint8_t * GetDataP() const
Get a writable pointer to the ByteStream.
Definition: ByteStream.h:46
void Clear()
Return this object to an empty/uninitialized state.
Definition: ByteStream.h:48
void clear()
Definition: ByteStream.h:82
unsigned char uint8_t
Definition: Bentley.r.h:89
uint32_t GetSize() const
Get the size in bytes of the current data in this ByteStream.
Definition: ByteStream.h:44
void SaveData(uint8_t const *data, uint32_t size)
Save a stream of bytes into this ByteStream.
Definition: ByteStream.h:62
size_t capacity() const
Definition: ByteStream.h:79

Copyright © 2017 Bentley Systems, Incorporated. All rights reserved.