data_io.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) Sergey P. Derevyago, 2008.
00003  *
00004  * Permission to copy, use, modify, sell and distribute this software is granted
00005  * provided this copyright notice appears in all copies.
00006  * This software is provided "as is" without express or implied warranty, and
00007  * with no claim as to its suitability for any purpose.
00008  *
00009  */
00010 
00015 #include <ders/data_io.hpp>
00016 #include <stdexcept>
00017 
00018 namespace {
00019 
00020 using namespace ders;
00021 using namespace std;
00022 
00023 class cb_dio : public data_io {
00024       sh_cycl_buf cb;
00025 
00026       bool read(void* ptr, int n, bool noeof);
00027       void write(const void* ptr, int n) { cb->put(ptr, n); }
00028 
00029       template<class T>
00030       bool read(T& val) { return read(&val, sizeof(T), false); }
00031       template<class T>
00032       void write(T val) { write(&val, sizeof(T)); }
00033 
00034  public:
00035       cb_dio(sh_cycl_buf c) : cb(c) {}
00036 
00037       virtual void destroy(mem_pool& mp2) { destroy_this(this, mp2); }
00038 
00039       virtual bool read_bool(bool& val);
00040       virtual bool read_char(char& val);
00041       virtual bool read_schar(signed char& val);
00042       virtual bool read_uchar(unsigned char& val);
00043       virtual bool read_wchar(wchar_t& val);
00044       virtual bool read_short(short& val);
00045       virtual bool read_ushort(unsigned short& val);
00046       virtual bool read_int(int& val);
00047       virtual bool read_uint(unsigned int& val);
00048       virtual bool read_long(long& val);
00049       virtual bool read_ulong(unsigned long& val);
00050       virtual bool read_float(float& val);
00051       virtual bool read_double(double& val);
00052       virtual bool read_ldouble(long double& val);
00053       virtual bool read_ptr(void*& val);
00054       virtual bool read_text(sh_text& val);
00055 
00056       virtual void write_bool(bool val);
00057       virtual void write_char(char val);
00058       virtual void write_schar(signed char val);
00059       virtual void write_uchar(unsigned char val);
00060       virtual void write_wchar(wchar_t val);
00061       virtual void write_short(short val);
00062       virtual void write_ushort(unsigned short val);
00063       virtual void write_int(int val);
00064       virtual void write_uint(unsigned int val);
00065       virtual void write_long(long val);
00066       virtual void write_ulong(unsigned long val);
00067       virtual void write_float(float val);
00068       virtual void write_double(double val);
00069       virtual void write_ldouble(long double val);
00070       virtual void write_ptr(void* val);
00071       virtual void write_text(const ch_rng& val);
00072 };
00073 
00074 bool cb_dio::read(void* ptr, int n, bool noeof)
00075 {
00076  if (cb->size()<n) {
00077     if (cb->size()==0 && !noeof) return false;
00078 
00079     // no thread-safe mem_pool object to throw shException at this point
00080     throw runtime_error("Not enough bytes in data_io");
00081  }
00082 
00083  cb->get(ptr, n);
00084  return true;
00085 }
00086 
00087 bool cb_dio::read_bool(bool& val)
00088 {
00089  return read(val);
00090 }
00091 
00092 bool cb_dio::read_char(char& val)
00093 {
00094  return read(val);
00095 }
00096 
00097 bool cb_dio::read_schar(signed char& val)
00098 {
00099  return read(val);
00100 }
00101 
00102 bool cb_dio::read_uchar(unsigned char& val)
00103 {
00104  return read(val);
00105 }
00106 
00107 bool cb_dio::read_wchar(wchar_t& val)
00108 {
00109  return read(val);
00110 }
00111 
00112 bool cb_dio::read_short(short& val)
00113 {
00114  return read(val);
00115 }
00116 
00117 bool cb_dio::read_ushort(unsigned short& val)
00118 {
00119  return read(val);
00120 }
00121 
00122 bool cb_dio::read_int(int& val)
00123 {
00124  return read(val);
00125 }
00126 
00127 bool cb_dio::read_uint(unsigned int& val)
00128 {
00129  return read(val);
00130 }
00131 
00132 bool cb_dio::read_long(long& val)
00133 {
00134  return read(val);
00135 }
00136 
00137 bool cb_dio::read_ulong(unsigned long& val)
00138 {
00139  return read(val);
00140 }
00141 
00142 bool cb_dio::read_float(float& val)
00143 {
00144  return read(val);
00145 }
00146 
00147 bool cb_dio::read_double(double& val)
00148 {
00149  return read(val);
00150 }
00151 
00152 bool cb_dio::read_ldouble(long double& val)
00153 {
00154  return read(val);
00155 }
00156 
00157 bool cb_dio::read_ptr(void*& val)
00158 {
00159  return read(val);
00160 }
00161 
00162 bool cb_dio::read_text(sh_text& val)
00163 {
00164  int sz;
00165  if (!read(sz)) return false;
00166 
00167  val->uninitialized_resize(sz);
00168  read(val->begin(), sz, true);
00169 
00170  return true;
00171 }
00172 
00173 void cb_dio::write_bool(bool val)
00174 {
00175  write(val);
00176 }
00177 
00178 void cb_dio::write_char(char val)
00179 {
00180  write(val);
00181 }
00182 
00183 void cb_dio::write_schar(signed char val)
00184 {
00185  write(val);
00186 }
00187 
00188 void cb_dio::write_uchar(unsigned char val)
00189 {
00190  write(val);
00191 }
00192 
00193 void cb_dio::write_wchar(wchar_t val)
00194 {
00195  write(val);
00196 }
00197 
00198 void cb_dio::write_short(short val)
00199 {
00200  write(val);
00201 }
00202 
00203 void cb_dio::write_ushort(unsigned short val)
00204 {
00205  write(val);
00206 }
00207 
00208 void cb_dio::write_int(int val)
00209 {
00210  write(val);
00211 }
00212 
00213 void cb_dio::write_uint(unsigned int val)
00214 {
00215  write(val);
00216 }
00217 
00218 void cb_dio::write_long(long val)
00219 {
00220  write(val);
00221 }
00222 
00223 void cb_dio::write_ulong(unsigned long val)
00224 {
00225  write(val);
00226 }
00227 
00228 void cb_dio::write_float(float val)
00229 {
00230  write(val);
00231 }
00232 
00233 void cb_dio::write_double(double val)
00234 {
00235  write(val);
00236 }
00237 
00238 void cb_dio::write_ldouble(long double val)
00239 {
00240  write(val);
00241 }
00242 
00243 void cb_dio::write_ptr(void* val)
00244 {
00245  write(val);
00246 }
00247 
00248 void cb_dio::write_text(const ch_rng& val)
00249 {
00250  int sz=val.size();
00251  write(sz);
00252  write(val.beg, sz);
00253 }
00254 
00255 }  // unnamed
00256 
00257 namespace ders {  // ::ders
00258 
00259 sh_data_io new_data_io(sh_cycl_buf cb)
00260 {
00261  mp_newbuf<cb_dio> buf(cb.pool());
00262  return sh_data_io(buf.pool(), buf.rls(::new(buf.get()) cb_dio(cb)));
00263 }
00264 
00265 }  // namespace ::ders
00266 

Generated on Tue Dec 8 11:35:32 2009 for derslib by  doxygen 1.5.5