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  SimpleBuffer(SimpleBuffer&&) = default;
62 
64  SimpleBuffer(const SimpleBuffer& b) = delete;
65  SimpleBuffer& operator= (const SimpleBuffer& b) = delete;
66 
68  SimpleBuffer& operator= (SimpleBuffer&&) = default;
69 
70 
72  bool empty() const { return _size==0; }
73 
75  size_t size() const { return _size; }
76 
78  size_t capacity() const { return _capacity; }
79 
81  std::uint8_t* data() { return _data.get(); }
82 
84  const std::uint8_t* data() const { return _data.get(); }
85 
87  void resize(size_t newSize)
88  {
89  reserve(newSize); // will set capacity
90  _size = newSize;
91  }
92 
94  void reserve(size_t newCapacity)
95  {
96  if ( _capacity >= newCapacity ) return;
97 
98  // TODO: use smalles power of 2 bigger then newCapacity
99  _capacity = std::max(newCapacity, _capacity*2);
100 
101  std::unique_ptr<std::uint8_t[]> tmp;
102  tmp.swap(_data);
103 
104  _data.reset(new std::uint8_t[_capacity]);
105 
106  if ( tmp.get() )
107  {
108  if ( _size ) std::copy(tmp.get(), tmp.get()+_size, _data.get());
109  }
110  }
111 
113  //
123  void append(const void* inData, size_t size)
124  {
125  const std::uint8_t* newData =
126  reinterpret_cast<const std::uint8_t*>(inData);
127  size_t curSize = _size;
128  resize(curSize+size);
129  std::copy(newData, newData+size, _data.get()+curSize);
130  assert(_size == curSize+size);
131  }
132 
134  //
140  void appendByte(const std::uint8_t b)
141  {
142  resize(_size + 1);
143  _data[_size - 1] = b;
144  }
145 
147  //
154  void appendNetworkShort(const std::uint16_t s)
155  {
156  resize(_size + 2);
157  _data[_size - 2] = s >> 8;
158  _data[_size - 1] = s & 0xff;
159  }
160 
162  //
169  void appendNetworkLong(const std::uint32_t l)
170  {
171  resize(_size + 4);
172  _data[_size - 4] = l >> 24;
173  _data[_size - 3] = (l >> 16) & 0xff;
174  _data[_size - 2] = (l >> 8) & 0xff;
175  _data[_size - 1] = l & 0xff;
176  }
177 
179  //
186  void append(const SimpleBuffer& buf)
187  {
188  size_t incomingDataSize = buf.size();
189  const std::uint8_t* incomingData = buf.data();
190  append(incomingData, incomingDataSize);
191  }
192 
193 private:
194  size_t _size;
195  size_t _capacity;
196 
197  std::unique_ptr<std::uint8_t[]> _data;
198 };
199 
200 
201 } // namespace gnash
202 
203 #endif // GNASH_SIMPLEBUFFER_H
void append(const SimpleBuffer &buf)
Append data to the buffer.
Definition: SimpleBuffer.h:186
void resize(size_t newSize)
Resize the buffer.
Definition: SimpleBuffer.h:87
size_t capacity() const
Return capacity of the buffer.
Definition: SimpleBuffer.h:78
void appendByte(const std::uint8_t b)
Append a byte to the buffer.
Definition: SimpleBuffer.h:140
const std::uint8_t * data() const
Get a pointer to start of data. May be NULL if size==0.
Definition: SimpleBuffer.h:84
void append(const void *inData, size_t size)
Append data to the buffer.
Definition: SimpleBuffer.h:123
Definition: GnashKey.h:158
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
bool empty() const
Return true if buffer is empty.
Definition: SimpleBuffer.h:72
SimpleBuffer(size_t capacity=0)
Construct a SimpleBuffer with an optional initial capacity.
Definition: SimpleBuffer.h:49
size_t size() const
Return size of the buffer.
Definition: SimpleBuffer.h:75
std::uint8_t * data()
Get a pointer to start of data. May be NULL if size==0.
Definition: SimpleBuffer.h:81
void appendNetworkShort(const std::uint16_t s)
Append 2 bytes to the buffer.
Definition: SimpleBuffer.h:154
SimpleBuffer & operator=(const SimpleBuffer &b)=delete
Definition: GnashKey.h:148
void appendNetworkLong(const std::uint32_t l)
Append 4 bytes to the buffer.
Definition: SimpleBuffer.h:169
A simple buffer of bytes.
Definition: SimpleBuffer.h:38
Definition: GnashKey.h:165
void reserve(size_t newCapacity)
Ensure at least &#39;newCapacity&#39; bytes are allocated for this buffer.
Definition: SimpleBuffer.h:94