retdec
OutputBuffer.h
Go to the documentation of this file.
1 /*
2 * OutputBuffer.h - Part of the PeLib library.
3 *
4 * Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
5 * All rights reserved.
6 *
7 * This software is licensed under the zlib/libpng License.
8 * For more details see http://www.opensource.org/licenses/zlib-license.php
9 * or the license information file (license.htm) in the root directory
10 * of PeLib.
11 */
12 
13 #ifndef RETDEC_PELIB_OUTPUTBUFFER_H
14 #define RETDEC_PELIB_OUTPUTBUFFER_H
15 
16 #include <vector>
17 #include <iterator>
18 
19 namespace PeLib
20 {
22  {
23  private:
24  std::vector<unsigned char>& m_vBuffer;
25 
26  public:
27  OutputBuffer(std::vector<unsigned char>& vBuffer);
28  const unsigned char* data() const;
29  unsigned long size();
30 
31  template<typename T>
32  OutputBuffer& operator<<(const T& value)
33  {
34  const unsigned char* p = reinterpret_cast<const unsigned char*>(&value);
35  std::copy(p, p + sizeof(value), std::back_inserter(m_vBuffer));
36  return *this;
37  }
38  void add(const char* lpBuffer, unsigned long ulSize);
39  void reset();
40  void resize(unsigned int uiSize);
41  void set(unsigned int uiPosition);
42 
43  template<typename T>
44  void update(unsigned long ulIndex, const T& value)
45  {
46  *reinterpret_cast<T*>(m_vBuffer.data() + ulIndex) = value;
47  }
48 
49  template<typename T>
50  void insert(unsigned long ulIndex, const T& value)
51  {
52  if (ulIndex + sizeof(T) >= size())
53  resize(ulIndex + sizeof(T));
54 
55  update(ulIndex, value);
56  }
57  };
58 }
59 
60 #endif
Definition: OutputBuffer.h:22
OutputBuffer & operator<<(const T &value)
Definition: OutputBuffer.h:32
OutputBuffer(std::vector< unsigned char > &vBuffer)
Definition: OutputBuffer.cpp:17
void reset()
Definition: OutputBuffer.cpp:37
unsigned long size()
Definition: OutputBuffer.cpp:27
void insert(unsigned long ulIndex, const T &value)
Definition: OutputBuffer.h:50
const unsigned char * data() const
Definition: OutputBuffer.cpp:22
void resize(unsigned int uiSize)
Definition: OutputBuffer.cpp:42
void add(const char *lpBuffer, unsigned long ulSize)
Definition: OutputBuffer.cpp:32
void update(unsigned long ulIndex, const T &value)
Definition: OutputBuffer.h:44
std::vector< unsigned char > & m_vBuffer
Definition: OutputBuffer.h:24
void set(unsigned int uiPosition)
Definition: BoundImportDirectory.h:21