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

| 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.
34 class BigEndianInputStreamInterface: public InputStreamInterface {};
35
41 class BigEndianInputStreamAdaptor: public BigEndianInputStreamInterface {
42 public:
43 // Ownership of the stream is not transferred.
44 explicit BigEndianInputStreamAdaptor(InputStreamInterface *stream)
45 : m_stream(stream) {
46 }
47 ~BigEndianInputStreamAdaptor() {}
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
70 BigEndianInputStreamAdaptor(const BigEndianInputStreamAdaptor&);
71 BigEndianInputStreamAdaptor& operator=(const BigEndianInputStreamAdaptor&);
72};
73
74
78 class BigEndianInputStream: public BigEndianInputStreamInterface {
79 public:
80 // Ownership of the InputBuffer is not transferred.
81 explicit BigEndianInputStream(InputBufferInterface *buffer)
82 : m_input_stream(buffer),
83 m_adaptor(&m_input_stream) {
84 }
85 ~BigEndianInputStream() {}
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;
100 BigEndianInputStreamAdaptor m_adaptor;
101
102 BigEndianInputStream(const BigEndianInputStream&);
103 BigEndianInputStream& operator=(const BigEndianInputStream&);
104};
105
106
107// An abstract class that guarantees byte order will be converted.
108 class BigEndianOutputStreamInterface: public OutputStreamInterface {};
109
115 class BigEndianOutputStreamAdaptor: public BigEndianOutputStreamInterface {
116 public:
117 // Ownership of the stream is not transferred.
118 explicit BigEndianOutputStreamAdaptor(OutputStreamInterface *stream)
119 : m_stream(stream) {
120 }
121 ~BigEndianOutputStreamAdaptor() {}
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
160 BigEndianOutputStreamAdaptor(const BigEndianOutputStreamAdaptor&);
161 BigEndianOutputStreamAdaptor& operator=(
162 const BigEndianOutputStreamAdaptor&);
163};
164
165
169 class BigEndianOutputStream: public BigEndianOutputStreamInterface {
170 public:
171 // Ownership of the OutputBuffer is not transferred.
172 explicit BigEndianOutputStream(OutputBufferInterface *buffer)
173 : m_output_stream(buffer),
174 m_adaptor(&m_output_stream) {
175 }
176 ~BigEndianOutputStream() {}
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;
191 BigEndianOutputStreamAdaptor m_adaptor;
192
193 template <typename T>
194 BigEndianOutputStream& Output(T val) {
195 m_adaptor << val;
196 return *this;
197 }
198
199 BigEndianOutputStream(const BigEndianOutputStream&);
200 BigEndianOutputStream& operator=(const BigEndianOutputStream&);
201};
202} // namespace io
203} // namespace ola
204#endif // INCLUDE_OLA_IO_BIGENDIANSTREAM_H_
ola::io::BigEndianInputStreamAdaptor
Definition BigEndianStream.h:41
ola::io::BigEndianInputStreamInterface
Definition BigEndianStream.h:34
ola::io::BigEndianOutputStreamAdaptor
Definition BigEndianStream.h:115
ola::io::BigEndianOutputStreamInterface
Definition BigEndianStream.h:108
ola::io::InputBufferInterface
Definition InputBuffer.h:34
ola::io::InputStream
Definition InputStream.h:61
ola::io::InputStreamInterface
Definition InputStream.h:34
ola::io::OutputBufferInterface
Definition OutputBuffer.h:36
ola::io::OutputStream
Definition OutputStream.h:53
ola::io::OutputStreamInterface
Definition OutputStream.h:34
ola::io
Classes for general I/O and event handling.
Definition Descriptor.cpp:49
ola::network::NetworkToHost
uint16_t NetworkToHost(uint16_t value)
16-bit unsigned network to host conversion.
Definition NetworkUtils.cpp:143
ola::network::HostToNetwork
uint16_t HostToNetwork(uint16_t value)
16-bit unsigned host to network conversion.
Definition NetworkUtils.cpp:159
ola
The namespace containing all OLA symbols.
Definition Credentials.cpp:44