Open Lighting Architecture
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MemoryBuffer.h
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * MemoryBuffer.h
17  * An derrived class of InputBuffer that wraps a memory buffer.
18  * Copyright (C) 2012 Simon Newton
19  */
20 
21 #ifndef INCLUDE_OLA_IO_MEMORYBUFFER_H_
22 #define INCLUDE_OLA_IO_MEMORYBUFFER_H_
23 
24 #include <ola/io/InputBuffer.h>
25 #include <stdint.h>
26 #include <string.h>
27 #include <algorithm>
28 #include <string>
29 
30 namespace ola {
31 namespace io {
32 
38  public:
39  explicit MemoryBuffer(const uint8_t *data, unsigned int size)
40  : m_data(data),
41  m_size(size),
42  m_cursor(0) {
43  }
44  ~MemoryBuffer() {}
45 
46  unsigned int Read(uint8_t *data, unsigned int length) {
47  unsigned int data_size = std::min(m_size - m_cursor, length);
48  memcpy(data, m_data + m_cursor, data_size);
49  m_cursor += data_size;
50  return data_size;
51  }
52 
53  unsigned int Read(std::string *output, unsigned int length) {
54  unsigned int data_size = std::min(m_size - m_cursor, length);
55  output->append(reinterpret_cast<const char*>(m_data + m_cursor),
56  data_size);
57  m_cursor += data_size;
58  return data_size;
59  }
60 
61  private:
62  const uint8_t *m_data;
63  const unsigned int m_size;
64  unsigned int m_cursor;
65 
66  MemoryBuffer(const MemoryBuffer&);
67  MemoryBuffer& operator=(const MemoryBuffer&);
68 };
69 } // namespace io
70 } // namespace ola
71 #endif // INCLUDE_OLA_IO_MEMORYBUFFER_H_