Open Lighting Architecture  Latest Git
OutputStream.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  * OutputStream.h
17  * A class that you can write structured data to.
18  * Copyright (C) 2012 Simon Newton
19  */
20 
21 #ifndef INCLUDE_OLA_IO_OUTPUTSTREAM_H_
22 #define INCLUDE_OLA_IO_OUTPUTSTREAM_H_
23 
24 #include <stdint.h>
25 #include <ola/base/Macro.h>
26 #include <ola/io/OutputBuffer.h>
27 
28 namespace ola {
29 namespace io {
30 
36  public:
37  virtual ~OutputStreamInterface() {}
38 
39  // Append some data to this OutputQueue
40  virtual void Write(const uint8_t *data, unsigned int length) = 0;
41 
42  virtual OutputStreamInterface& operator<<(uint8_t) = 0;
43  virtual OutputStreamInterface& operator<<(uint16_t) = 0;
44  virtual OutputStreamInterface& operator<<(uint32_t) = 0;
45  virtual OutputStreamInterface& operator<<(int8_t) = 0;
46  virtual OutputStreamInterface& operator<<(int16_t) = 0;
47  virtual OutputStreamInterface& operator<<(int32_t) = 0;
48 };
49 
50 
55  public:
56  // Ownership of the OutputBuffer is not transferred.
57  explicit OutputStream(OutputBufferInterface *buffer)
58  : m_buffer(buffer) {
59  }
60  virtual ~OutputStream() {}
61 
62  // Append some data directly to this OutputBuffer
63  void Write(const uint8_t *data, unsigned int length) {
64  m_buffer->Write(data, length);
65  }
66 
67  OutputStream& operator<<(uint8_t val) { return Write(val); }
68  OutputStream& operator<<(uint16_t val) { return Write(val); }
69  OutputStream& operator<<(uint32_t val) { return Write(val); }
70  OutputStream& operator<<(int8_t val) { return Write(val); }
71  OutputStream& operator<<(int16_t val) { return Write(val); }
72  OutputStream& operator<<(int32_t val) { return Write(val); }
73 
74  private:
75  OutputBufferInterface *m_buffer;
76 
77  template<typename T>
78  OutputStream& Write(const T &val) {
79  m_buffer->Write(reinterpret_cast<const uint8_t*>(&val),
80  static_cast<unsigned int>(sizeof(val)));
81  return *this;
82  }
83 
85 };
86 } // namespace io
87 } // namespace ola
88 #endif // INCLUDE_OLA_IO_OUTPUTSTREAM_H_
Definition: OutputStream.h:35
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Creates dummy copy constructor and assignment operator declarations.
Definition: Macro.h:44
Definition: OutputBuffer.h:36
Definition: OutputStream.h:54
Helper macros.
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44