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 <boost/cstdint.hpp> // for boost::uint8_t
24 #include <algorithm> // for std::copy
25 #include <boost/scoped_array.hpp>
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 boost::uint8_t[_capacity]);
57  }
58  }
59 
61  //
67  :
68  _size(b._size),
69  _capacity(b._size)
70  {
71  if ( _size )
72  {
73  _data.reset(new boost::uint8_t[_size]);
74  std::copy(b.data(), b.data()+b.size(), _data.get());
75  }
76  }
77 
79  //
83  {
84  if ( this != &b ) // don't waste time on self-assignment
85  {
86  resize(0); // shouldn't deallocate memory
87  append(b);
88  }
89  return *this;
90  }
91 
93  bool empty() const { return _size==0; }
94 
96  size_t size() const { return _size; }
97 
99  size_t capacity() const { return _capacity; }
100 
102  boost::uint8_t* data() { return _data.get(); }
103 
105  const boost::uint8_t* data() const { return _data.get(); }
106 
108  void resize(size_t newSize)
109  {
110  reserve(newSize); // will set capacity
111  _size = newSize;
112  }
113 
115  void reserve(size_t newCapacity)
116  {
117  if ( _capacity >= newCapacity ) return;
118 
119  // TODO: use smalles power of 2 bigger then newCapacity
120  _capacity = std::max(newCapacity, _capacity*2);
121 
122  boost::scoped_array<boost::uint8_t> tmp;
123  tmp.swap(_data);
124 
125  _data.reset(new boost::uint8_t[_capacity]);
126 
127  if ( tmp.get() )
128  {
129  if ( _size ) std::copy(tmp.get(), tmp.get()+_size, _data.get());
130  }
131  }
132 
134  //
144  void append(const void* inData, size_t size)
145  {
146  const boost::uint8_t* newData =
147  reinterpret_cast<const boost::uint8_t*>(inData);
148  size_t curSize = _size;
149  resize(curSize+size);
150  std::copy(newData, newData+size, _data.get()+curSize);
151  assert(_size == curSize+size);
152  }
153 
155  //
161  void appendByte(const boost::uint8_t b)
162  {
163  resize(_size + 1);
164  _data[_size - 1] = b;
165  }
166 
168  //
175  void appendNetworkShort(const boost::uint16_t s)
176  {
177  resize(_size + 2);
178  _data[_size - 2] = s >> 8;
179  _data[_size - 1] = s & 0xff;
180  }
181 
183  //
190  void appendNetworkLong(const boost::uint32_t l)
191  {
192  resize(_size + 4);
193  _data[_size - 4] = l >> 24;
194  _data[_size - 3] = (l >> 16) & 0xff;
195  _data[_size - 2] = (l >> 8) & 0xff;
196  _data[_size - 1] = l & 0xff;
197  }
198 
200  //
207  void append(const SimpleBuffer& buf)
208  {
209  size_t incomingDataSize = buf.size();
210  const boost::uint8_t* incomingData = buf.data();
211  append(incomingData, incomingDataSize);
212  }
213 
214 private:
215  size_t _size;
216  size_t _capacity;
217 
218  boost::scoped_array<boost::uint8_t> _data;
219 };
220 
221 
222 } // namespace gnash
223 
224 #endif // GNASH_SIMPLEBUFFER_H
void append(const SimpleBuffer &buf)
Append data to the buffer.
Definition: SimpleBuffer.h:207
void resize(size_t newSize)
Resize the buffer.
Definition: SimpleBuffer.h:108
SimpleBuffer & operator=(const SimpleBuffer &b)
Assignment operator.
Definition: SimpleBuffer.h:82
SWFStream & s
Definition: DefineBitsTag.cpp:73
void append(const void *inData, size_t size)
Append data to the buffer.
Definition: SimpleBuffer.h:144
boost::uint8_t * data()
Get a pointer to start of data. May be NULL if size==0.
Definition: SimpleBuffer.h:102
Definition: GnashKey.h:158
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
void appendNetworkShort(const boost::uint16_t s)
Append 2 bytes to the buffer.
Definition: SimpleBuffer.h:175
void appendNetworkLong(const boost::uint32_t l)
Append 4 bytes to the buffer.
Definition: SimpleBuffer.h:190
void appendByte(const boost::uint8_t b)
Append a byte to the buffer.
Definition: SimpleBuffer.h:161
SimpleBuffer(size_t capacity=0)
Construct a SimpleBuffer with an optional initial capacity.
Definition: SimpleBuffer.h:49
SimpleBuffer(const SimpleBuffer &b)
Copy constructor.
Definition: SimpleBuffer.h:66
const boost::uint8_t * data() const
Get a pointer to start of data. May be NULL if size==0.
Definition: SimpleBuffer.h:105
Definition: GnashKey.h:148
bool empty() const
Return true if buffer is empty.
Definition: SimpleBuffer.h:93
size_t capacity() const
Return capacity of the buffer.
Definition: SimpleBuffer.h:99
A simple buffer of bytes.
Definition: SimpleBuffer.h:38
size_t size() const
Return size of the buffer.
Definition: SimpleBuffer.h:96
void reserve(size_t newCapacity)
Ensure at least 'newCapacity' bytes are allocated for this buffer.
Definition: SimpleBuffer.h:115