Open Lighting Architecture  Latest Git
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * MemoryBuffer.h
17  * A derived 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/base/Macro.h>
25 #include <ola/io/InputBuffer.h>
26 #include <stdint.h>
27 #include <string.h>
28 #include <algorithm>
29 #include <string>
30 
31 namespace ola {
32 namespace io {
33 
39  public:
40  explicit MemoryBuffer(const uint8_t *data, unsigned int size)
41  : m_data(data),
42  m_size(size),
43  m_cursor(0) {
44  }
45  ~MemoryBuffer() {}
46 
47  unsigned int Read(uint8_t *data, unsigned int length) {
48  unsigned int data_size = std::min(m_size - m_cursor, length);
49  memcpy(data, m_data + m_cursor, data_size);
50  m_cursor += data_size;
51  return data_size;
52  }
53 
54  unsigned int Read(std::string *output, unsigned int length) {
55  unsigned int data_size = std::min(m_size - m_cursor, length);
56  output->append(reinterpret_cast<const char*>(m_data + m_cursor),
57  data_size);
58  m_cursor += data_size;
59  return data_size;
60  }
61 
62  private:
63  const uint8_t *m_data;
64  const unsigned int m_size;
65  unsigned int m_cursor;
66 
67  DISALLOW_COPY_AND_ASSIGN(MemoryBuffer);
68 };
69 } // namespace io
70 } // namespace ola
71 #endif // INCLUDE_OLA_IO_MEMORYBUFFER_H_
Definition: MemoryBuffer.h:38
unsigned int Read(uint8_t *data, unsigned int length)
Definition: MemoryBuffer.h:47
Definition: InputBuffer.h:34
Helper macros.
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44