Open Lighting Architecture
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 
30 namespace ola {
31 namespace io {
32 
33 using std::string;
34 
35 // An abstract class that guarantees byte order will be converted.
37 
44  public:
45  // Ownership of the stream is not transferred.
47  : m_stream(stream) {
48  }
50 
51  bool operator>>(int8_t &val) { return ExtractAndConvert(&val); }
52  bool operator>>(uint8_t &val) { return ExtractAndConvert(&val); }
53  bool operator>>(int16_t &val) { return ExtractAndConvert(&val); }
54  bool operator>>(uint16_t &val) { return ExtractAndConvert(&val); }
55  bool operator>>(int32_t &val) { return ExtractAndConvert(&val); }
56  bool operator>>(uint32_t &val) { return ExtractAndConvert(&val); }
57 
58  unsigned int ReadString(string *output, unsigned int size) {
59  return m_stream->ReadString(output, size);
60  }
61 
62  private:
63  InputStreamInterface *m_stream;
64 
65  template <typename T>
66  bool ExtractAndConvert(T *val) {
67  bool ok = (*m_stream) >> *val;
68  *val = ola::network::NetworkToHost(*val);
69  return ok;
70  }
71 
74 };
75 
76 
81  public:
82  // Ownership of the InputBuffer is not transferred.
84  : m_input_stream(buffer),
85  m_adaptor(&m_input_stream) {
86  }
88 
89  bool operator>>(int8_t &val) { return m_adaptor >> val; }
90  bool operator>>(uint8_t &val) { return m_adaptor >> val; }
91  bool operator>>(int16_t &val) { return m_adaptor >> val; }
92  bool operator>>(uint16_t &val) { return m_adaptor >> val; }
93  bool operator>>(int32_t &val) { return m_adaptor >> val; }
94  bool operator>>(uint32_t &val) { return m_adaptor >> val; }
95 
96  unsigned int ReadString(string *output, unsigned int size) {
97  return m_adaptor.ReadString(output, size);
98  }
99 
100  private:
101  InputStream m_input_stream;
102  BigEndianInputStreamAdaptor m_adaptor;
103 
105  BigEndianInputStream& operator=(const BigEndianInputStream&);
106 };
107 
108 
109 // An abstract class that guarantees byte order will be converted.
111 
118  public:
119  // Ownership of the stream is not transferred.
121  : m_stream(stream) {
122  }
124 
125  void Write(const uint8_t *data, unsigned int length) {
126  m_stream->Write(data, length);
127  }
128 
129  BigEndianOutputStreamAdaptor& operator<<(int8_t val) {
130  return ConvertAndWrite(val);
131  }
132 
133  BigEndianOutputStreamAdaptor& operator<<(uint8_t val) {
134  return ConvertAndWrite(val);
135  }
136 
137  BigEndianOutputStreamAdaptor& operator<<(int16_t val) {
138  return ConvertAndWrite(val);
139  }
140 
141  BigEndianOutputStreamAdaptor& operator<<(uint16_t val) {
142  return ConvertAndWrite(val);
143  }
144 
145  BigEndianOutputStreamAdaptor& operator<<(int32_t val) {
146  return ConvertAndWrite(val);
147  }
148 
149  BigEndianOutputStreamAdaptor& operator<<(uint32_t val) {
150  return ConvertAndWrite(val);
151  }
152 
153  private:
154  OutputStreamInterface *m_stream;
155 
156  template <typename T>
157  BigEndianOutputStreamAdaptor& ConvertAndWrite(const T &val) {
158  (*m_stream) << ola::network::HostToNetwork(val);
159  return *this;
160  }
161 
163  BigEndianOutputStreamAdaptor& operator=(
165 };
166 
167 
172  public:
173  // Ownership of the OutputBuffer is not transferred.
175  : m_output_stream(buffer),
176  m_adaptor(&m_output_stream) {
177  }
179 
180  void Write(const uint8_t *data, unsigned int length) {
181  m_adaptor.Write(data, length);
182  }
183 
184  BigEndianOutputStream& operator<<(int8_t val) { return Output(val); }
185  BigEndianOutputStream& operator<<(uint8_t val) { return Output(val); }
186  BigEndianOutputStream& operator<<(int16_t val) { return Output(val); }
187  BigEndianOutputStream& operator<<(uint16_t val) { return Output(val); }
188  BigEndianOutputStream& operator<<(int32_t val) { return Output(val); }
189  BigEndianOutputStream& operator<<(uint32_t val) { return Output(val); }
190 
191  private:
192  OutputStream m_output_stream;
194 
195  template <typename T>
196  BigEndianOutputStream& Output(T val) {
197  m_adaptor << val;
198  return *this;
199  }
200 
202  BigEndianOutputStream& operator=(const BigEndianOutputStream&);
203 };
204 } // namespace io
205 } // namespace ola
206 #endif // INCLUDE_OLA_IO_BIGENDIANSTREAM_H_