CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/PhysicsTools/MVAComputer/interface/memstream.h

Go to the documentation of this file.
00001 #ifndef PhysicsTools_MVAComputer_memstream_h
00002 #define PhysicsTools_MVAComputer_memstream_h
00003 // -*- C++ -*-
00004 //
00005 // Package:     MVAComputer
00006 //
00007 
00008 //
00009 // Author:      Christophe Saout <christophe.saout@cern.ch>
00010 // Created:     Sat Apr 24 15:18 CEST 2007
00011 // $Id: memstream.h,v 1.4 2011/05/23 13:41:03 eulisse Exp $
00012 //
00013 
00014 #include <iostream>
00015 
00016 namespace ext {
00017 
00018 // provide STL stream to read/write into memory buffers
00019 //
00020 // constructors both take pointer to a buffer and buffer size
00021 
00022 template<typename Item_t, typename Traits_t = std::char_traits<Item_t>,
00023          typename Allocator_t = std::allocator<Item_t> >
00024 class basic_omemstream : private std::basic_streambuf<Item_t, Traits_t>,
00025                          public std::basic_ostream<Item_t, Traits_t> {
00026     public:
00027         typedef typename Traits_t::char_type    char_type;
00028         typedef typename Traits_t::int_type     int_type;
00029         typedef Traits_t                        traits_type;
00030 
00031         basic_omemstream(char_type *buf, size_t size) :
00032                         std::basic_ostream<Item_t, Traits_t>(this),
00033                         buffer(buf), cur(buf), last(buf + size)
00034         { this->exceptions(std::ios_base::badbit); }
00035 
00036         char_type* begin() const { return buffer; }
00037         char_type* end() const { return cur; }
00038         size_t size() const { return cur - buffer; }
00039         bool empty() const { return cur == buffer; }
00040 
00041     private:
00042         std::streamsize xsputn(char_type const *data, std::streamsize size) {
00043                 size_t n = std::min<size_t>(last - cur, size);
00044                 traits_type::copy(cur, data, n);
00045                 cur += n;
00046                 return n;
00047         }
00048 
00049         int_type overflow(int_type c)
00050         {
00051                 if (!traits_type::eq_int_type(c, traits_type::eof())) {
00052                         char_type t = traits_type::to_char_type(c);
00053                         if (xsputn(&t, 1) < 1)
00054                                 return traits_type::eof();
00055                 }
00056 
00057                 return c;
00058         }
00059 
00060         int sync() { return 0; }
00061 
00062         char_type       *buffer, *cur, *last;
00063 };
00064 
00065 template<typename Item_t, typename Traits_t = std::char_traits<Item_t>,
00066          typename Allocator_t = std::allocator<Item_t> >
00067 class basic_imemstream : private std::basic_streambuf<Item_t, Traits_t>,
00068                          public std::basic_istream<Item_t, Traits_t> {
00069     public:
00070         typedef typename Traits_t::char_type    char_type;
00071         typedef typename Traits_t::int_type     int_type;
00072         typedef Traits_t                        traits_type;
00073 
00074         basic_imemstream(const char_type *buf, size_t size) :
00075                         std::basic_istream<Item_t, Traits_t>(this)
00076         {
00077                 this->exceptions(std::ios_base::badbit);
00078                 this->setg(const_cast<char_type*>(buf),
00079                      const_cast<char_type*>(buf),
00080                      const_cast<char_type*>(buf + size));
00081         }
00082 
00083     private:
00084         int_type underflow()
00085         {
00086                 if (this->gptr() && this->gptr() < this->egptr())
00087                         return traits_type::to_int_type(*this->gptr());
00088 
00089                 return traits_type::eof();
00090         }
00091 };
00092 
00093 typedef basic_omemstream<char>          omemstream;
00094 typedef basic_omemstream<wchar_t>       womemstream;
00095 typedef basic_imemstream<char>          imemstream;
00096 typedef basic_imemstream<wchar_t>       wimemstream;
00097 
00098 } // namespace ext
00099 
00100 #endif // PhysicsTools_MVAComputer_memstream_h