Open Lighting Architecture  Latest Git
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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/base/Macro.h>
26 #include <ola/io/InputStream.h>
27 #include <ola/io/OutputStream.h>
28 #include <ola/network/NetworkUtils.h>
29 #include <string>
30 
31 namespace ola {
32 namespace io {
33 
34 // An abstract class that guarantees byte order will be converted.
36 
43  public:
44  // Ownership of the stream is not transferred.
46  : m_stream(stream) {
47  }
49 
50  bool operator>>(int8_t &val) { return ExtractAndConvert(&val); }
51  bool operator>>(uint8_t &val) { return ExtractAndConvert(&val); }
52  bool operator>>(int16_t &val) { return ExtractAndConvert(&val); }
53  bool operator>>(uint16_t &val) { return ExtractAndConvert(&val); }
54  bool operator>>(int32_t &val) { return ExtractAndConvert(&val); }
55  bool operator>>(uint32_t &val) { return ExtractAndConvert(&val); }
56 
57  unsigned int ReadString(std::string *output, unsigned int size) {
58  return m_stream->ReadString(output, size);
59  }
60 
61  private:
62  InputStreamInterface *m_stream;
63 
64  template <typename T>
65  bool ExtractAndConvert(T *val) {
66  bool ok = (*m_stream) >> *val;
67  *val = ola::network::NetworkToHost(*val);
68  return ok;
69  }
70 
71  DISALLOW_COPY_AND_ASSIGN(BigEndianInputStreamAdaptor);
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;
100  BigEndianInputStreamAdaptor m_adaptor;
101 
102  DISALLOW_COPY_AND_ASSIGN(BigEndianInputStream);
103 };
104 
105 
106 // An abstract class that guarantees byte order will be converted.
108 
115  public:
116  // Ownership of the stream is not transferred.
118  : m_stream(stream) {
119  }
121 
122  void Write(const uint8_t *data, unsigned int length) {
123  m_stream->Write(data, length);
124  }
125 
126  BigEndianOutputStreamAdaptor& operator<<(int8_t val) {
127  return ConvertAndWrite(val);
128  }
129 
130  BigEndianOutputStreamAdaptor& operator<<(uint8_t val) {
131  return ConvertAndWrite(val);
132  }
133 
134  BigEndianOutputStreamAdaptor& operator<<(int16_t val) {
135  return ConvertAndWrite(val);
136  }
137 
138  BigEndianOutputStreamAdaptor& operator<<(uint16_t val) {
139  return ConvertAndWrite(val);
140  }
141 
142  BigEndianOutputStreamAdaptor& operator<<(int32_t val) {
143  return ConvertAndWrite(val);
144  }
145 
146  BigEndianOutputStreamAdaptor& operator<<(uint32_t val) {
147  return ConvertAndWrite(val);
148  }
149 
150  private:
151  OutputStreamInterface *m_stream;
152 
153  template <typename T>
154  BigEndianOutputStreamAdaptor& ConvertAndWrite(const T &val) {
155  (*m_stream) << ola::network::HostToNetwork(val);
156  return *this;
157  }
158 
159  DISALLOW_COPY_AND_ASSIGN(BigEndianOutputStreamAdaptor);
160 };
161 
162 
167  public:
168  // Ownership of the OutputBuffer is not transferred.
170  : m_output_stream(buffer),
171  m_adaptor(&m_output_stream) {
172  }
174 
175  void Write(const uint8_t *data, unsigned int length) {
176  m_adaptor.Write(data, length);
177  }
178 
179  BigEndianOutputStream& operator<<(int8_t val) { return Output(val); }
180  BigEndianOutputStream& operator<<(uint8_t val) { return Output(val); }
181  BigEndianOutputStream& operator<<(int16_t val) { return Output(val); }
182  BigEndianOutputStream& operator<<(uint16_t val) { return Output(val); }
183  BigEndianOutputStream& operator<<(int32_t val) { return Output(val); }
184  BigEndianOutputStream& operator<<(uint32_t val) { return Output(val); }
185 
186  private:
187  OutputStream m_output_stream;
189 
190  template <typename T>
191  BigEndianOutputStream& Output(T val) {
192  m_adaptor << val;
193  return *this;
194  }
195 
196  DISALLOW_COPY_AND_ASSIGN(BigEndianOutputStream);
197 };
198 } // namespace io
199 } // namespace ola
200 #endif // INCLUDE_OLA_IO_BIGENDIANSTREAM_H_
Definition: OutputStream.h:35
uint16_t HostToNetwork(uint16_t value)
16-bit unsigned host to network conversion.
Definition: NetworkUtils.cpp:158
Definition: BigEndianStream.h:166
Definition: OutputBuffer.h:36
Definition: BigEndianStream.h:35
uint16_t NetworkToHost(uint16_t value)
16-bit unsigned network to host conversion.
Definition: NetworkUtils.cpp:142
Definition: BigEndianStream.h:78
Definition: BigEndianStream.h:114
Definition: OutputStream.h:54
Definition: InputStream.h:35
Definition: BigEndianStream.h:107
Definition: InputBuffer.h:34
Helper macros.
Definition: InputStream.h:62
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
Definition: BigEndianStream.h:42