00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef OST_IO_CONVERTING_STREAMS_H
00021 #define OST_IO_CONVERTING_STREAMS_H
00022
00023 #include <iostream>
00024 #include "convert.hh"
00025
00026 #include <ost/stdint.hh>
00027 #include <ost/config.hh>
00028
00029
00030 namespace ost { namespace io {
00031
00032
00033 template<int CONVERSIONTYPE>
00034 class BinaryOStream : public std::basic_ostream<char>
00035 {
00036 public:
00037 BinaryOStream(std::basic_ostream<char>& ostr):
00038 std::basic_ostream<char>(ostr.rdbuf())
00039 {
00040 }
00041
00042 BinaryOStream& operator<<(const char& value)
00043 {
00044 std::basic_ostream<char>::write(&value,1);
00045 return *this;
00046 }
00047 BinaryOStream& operator<<(const int8_t& value)
00048 {
00049 return write_helper(&value,1);
00050 }
00051 BinaryOStream& operator<<(const uint8_t& value)
00052 {
00053 return write_helper(&value,1);
00054 }
00055 BinaryOStream& operator<<(const int16_t& value)
00056 {
00057 return write_helper(&value,1);
00058 }
00059 BinaryOStream& operator<<(const uint16_t& value)
00060 {
00061 return write_helper(&value,1);
00062 }
00063 BinaryOStream& operator<<(const int32_t& value)
00064 {
00065 return write_helper(&value,1);
00066 }
00067 BinaryOStream& operator<<(const uint32_t& value)
00068 {
00069 return write_helper(&value,1);
00070 }
00071 BinaryOStream& operator<<(const int64_t& value)
00072 {
00073 return write_helper(&value,1);
00074 }
00075 BinaryOStream& operator<<(const uint64_t& value)
00076 {
00077 return write_helper(&value,1);
00078 }
00079 BinaryOStream& operator<<(const float& value)
00080 {
00081 return write_helper(&value,1);
00082 }
00083 BinaryOStream& operator<<(const double& value)
00084 {
00085 return write_helper(&value,1);
00086 }
00087
00088 BinaryOStream& write(const char* value,std::streamsize n)
00089 {
00090 std::basic_ostream<char>::write(value,n);
00091 return *this;
00092 }
00093 BinaryOStream& write(const int8_t* value,std::streamsize n)
00094 {
00095 return write_helper(value,n);
00096 }
00097 BinaryOStream& write(const uint8_t* value,std::streamsize n)
00098 {
00099 return write_helper(value,n);
00100 }
00101 BinaryOStream& write(const int16_t* value,std::streamsize n)
00102 {
00103 return write_helper(value,n);
00104 }
00105 BinaryOStream& write(const uint16_t* value,std::streamsize n)
00106 {
00107 return write_helper(value,n);
00108 }
00109 BinaryOStream& write(const int32_t* value,std::streamsize n)
00110 {
00111 return write_helper(value,n);
00112 }
00113 BinaryOStream& write(const uint32_t* value,std::streamsize n)
00114 {
00115 return write_helper(value,n);
00116 }
00117 BinaryOStream& write(const int64_t* value,std::streamsize n)
00118 {
00119 return write_helper(value,n);
00120 }
00121 BinaryOStream& write(const uint64_t* value,std::streamsize n)
00122 {
00123 return write_helper(value,n);
00124 }
00125 BinaryOStream& write(const float* value,std::streamsize n)
00126 {
00127 return write_helper(value,n);
00128 }
00129
00130 BinaryOStream& write(const double* value,std::streamsize n)
00131 {
00132 return write_helper(value,n);
00133 }
00134
00135 protected:
00136 template<typename T>
00137 BinaryOStream& write_helper(const T* value,std::streamsize n)
00138 {
00139 for(std::streamsize i=0;i<n;++i){
00140 T tmp=Convert<CONVERSIONTYPE,T>::To(value[i]);
00141 std::basic_ostream<char>::write(reinterpret_cast<char*>(&tmp),sizeof(T));
00142 }
00143 return *this;
00144 }
00145
00146 };
00147
00148 template<int CONVERSIONTYPE>
00149 class BinaryIStream : public std::basic_istream<char>
00150 {
00151 public:
00152 BinaryIStream( std::basic_istream<char>& istr):
00153 std::basic_istream<char>(istr.rdbuf())
00154 {
00155 }
00156
00157 BinaryIStream& operator>>(int8_t& value)
00158 {
00159 return read_helper(&value,1);
00160 }
00161 BinaryIStream& operator>>(uint8_t& value)
00162 {
00163 return read_helper(&value,1);
00164 }
00165 BinaryIStream& operator>>(int16_t& value)
00166 {
00167 return read_helper(&value,1);
00168 }
00169 BinaryIStream& operator>>(uint16_t& value)
00170 {
00171 return read_helper(&value,1);
00172 }
00173 BinaryIStream& operator>>(int32_t& value)
00174 {
00175 return read_helper(&value,1);
00176 }
00177 BinaryIStream& operator>>(uint32_t& value)
00178 {
00179 return read_helper(&value,1);
00180 }
00181 BinaryIStream& operator>>(int64_t& value)
00182 {
00183 return read_helper(&value,1);
00184 }
00185 BinaryIStream& operator>>(uint64_t& value)
00186 {
00187 return read_helper(&value,1);
00188 }
00189 BinaryIStream& operator>>(float& value)
00190 {
00191 return read_helper(&value,1);
00192 }
00193 BinaryIStream& operator>>(double& value)
00194 {
00195 return read_helper(&value,1);
00196 }
00197
00198 BinaryIStream& read(char* value,std::streamsize n)
00199 {
00200 std::basic_istream<char>::read(value,n);
00201 return *this;
00202 }
00203 BinaryIStream& read(uint8_t* value,std::streamsize n)
00204 {
00205 return read_helper(value,n);
00206 }
00207 BinaryIStream& read(int8_t* value,std::streamsize n)
00208 {
00209 return read_helper(value,n);
00210 }
00211 BinaryIStream& read(int16_t* value,std::streamsize n)
00212 {
00213 return read_helper(value,n);
00214 }
00215 BinaryIStream& read(uint16_t* value,std::streamsize n)
00216 {
00217 return read_helper(value,n);
00218 }
00219 BinaryIStream& read(int32_t* value,std::streamsize n)
00220 {
00221 return read_helper(value,n);
00222 }
00223 BinaryIStream& read(uint32_t* value,std::streamsize n)
00224 {
00225 return read_helper(value,n);
00226 }
00227 BinaryIStream& read(int64_t* value,std::streamsize n)
00228 {
00229 return read_helper(value,n);
00230 }
00231 BinaryIStream& read(uint64_t* value,std::streamsize n)
00232 {
00233 return read_helper(value,n);
00234 }
00235 BinaryIStream& read(float* value,std::streamsize n)
00236 {
00237 return read_helper(value,n);
00238 }
00239 BinaryIStream& read(double* value,std::streamsize n)
00240 {
00241 return read_helper(value,n);
00242 }
00243
00244 protected:
00245 template<typename T>
00246 BinaryIStream& read_helper(T* value,std::streamsize n)
00247 {
00248 std::basic_istream<char>::read(reinterpret_cast<char*>(value),n*sizeof(T));
00249 for(std::streamsize i=0;i<n;++i){
00250 Convert<CONVERSIONTYPE,T>::FromIP(&value[i]);
00251 }
00252 return *this;
00253 }
00254 };
00255
00256
00257
00258 }
00259
00260
00261
00262 }
00263
00264
00265 #endif
00266
00267