Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:37

0001 #ifndef PhysicsTools_MVAComputer_memstream_h
0002 #define PhysicsTools_MVAComputer_memstream_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     MVAComputer
0006 //
0007 
0008 //
0009 // Author:  Christophe Saout <christophe.saout@cern.ch>
0010 // Created:     Sat Apr 24 15:18 CEST 2007
0011 //
0012 
0013 #include <iostream>
0014 
0015 namespace ext {
0016 
0017   // provide STL stream to read/write into memory buffers
0018   //
0019   // constructors both take pointer to a buffer and buffer size
0020 
0021   template <typename Item_t, typename Traits_t = std::char_traits<Item_t>, typename Allocator_t = std::allocator<Item_t> >
0022   class basic_omemstream : private std::basic_streambuf<Item_t, Traits_t>, public std::basic_ostream<Item_t, Traits_t> {
0023   public:
0024     typedef typename Traits_t::char_type char_type;
0025     typedef typename Traits_t::int_type int_type;
0026     typedef Traits_t traits_type;
0027 
0028     basic_omemstream(char_type *buf, size_t size)
0029         : std::basic_ostream<Item_t, Traits_t>(this), buffer(buf), cur(buf), last(buf + size) {
0030       this->exceptions(std::ios_base::badbit);
0031     }
0032 
0033     char_type *begin() const { return buffer; }
0034     char_type *end() const { return cur; }
0035     size_t size() const { return cur - buffer; }
0036     bool empty() const { return cur == buffer; }
0037 
0038   private:
0039     std::streamsize xsputn(char_type const *data, std::streamsize size) override {
0040       size_t n = std::min<size_t>(last - cur, size);
0041       traits_type::copy(cur, data, n);
0042       cur += n;
0043       return n;
0044     }
0045 
0046     int_type overflow(int_type c) override {
0047       if (!traits_type::eq_int_type(c, traits_type::eof())) {
0048         char_type t = traits_type::to_char_type(c);
0049         if (xsputn(&t, 1) < 1)
0050           return traits_type::eof();
0051       }
0052 
0053       return c;
0054     }
0055 
0056     int sync() override { return 0; }
0057 
0058     char_type *buffer, *cur, *last;
0059   };
0060 
0061   template <typename Item_t, typename Traits_t = std::char_traits<Item_t>, typename Allocator_t = std::allocator<Item_t> >
0062   class basic_imemstream : private std::basic_streambuf<Item_t, Traits_t>, public std::basic_istream<Item_t, Traits_t> {
0063   public:
0064     typedef typename Traits_t::char_type char_type;
0065     typedef typename Traits_t::int_type int_type;
0066     typedef Traits_t traits_type;
0067 
0068     basic_imemstream(const char_type *buf, size_t size) : std::basic_istream<Item_t, Traits_t>(this) {
0069       this->exceptions(std::ios_base::badbit);
0070       this->setg(const_cast<char_type *>(buf), const_cast<char_type *>(buf), const_cast<char_type *>(buf + size));
0071     }
0072 
0073   private:
0074     int_type underflow() override {
0075       if (this->gptr() && this->gptr() < this->egptr())
0076         return traits_type::to_int_type(*this->gptr());
0077 
0078       return traits_type::eof();
0079     }
0080   };
0081 
0082   typedef basic_omemstream<char> omemstream;
0083   typedef basic_omemstream<wchar_t> womemstream;
0084   typedef basic_imemstream<char> imemstream;
0085   typedef basic_imemstream<wchar_t> wimemstream;
0086 
0087 }  // namespace ext
0088 
0089 #endif  // PhysicsTools_MVAComputer_memstream_h