Open Lighting Architecture  0.9.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 structed 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/io/OutputBuffer.h>
26 
27 namespace ola {
28 namespace io {
29 
35  public:
36  virtual ~OutputStreamInterface() {}
37 
38  // Append some data to this OutputQueue
39  virtual void Write(const uint8_t *data, unsigned int length) = 0;
40 
41  virtual OutputStreamInterface& operator<<(uint8_t) = 0;
42  virtual OutputStreamInterface& operator<<(uint16_t) = 0;
43  virtual OutputStreamInterface& operator<<(uint32_t) = 0;
44  virtual OutputStreamInterface& operator<<(int8_t) = 0;
45  virtual OutputStreamInterface& operator<<(int16_t) = 0;
46  virtual OutputStreamInterface& operator<<(int32_t) = 0;
47 };
48 
49 
54  public:
55  // Ownership of the OutputBuffer is not transferred.
56  explicit OutputStream(OutputBufferInterface *buffer)
57  : m_buffer(buffer) {
58  }
59  virtual ~OutputStream() {}
60 
61  // Append some data directly to this OutputBuffer
62  void Write(const uint8_t *data, unsigned int length) {
63  m_buffer->Write(data, length);
64  }
65 
66  OutputStream& operator<<(uint8_t val) { return Write(val); }
67  OutputStream& operator<<(uint16_t val) { return Write(val); }
68  OutputStream& operator<<(uint32_t val) { return Write(val); }
69  OutputStream& operator<<(int8_t val) { return Write(val); }
70  OutputStream& operator<<(int16_t val) { return Write(val); }
71  OutputStream& operator<<(int32_t val) { return Write(val); }
72 
73  private:
74  OutputBufferInterface *m_buffer;
75 
76  template<typename T>
77  OutputStream& Write(const T &val) {
78  m_buffer->Write(reinterpret_cast<const uint8_t*>(&val),
79  static_cast<unsigned int>(sizeof(val)));
80  return *this;
81  }
82 
83  OutputStream(const OutputStream&);
84  OutputStream& operator=(const OutputStream&);
85 };
86 } // namespace io
87 } // namespace ola
88 #endif // INCLUDE_OLA_IO_OUTPUTSTREAM_H_