Open Lighting Architecture 0.10.9
Loading...
Searching...
No Matches
BigEndianStream.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 * BigEndianStream.h
17 * Wraps another {Input,Output}StreamInterface object and converts from Big
18 * Endian to host order.
19 * Copyright (C) 2012 Simon Newton
20 */
21
22#ifndef INCLUDE_OLA_IO_BIGENDIANSTREAM_H_
23#define INCLUDE_OLA_IO_BIGENDIANSTREAM_H_
24
25#include <ola/io/InputStream.h>
26#include <ola/io/OutputStream.h>
27#include <ola/network/NetworkUtils.h>
28#include <string>
29
30namespace ola {
31namespace io {
32
33// An abstract class that guarantees byte order will be converted.
35
42 public:
43 // Ownership of the stream is not transferred.
45 : m_stream(stream) {
46 }
48
49 bool operator>>(int8_t &val) { return ExtractAndConvert(&val); }
50 bool operator>>(uint8_t &val) { return ExtractAndConvert(&val); }
51 bool operator>>(int16_t &val) { return ExtractAndConvert(&val); }
52 bool operator>>(uint16_t &val) { return ExtractAndConvert(&val); }
53 bool operator>>(int32_t &val) { return ExtractAndConvert(&val); }
54 bool operator>>(uint32_t &val) { return ExtractAndConvert(&val); }
55
56 unsigned int ReadString(std::string *output, unsigned int size) {
57 return m_stream->ReadString(output, size);
58 }
59
60 private:
61 InputStreamInterface *m_stream;
62
63 template <typename T>
64 bool ExtractAndConvert(T *val) {
65 bool ok = (*m_stream) >> *val;
66 *val = ola::network::NetworkToHost(*val);
67 return ok;
68 }
69
72};
73
74
79 public:
80 // Ownership of the InputBuffer is not transferred.
82 : m_input_stream(buffer),
83 m_adaptor(&m_input_stream) {
84 }
86
87 bool operator>>(int8_t &val) { return m_adaptor >> val; }
88 bool operator>>(uint8_t &val) { return m_adaptor >> val; }
89 bool operator>>(int16_t &val) { return m_adaptor >> val; }
90 bool operator>>(uint16_t &val) { return m_adaptor >> val; }
91 bool operator>>(int32_t &val) { return m_adaptor >> val; }
92 bool operator>>(uint32_t &val) { return m_adaptor >> val; }
93
94 unsigned int ReadString(std::string *output, unsigned int size) {
95 return m_adaptor.ReadString(output, size);
96 }
97
98 private:
99 InputStream m_input_stream;
101
104};
105
106
107// An abstract class that guarantees byte order will be converted.
109
116 public:
117 // Ownership of the stream is not transferred.
119 : m_stream(stream) {
120 }
122
123 void Write(const uint8_t *data, unsigned int length) {
124 m_stream->Write(data, length);
125 }
126
127 BigEndianOutputStreamAdaptor& operator<<(int8_t val) {
128 return ConvertAndWrite(val);
129 }
130
131 BigEndianOutputStreamAdaptor& operator<<(uint8_t val) {
132 return ConvertAndWrite(val);
133 }
134
135 BigEndianOutputStreamAdaptor& operator<<(int16_t val) {
136 return ConvertAndWrite(val);
137 }
138
139 BigEndianOutputStreamAdaptor& operator<<(uint16_t val) {
140 return ConvertAndWrite(val);
141 }
142
143 BigEndianOutputStreamAdaptor& operator<<(int32_t val) {
144 return ConvertAndWrite(val);
145 }
146
147 BigEndianOutputStreamAdaptor& operator<<(uint32_t val) {
148 return ConvertAndWrite(val);
149 }
150
151 private:
152 OutputStreamInterface *m_stream;
153
154 template <typename T>
155 BigEndianOutputStreamAdaptor& ConvertAndWrite(const T &val) {
156 (*m_stream) << ola::network::HostToNetwork(val);
157 return *this;
158 }
159
163};
164
165
170 public:
171 // Ownership of the OutputBuffer is not transferred.
173 : m_output_stream(buffer),
174 m_adaptor(&m_output_stream) {
175 }
177
178 void Write(const uint8_t *data, unsigned int length) {
179 m_adaptor.Write(data, length);
180 }
181
182 BigEndianOutputStream& operator<<(int8_t val) { return Output(val); }
183 BigEndianOutputStream& operator<<(uint8_t val) { return Output(val); }
184 BigEndianOutputStream& operator<<(int16_t val) { return Output(val); }
185 BigEndianOutputStream& operator<<(uint16_t val) { return Output(val); }
186 BigEndianOutputStream& operator<<(int32_t val) { return Output(val); }
187 BigEndianOutputStream& operator<<(uint32_t val) { return Output(val); }
188
189 private:
190 OutputStream m_output_stream;
192
193 template <typename T>
194 BigEndianOutputStream& Output(T val) {
195 m_adaptor << val;
196 return *this;
197 }
198
201};
202} // namespace io
203} // namespace ola
204#endif // INCLUDE_OLA_IO_BIGENDIANSTREAM_H_
Definition BigEndianStream.h:41
Definition BigEndianStream.h:78
Definition BigEndianStream.h:34
Definition BigEndianStream.h:115
Definition BigEndianStream.h:169
Definition BigEndianStream.h:108
Definition InputBuffer.h:34
Definition InputStream.h:61
Definition InputStream.h:34
Definition OutputBuffer.h:36
Definition OutputStream.h:53
Definition OutputStream.h:34
uint16_t NetworkToHost(uint16_t value)
16-bit unsigned network to host conversion.
Definition NetworkUtils.cpp:143
uint16_t HostToNetwork(uint16_t value)
16-bit unsigned host to network conversion.
Definition NetworkUtils.cpp:159
The namespace containing all OLA symbols.
Definition Credentials.cpp:44