AFEPack
BinaryBuffer.h
浏览该文件的文档。
00001 
00011 #ifndef __BinaryBuffer_h__
00012 #define __BinaryBuffer_h__
00013 
00014 #include <vector>
00015 #include "Geometry.h"
00016 
00017 template <typename CHAR = char,
00018   template <typename T, typename ALLOC = std::allocator<T> > class VECTOR = std::vector>
00019   class BinaryBuffer : public VECTOR<CHAR> {
00020  public:
00021  typedef CHAR char_t;
00022  typedef VECTOR<CHAR> _Base;
00023  typedef typename _Base::iterator iterator;
00024  typedef typename _Base::const_iterator const_iterator;
00025 
00026  BinaryBuffer() : _Base() {}
00027  ~BinaryBuffer() {}
00028 
00032  void * start_address() const { return (void *)&(*this)[0]; }
00033 
00034 };
00035 
00036 namespace AFEPack {
00037 
00038   template <typename BUFFER>
00039     class stream_base {
00040   protected:
00041     typedef typename BUFFER::char_t char_t;
00042     typedef typename BUFFER::const_iterator const_iterator;
00043   public:
00045   stream_base() : _buf(NULL) {}
00047   stream_base(BUFFER& buf) : _buf(&buf) {}
00049     ~stream_base() {}
00050 
00051   public:
00057     void set_buffer(BUFFER& buf) { _buf = &buf; }
00058 
00063     const BUFFER& get_buffer() const { return *_buf; }
00064 
00065   protected:
00067     BUFFER * _buf;
00068   };
00069 
00071   template <typename BUFFER = BinaryBuffer<> >
00072     class ostream : public stream_base<BUFFER>{
00073   private:
00074   typedef stream_base<BUFFER> _Base;
00075   typedef typename _Base::char_t char_t;
00076   using _Base::_buf;
00077   public:
00078   ostream() : _Base() {}
00079   ostream(BUFFER& buf) : _Base(buf) {}
00080   ~ostream() {}
00081 
00088   template <class T>
00089   void encode(const T& t) {
00090     int n = sizeof(t);
00091     const char_t * ptr = (const char_t *) (&t);
00092     _buf->insert(_buf->end(), ptr, ptr + n);
00093   }
00094 
00095   void encode_binary(void * data, int n) {
00096     const char_t * ptr = (const char_t *)data;
00097     _buf->insert(_buf->end(), ptr, ptr + n); 
00098   }
00099   };
00100 
00102   template <typename BUFFER = BinaryBuffer<> >
00103     class istream : public stream_base<const BUFFER>{
00104   private:
00105   typedef stream_base<const BUFFER> _Base;
00106   typedef typename _Base::char_t char_t;
00107   typedef typename _Base::const_iterator iterator;
00108   using _Base::_buf;
00109   public:
00110   istream() : _Base() {}
00111   istream(const BUFFER& buf) : _Base(buf) { reset(); }
00112   ~istream() {}
00113 
00115   void set_buffer(const BUFFER& buf) {
00116     _Base::set_buffer(buf);
00117     reset();
00118   }
00119 
00126   template <class T>
00127   void decode(T& t) {
00128     std::size_t n = sizeof(T);
00129     std::copy(_pos, _pos + n, (char_t *)(&t));
00130     _pos += n;
00131   }
00132   void decode_binary(void * data, int n) {
00133     std::copy(_pos, _pos + n, (char_t *)data);
00134     _pos += n;
00135   }
00136 
00137   private:
00142   void reset() {
00143     if (_buf != NULL) {
00144       _pos = _buf->begin();
00145     }
00146   }
00147 
00148   private:
00150   iterator _pos;
00151   }; 
00152   
00153   template <class OSTREAM, class T>
00154     void encode(OSTREAM& os, const T& t) {
00155     os.encode(t);
00156   }
00157   template <class ISTREAM, class T>
00158     void decode(ISTREAM& is, T& t) {
00159     is.decode(t);
00160   }
00161 
00162   template <class OSTREAM, class T>
00163     void encode_binary(OSTREAM& os, const T& t) {
00164     os.encode_binary(t);
00165   }
00166   template <class ISTREAM, class T>
00167     void decode_binary(ISTREAM& is, T& t) {
00168     is.decode_binary(t);
00169   }
00170 
00171   template <class OSTREAM, class T>
00172     OSTREAM& operator<<(OSTREAM& os, const T& t){
00173     os.encode(t);
00174     return os;
00175   }
00176   template <class ISTREAM, class T>
00177     ISTREAM& operator>>(ISTREAM& is, T& t){
00178     is.decode(t);
00179     return is;
00180   }
00181 
00182 
00183 
00184   template <class BUF, class T>
00185     ostream<BUF>& operator<<(ostream<BUF>& os, const std::vector<T>& t) {
00186     u_int n = t.size();
00187     os << n;
00188     for (u_int i = 0;i < n;++ i) {
00189       os << t[i];
00190     }
00191     return os;
00192   }
00193   template <class BUF, class T>
00194     istream<BUF>& operator>>(istream<BUF>& is, std::vector<T>& t) {
00195     u_int n;
00196     is >> n;
00197     t.resize(n);
00198     for (u_int i = 0;i < n;++ i) {
00199       is >> t[i];
00200     }
00201     return is;
00202   }
00203 
00204   template <class BUF>
00205     ostream<BUF>& operator<<(ostream<BUF>& os, const std::vector<double>& t) {
00206     u_int n = t.size();
00207     os << n;
00208     os.encode_binary((void *)(&t[0]), sizeof(double)*n);
00209     return os;
00210   }
00211   template <class BUF>
00212     istream<BUF>& operator>>(istream<BUF>& is, std::vector<double>& t) {
00213     u_int n;
00214     is >> n;
00215     t.resize(n);
00216     is.decode_binary(&t[0], sizeof(double)*n);
00217     return is;
00218   }
00219 
00220   template <class BUF>
00221     ostream<BUF>& operator<<(ostream<BUF>& os, const std::vector<int>& t) {
00222     u_int n = t.size();
00223     os << n;
00224     os.encode_binary((void *)(&t[0]), sizeof(int)*n);
00225     return os;
00226   }
00227   template <class BUF>
00228     istream<BUF>& operator>>(istream<BUF>& is, std::vector<int>& t) {
00229     u_int n;
00230     is >> n;
00231     t.resize(n);
00232     is.decode_binary(&t[0], sizeof(int)*n);
00233     return is;
00234   }
00235 
00236   template <class BUF>
00237     ostream<BUF>& operator<<(ostream<BUF>& os, const std::vector<u_int>& t) {
00238     u_int n = t.size();
00239     os << n;
00240     os.encode_binary((void *)(&t[0]), sizeof(u_int)*n);
00241     return os;
00242   }
00243   template <class BUF>
00244     istream<BUF>& operator>>(istream<BUF>& is, std::vector<u_int>& t) {
00245     u_int n;
00246     is >> n;
00247     t.resize(n);
00248     is.decode_binary(&t[0], sizeof(u_int)*n);
00249     return is;
00250   }
00251 
00252   template <class BUF>
00253     ostream<BUF>& operator<<(ostream<BUF>& os, const Vector<double>& t) {
00254     u_int n = t.size();
00255     os << n;
00256     os.encode_binary((void *)(&(*t.begin())), sizeof(double)*n);
00257     return os;
00258   }
00259   template <class BUF>
00260     istream<BUF>& operator>>(istream<BUF>& is, Vector<double>& t) {
00261     u_int n;
00262     is >> n;
00263     t.reinit(n);
00264     is.decode_binary(&t(0), sizeof(double)*n);
00265     return is;
00266   }
00267 
00268   template <class BUF, int DIM>
00269     ostream<BUF>& operator<<(ostream<BUF>& os, const afepack::Point<DIM>& t) {
00270     os.encode_binary((void *)(&t[0]), sizeof(double)*DIM);
00271     return os;
00272   }
00273   template <class BUF, int DIM>
00274     istream<BUF>& operator>>(istream<BUF>& is, afepack::Point<DIM>& t) {
00275     is.decode_binary(&t[0], sizeof(double)*DIM);
00276     return is;
00277   }
00278 
00279   template <class BUF, typename CHAR, template <typename T = char, 
00280     typename ALLOC = std::allocator<T> > class VEC>
00281     ostream<BUF>& operator<<(ostream<BUF>& os, const BinaryBuffer<CHAR,VEC>& buf) {
00282     const VEC<CHAR>& v(buf);
00283     os << v;
00284     return os;
00285   }
00286   template <class BUF, typename CHAR, template <typename T = char, 
00287     typename ALLOC = std::allocator<T> > class VEC>
00288     istream<BUF>& operator>>(istream<BUF>& is, BinaryBuffer<CHAR,VEC>& buf) {
00289     VEC<CHAR>& v(buf);
00290     is >> v;
00291     return is;
00292   }
00293 }
00294 
00295 #endif // __BinaryBuffer_h__
00296