Gnash  0.8.11dev
SimpleBuffer.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 
19 #ifndef GNASH_SIMPLEBUFFER_H
20 #define GNASH_SIMPLEBUFFER_H
21 
22 
23 #include <cstdint> // for std::uint8_t
24 #include <algorithm> // for std::copy
25 #include <memory>
26 #include <cassert>
27 
28 
29 namespace gnash {
30 
32 //
38 class SimpleBuffer {
39 
40 public:
41 
43  //
50  :
51  _size(0),
52  _capacity(capacity)
53  {
54  if ( _capacity )
55  {
56  _data.reset(new std::uint8_t[_capacity]);
57  }
58  }
59 
61  //
64  SimpleBuffer(size_t size, std::uint8_t* buffer)
65  : _size(size),
66  _capacity(size),
67  _data(buffer)
68  {
69  }
70 
72  SimpleBuffer(SimpleBuffer&&) = default;
73 
75  SimpleBuffer(const SimpleBuffer& b) = delete;
76  SimpleBuffer& operator= (const SimpleBuffer& b) = delete;
77 
79  SimpleBuffer& operator= (SimpleBuffer&&) = default;
80 
81 
83  bool empty() const { return _size==0; }
84 
86  size_t size() const { return _size; }
87 
89  size_t capacity() const { return _capacity; }
90 
92  std::uint8_t* data() { return _data.get(); }
93 
95  const std::uint8_t* data() const { return _data.get(); }
96 
98  void resize(size_t newSize)
99  {
100  reserve(newSize); // will set capacity
101  _size = newSize;
102  }
103 
105  void reserve(size_t newCapacity)
106  {
107  if ( _capacity >= newCapacity ) return;
108 
109  // TODO: use smalles power of 2 bigger then newCapacity
110  _capacity = std::max(newCapacity, _capacity*2);
111 
112  std::unique_ptr<std::uint8_t[]> tmp;
113  tmp.swap(_data);
114 
115  _data.reset(new std::uint8_t[_capacity]);
116 
117  if ( tmp.get() )
118  {
119  if ( _size ) std::copy(tmp.get(), tmp.get()+_size, _data.get());
120  }
121  }
122 
124  //
134  void append(const void* inData, size_t size)
135  {
136  const std::uint8_t* newData =
137  reinterpret_cast<const std::uint8_t*>(inData);
138  size_t curSize = _size;
139  resize(curSize+size);
140  std::copy(newData, newData+size, _data.get()+curSize);
141  assert(_size == curSize+size);
142  }
143 
145  //
151  void appendByte(const std::uint8_t b)
152  {
153  resize(_size + 1);
154  _data[_size - 1] = b;
155  }
156 
158  //
165  void appendNetworkShort(const std::uint16_t s)
166  {
167  resize(_size + 2);
168  _data[_size - 2] = s >> 8;
169  _data[_size - 1] = s & 0xff;
170  }
171 
173  //
180  void appendNetworkLong(const std::uint32_t l)
181  {
182  resize(_size + 4);
183  _data[_size - 4] = l >> 24;
184  _data[_size - 3] = (l >> 16) & 0xff;
185  _data[_size - 2] = (l >> 8) & 0xff;
186  _data[_size - 1] = l & 0xff;
187  }
188 
190  //
197  void append(const SimpleBuffer& buf)
198  {
199  size_t incomingDataSize = buf.size();
200  const std::uint8_t* incomingData = buf.data();
201  append(incomingData, incomingDataSize);
202  }
203 
204 private:
205  size_t _size;
206  size_t _capacity;
207 
208  std::unique_ptr<std::uint8_t[]> _data;
209 };
210 
211 
212 } // namespace gnash
213 
214 #endif // GNASH_SIMPLEBUFFER_H
void append(const SimpleBuffer &buf)
Append data to the buffer.
Definition: SimpleBuffer.h:197
void resize(size_t newSize)
Resize the buffer.
Definition: SimpleBuffer.h:98
SWFStream & s
Definition: DefineBitsTag.cpp:71
void appendByte(const std::uint8_t b)
Append a byte to the buffer.
Definition: SimpleBuffer.h:151
void append(const void *inData, size_t size)
Append data to the buffer.
Definition: SimpleBuffer.h:134
Definition: GnashKey.h:158
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
SimpleBuffer(size_t capacity=0)
Construct a SimpleBuffer with an optional initial capacity.
Definition: SimpleBuffer.h:49
std::uint8_t * data()
Get a pointer to start of data. May be NULL if size==0.
Definition: SimpleBuffer.h:92
const std::uint8_t * data() const
Get a pointer to start of data. May be NULL if size==0.
Definition: SimpleBuffer.h:95
void appendNetworkShort(const std::uint16_t s)
Append 2 bytes to the buffer.
Definition: SimpleBuffer.h:165
SimpleBuffer & operator=(const SimpleBuffer &b)=delete
Definition: GnashKey.h:148
bool empty() const
Return true if buffer is empty.
Definition: SimpleBuffer.h:83
void appendNetworkLong(const std::uint32_t l)
Append 4 bytes to the buffer.
Definition: SimpleBuffer.h:180
size_t capacity() const
Return capacity of the buffer.
Definition: SimpleBuffer.h:89
A simple buffer of bytes.
Definition: SimpleBuffer.h:38
SimpleBuffer(size_t size, std::uint8_t *buffer)
Construct a SimpleBuffer by taking ownership of an existing buffer.
Definition: SimpleBuffer.h:64
size_t size() const
Return size of the buffer.
Definition: SimpleBuffer.h:86
void reserve(size_t newCapacity)
Ensure at least 'newCapacity' bytes are allocated for this buffer.
Definition: SimpleBuffer.h:105