OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
converting_streams.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2011 by the OpenStructure authors
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 3.0 of the License, or (at your option)
9 // any later version.
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 //------------------------------------------------------------------------------
19 
20 #ifndef OST_IO_CONVERTING_STREAMS_H
21 #define OST_IO_CONVERTING_STREAMS_H
22 
23 #include <iostream>
24 #include "convert.hh"
25 
26 #include <ost/stdint.hh>
27 #include <ost/config.hh>
28 
29 
30 namespace ost { namespace io {
31 
32 
33 template<int CONVERSIONTYPE>
34 class BinaryOStream : public std::basic_ostream<char>
35 {
36 public:
37  BinaryOStream(std::basic_ostream<char>& ostr):
38  std::basic_ostream<char>(ostr.rdbuf())
39  {
40  }
41 
42  BinaryOStream& operator<<(const char& value)
43  {
44  std::basic_ostream<char>::write(&value,1);
45  return *this;
46  }
48  {
49  return write_helper(&value,1);
50  }
52  {
53  return write_helper(&value,1);
54  }
56  {
57  return write_helper(&value,1);
58  }
60  {
61  return write_helper(&value,1);
62  }
64  {
65  return write_helper(&value,1);
66  }
68  {
69  return write_helper(&value,1);
70  }
72  {
73  return write_helper(&value,1);
74  }
76  {
77  return write_helper(&value,1);
78  }
79  BinaryOStream& operator<<(const float& value)
80  {
81  return write_helper(&value,1);
82  }
83  BinaryOStream& operator<<(const double& value)
84  {
85  return write_helper(&value,1);
86  }
87 
88  BinaryOStream& write(const char* value,std::streamsize n)
89  {
90  std::basic_ostream<char>::write(value,n);
91  return *this;
92  }
93  BinaryOStream& write(const int8_t* value,std::streamsize n)
94  {
95  return write_helper(value,n);
96  }
97  BinaryOStream& write(const uint8_t* value,std::streamsize n)
98  {
99  return write_helper(value,n);
100  }
101  BinaryOStream& write(const int16_t* value,std::streamsize n)
102  {
103  return write_helper(value,n);
104  }
105  BinaryOStream& write(const uint16_t* value,std::streamsize n)
106  {
107  return write_helper(value,n);
108  }
109  BinaryOStream& write(const int32_t* value,std::streamsize n)
110  {
111  return write_helper(value,n);
112  }
113  BinaryOStream& write(const uint32_t* value,std::streamsize n)
114  {
115  return write_helper(value,n);
116  }
117  BinaryOStream& write(const int64_t* value,std::streamsize n)
118  {
119  return write_helper(value,n);
120  }
121  BinaryOStream& write(const uint64_t* value,std::streamsize n)
122  {
123  return write_helper(value,n);
124  }
125  BinaryOStream& write(const float* value,std::streamsize n)
126  {
127  return write_helper(value,n);
128  }
129 
130  BinaryOStream& write(const double* value,std::streamsize n)
131  {
132  return write_helper(value,n);
133  }
134 
135 protected:
136  template<typename T>
137  BinaryOStream& write_helper(const T* value,std::streamsize n)
138  {
139  for(std::streamsize i=0;i<n;++i){
140  T tmp=Convert<CONVERSIONTYPE,T>::To(value[i]);
141  std::basic_ostream<char>::write(reinterpret_cast<char*>(&tmp),sizeof(T));
142  }
143  return *this;
144  }
145 
146 };
147 
148 template<int CONVERSIONTYPE>
149 class BinaryIStream : public std::basic_istream<char>
150 {
151 public:
152  BinaryIStream( std::basic_istream<char>& istr):
153  std::basic_istream<char>(istr.rdbuf())
154  {
155  }
156 
158  {
159  return read_helper(&value,1);
160  }
162  {
163  return read_helper(&value,1);
164  }
166  {
167  return read_helper(&value,1);
168  }
170  {
171  return read_helper(&value,1);
172  }
174  {
175  return read_helper(&value,1);
176  }
178  {
179  return read_helper(&value,1);
180  }
182  {
183  return read_helper(&value,1);
184  }
186  {
187  return read_helper(&value,1);
188  }
189  BinaryIStream& operator>>(float& value)
190  {
191  return read_helper(&value,1);
192  }
193  BinaryIStream& operator>>(double& value)
194  {
195  return read_helper(&value,1);
196  }
197 
198  BinaryIStream& read(char* value,std::streamsize n)
199  {
200  std::basic_istream<char>::read(value,n);
201  return *this;
202  }
203  BinaryIStream& read(uint8_t* value,std::streamsize n)
204  {
205  return read_helper(value,n);
206  }
207  BinaryIStream& read(int8_t* value,std::streamsize n)
208  {
209  return read_helper(value,n);
210  }
211  BinaryIStream& read(int16_t* value,std::streamsize n)
212  {
213  return read_helper(value,n);
214  }
215  BinaryIStream& read(uint16_t* value,std::streamsize n)
216  {
217  return read_helper(value,n);
218  }
219  BinaryIStream& read(int32_t* value,std::streamsize n)
220  {
221  return read_helper(value,n);
222  }
223  BinaryIStream& read(uint32_t* value,std::streamsize n)
224  {
225  return read_helper(value,n);
226  }
227  BinaryIStream& read(int64_t* value,std::streamsize n)
228  {
229  return read_helper(value,n);
230  }
231  BinaryIStream& read(uint64_t* value,std::streamsize n)
232  {
233  return read_helper(value,n);
234  }
235  BinaryIStream& read(float* value,std::streamsize n)
236  {
237  return read_helper(value,n);
238  }
239  BinaryIStream& read(double* value,std::streamsize n)
240  {
241  return read_helper(value,n);
242  }
243 
244 protected:
245  template<typename T>
246  BinaryIStream& read_helper(T* value,std::streamsize n)
247  {
248  std::basic_istream<char>::read(reinterpret_cast<char*>(value),n*sizeof(T));
249  for(std::streamsize i=0;i<n;++i){
251  }
252  return *this;
253  }
254 };
255 
256 
257 
258 } // io ns
259 
260 
261 
262 } // ost ns
263 
264 
265 #endif
266 
267 
BinaryOStream & write(const int16_t *value, std::streamsize n)
BinaryOStream & operator<<(const uint8_t &value)
BinaryIStream & read(int16_t *value, std::streamsize n)
BinaryIStream & operator>>(int32_t &value)
BinaryOStream(std::basic_ostream< char > &ostr)
BinaryIStream & operator>>(int64_t &value)
BinaryIStream & operator>>(int16_t &value)
static VALUETYPE To(const VALUETYPE &value)
signed int int32_t
Definition: stdint_msc.hh:77
BinaryIStream & operator>>(double &value)
BinaryOStream & write(const uint32_t *value, std::streamsize n)
BinaryOStream & operator<<(const uint16_t &value)
unsigned __int64 uint64_t
Definition: stdint_msc.hh:90
signed short int16_t
Definition: stdint_msc.hh:76
BinaryOStream & operator<<(const char &value)
BinaryOStream & operator<<(const float &value)
signed __int64 int64_t
Definition: stdint_msc.hh:89
BinaryOStream & write(const uint16_t *value, std::streamsize n)
unsigned int uint32_t
Definition: stdint_msc.hh:80
BinaryIStream & operator>>(uint8_t &value)
unsigned char uint8_t
Definition: stdint_msc.hh:78
BinaryIStream & read_helper(T *value, std::streamsize n)
BinaryOStream & write(const uint8_t *value, std::streamsize n)
BinaryIStream & read(int32_t *value, std::streamsize n)
BinaryOStream & write(const char *value, std::streamsize n)
BinaryOStream & operator<<(const int16_t &value)
BinaryIStream & read(double *value, std::streamsize n)
BinaryIStream & read(int8_t *value, std::streamsize n)
signed char int8_t
Definition: stdint_msc.hh:75
BinaryIStream & operator>>(uint16_t &value)
BinaryIStream & operator>>(uint64_t &value)
BinaryOStream & operator<<(const int64_t &value)
BinaryIStream & operator>>(float &value)
BinaryIStream & operator>>(uint32_t &value)
BinaryIStream(std::basic_istream< char > &istr)
static void FromIP(VALUETYPE *value)
BinaryOStream & write(const int64_t *value, std::streamsize n)
BinaryIStream & operator>>(int8_t &value)
BinaryOStream & write(const uint64_t *value, std::streamsize n)
BinaryOStream & write(const int32_t *value, std::streamsize n)
BinaryIStream & read(uint16_t *value, std::streamsize n)
unsigned short uint16_t
Definition: stdint_msc.hh:79
BinaryOStream & write(const int8_t *value, std::streamsize n)
BinaryIStream & read(float *value, std::streamsize n)
BinaryOStream & operator<<(const int32_t &value)
BinaryOStream & write(const float *value, std::streamsize n)
BinaryOStream & write(const double *value, std::streamsize n)
BinaryOStream & operator<<(const double &value)
BinaryIStream & read(char *value, std::streamsize n)
BinaryOStream & write_helper(const T *value, std::streamsize n)
BinaryOStream & operator<<(const uint64_t &value)
BinaryIStream & read(uint8_t *value, std::streamsize n)
BinaryIStream & read(uint64_t *value, std::streamsize n)
BinaryOStream & operator<<(const uint32_t &value)
BinaryIStream & read(int64_t *value, std::streamsize n)
BinaryOStream & operator<<(const int8_t &value)
BinaryIStream & read(uint32_t *value, std::streamsize n)