Open Lighting Architecture: include/ola/io/InputStream.h Source File

| Open Lighting Architecture 0.10.9

Loading...
Searching...
No Matches
InputStream.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 * InputStream.h
17 * A interface & class to extract formatted data.
18 * Copyright (C) 2012 Simon Newton
19 */
20
21#ifndef INCLUDE_OLA_IO_INPUTSTREAM_H_
22#define INCLUDE_OLA_IO_INPUTSTREAM_H_
23
24#include <stdint.h>
25#include <ola/io/InputBuffer.h>
26#include <string>
27
28namespace ola {
29namespace io {
30
34 class InputStreamInterface {
35 public:
36 virtual ~InputStreamInterface() {}
37
38 /*
39 * Extract data from the Stream.
40 * Returns false if there was insufficient data to extract.
41 */
42 virtual bool operator>>(int8_t &val) = 0;
43 virtual bool operator>>(uint8_t &val) = 0;
44 virtual bool operator>>(int16_t &val) = 0;
45 virtual bool operator>>(uint16_t &val) = 0;
46 virtual bool operator>>(int32_t &val) = 0;
47 virtual bool operator>>(uint32_t &val) = 0;
48
49 /*
50 * Append up to size bytes of data to the string.
51 * @returns the number of bytes read
52 */
53 virtual unsigned int ReadString(std::string *output, unsigned int size) = 0;
54};
55
56
61 class InputStream: public InputStreamInterface {
62 public:
63 // Ownership of the InputBuffer is not transferred.
64 explicit InputStream(InputBufferInterface *buffer)
65 : m_buffer(buffer) {
66 }
67 virtual ~InputStream() {}
68
69 bool operator>>(int8_t &val) { return Extract(&val); }
70 bool operator>>(uint8_t &val) { return Extract(&val); }
71 bool operator>>(int16_t &val) { return Extract(&val); }
72 bool operator>>(uint16_t &val) { return Extract(&val); }
73 bool operator>>(int32_t &val) { return Extract(&val); }
74 bool operator>>(uint32_t &val) { return Extract(&val); }
75
76 unsigned int ReadString(std::string *output, unsigned int size) {
77 return m_buffer->Read(output, size);
78 }
79
80 private:
81 InputBufferInterface *m_buffer;
82
83 template <typename T>
84 bool Extract(T *val) {
85 unsigned int length = m_buffer->Read(reinterpret_cast<uint8_t*>(val),
86 sizeof(*val));
87 return length == sizeof(*val);
88 }
89
90 InputStream(const InputStream&);
91 InputStream& operator=(const InputStream&);
92};
93} // namespace io
94} // namespace ola
95#endif // INCLUDE_OLA_IO_INPUTSTREAM_H_
ola::io::InputBufferInterface
Definition InputBuffer.h:34
ola::io::InputBufferInterface::Read
virtual unsigned int Read(uint8_t *data, unsigned int length)=0
ola::io::InputStreamInterface
Definition InputStream.h:34
ola::io
Classes for general I/O and event handling.
Definition Descriptor.cpp:49
ola
The namespace containing all OLA symbols.
Definition Credentials.cpp:44