Open Lighting Architecture  Latest Git
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/base/Macro.h>
26 #include <ola/io/InputBuffer.h>
27 #include <string>
28 
29 namespace ola {
30 namespace io {
31 
36  public:
37  virtual ~InputStreamInterface() {}
38 
39  /*
40  * Extract data from the Stream.
41  * Returns false if there was insufficient data to extract.
42  */
43  virtual bool operator>>(int8_t &val) = 0;
44  virtual bool operator>>(uint8_t &val) = 0;
45  virtual bool operator>>(int16_t &val) = 0;
46  virtual bool operator>>(uint16_t &val) = 0;
47  virtual bool operator>>(int32_t &val) = 0;
48  virtual bool operator>>(uint32_t &val) = 0;
49 
50  /*
51  * Append up to size bytes of data to the string.
52  * @returns the number of bytes read
53  */
54  virtual unsigned int ReadString(std::string *output, unsigned int size) = 0;
55 };
56 
57 
63  public:
64  // Ownership of the InputBuffer is not transferred.
65  explicit InputStream(InputBufferInterface *buffer)
66  : m_buffer(buffer) {
67  }
68  virtual ~InputStream() {}
69 
70  bool operator>>(int8_t &val) { return Extract(&val); }
71  bool operator>>(uint8_t &val) { return Extract(&val); }
72  bool operator>>(int16_t &val) { return Extract(&val); }
73  bool operator>>(uint16_t &val) { return Extract(&val); }
74  bool operator>>(int32_t &val) { return Extract(&val); }
75  bool operator>>(uint32_t &val) { return Extract(&val); }
76 
77  unsigned int ReadString(std::string *output, unsigned int size) {
78  return m_buffer->Read(output, size);
79  }
80 
81  private:
82  InputBufferInterface *m_buffer;
83 
84  template <typename T>
85  bool Extract(T *val) {
86  unsigned int length = m_buffer->Read(reinterpret_cast<uint8_t*>(val),
87  sizeof(*val));
88  return length == sizeof(*val);
89  }
90 
92 };
93 } // namespace io
94 } // namespace ola
95 #endif // INCLUDE_OLA_IO_INPUTSTREAM_H_
virtual unsigned int Read(uint8_t *data, unsigned int length)=0
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Creates dummy copy constructor and assignment operator declarations.
Definition: Macro.h:44
Definition: InputStream.h:35
Definition: InputBuffer.h:34
Helper macros.
Definition: InputStream.h:62
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44