CMS 3D CMS Logo

memstream.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_MVAComputer_memstream_h
2 #define PhysicsTools_MVAComputer_memstream_h
3 // -*- C++ -*-
4 //
5 // Package: MVAComputer
6 //
7 
8 //
9 // Author: Christophe Saout <christophe.saout@cern.ch>
10 // Created: Sat Apr 24 15:18 CEST 2007
11 //
12 
13 #include <iostream>
14 
15 namespace ext {
16 
17  // provide STL stream to read/write into memory buffers
18  //
19  // constructors both take pointer to a buffer and buffer size
20 
21  template <typename Item_t, typename Traits_t = std::char_traits<Item_t>, typename Allocator_t = std::allocator<Item_t> >
22  class basic_omemstream : private std::basic_streambuf<Item_t, Traits_t>, public std::basic_ostream<Item_t, Traits_t> {
23  public:
24  typedef typename Traits_t::char_type char_type;
25  typedef typename Traits_t::int_type int_type;
26  typedef Traits_t traits_type;
27 
28  basic_omemstream(char_type *buf, size_t size)
29  : std::basic_ostream<Item_t, Traits_t>(this), buffer(buf), cur(buf), last(buf + size) {
30  this->exceptions(std::ios_base::badbit);
31  }
32 
33  char_type *begin() const { return buffer; }
34  char_type *end() const { return cur; }
35  size_t size() const { return cur - buffer; }
36  bool empty() const { return cur == buffer; }
37 
38  private:
39  std::streamsize xsputn(char_type const *data, std::streamsize size) override {
40  size_t n = std::min<size_t>(last - cur, size);
41  traits_type::copy(cur, data, n);
42  cur += n;
43  return n;
44  }
45 
46  int_type overflow(int_type c) override {
47  if (!traits_type::eq_int_type(c, traits_type::eof())) {
48  char_type t = traits_type::to_char_type(c);
49  if (xsputn(&t, 1) < 1)
50  return traits_type::eof();
51  }
52 
53  return c;
54  }
55 
56  int sync() override { return 0; }
57 
58  char_type *buffer, *cur, *last;
59  };
60 
61  template <typename Item_t, typename Traits_t = std::char_traits<Item_t>, typename Allocator_t = std::allocator<Item_t> >
62  class basic_imemstream : private std::basic_streambuf<Item_t, Traits_t>, public std::basic_istream<Item_t, Traits_t> {
63  public:
64  typedef typename Traits_t::char_type char_type;
65  typedef typename Traits_t::int_type int_type;
66  typedef Traits_t traits_type;
67 
68  basic_imemstream(const char_type *buf, size_t size) : std::basic_istream<Item_t, Traits_t>(this) {
69  this->exceptions(std::ios_base::badbit);
70  this->setg(const_cast<char_type *>(buf), const_cast<char_type *>(buf), const_cast<char_type *>(buf + size));
71  }
72 
73  private:
74  int_type underflow() override {
75  if (this->gptr() && this->gptr() < this->egptr())
76  return traits_type::to_int_type(*this->gptr());
77 
78  return traits_type::eof();
79  }
80  };
81 
86 
87 } // namespace ext
88 
89 #endif // PhysicsTools_MVAComputer_memstream_h
Traits_t::char_type char_type
Definition: memstream.h:64
char_type * buffer
Definition: memstream.h:58
basic_imemstream(const char_type *buf, size_t size)
Definition: memstream.h:68
int_type overflow(int_type c) override
Definition: memstream.h:46
Traits_t::char_type char_type
Definition: memstream.h:24
basic_imemstream< wchar_t > wimemstream
Definition: memstream.h:85
int_type underflow() override
Definition: memstream.h:74
Traits_t::int_type int_type
Definition: memstream.h:25
char_type * end() const
Definition: memstream.h:34
basic_omemstream< char > omemstream
Definition: memstream.h:82
basic_omemstream< wchar_t > womemstream
Definition: memstream.h:83
int sync() override
Definition: memstream.h:56
std::streamsize xsputn(char_type const *data, std::streamsize size) override
Definition: memstream.h:39
basic_omemstream(char_type *buf, size_t size)
Definition: memstream.h:28
basic_imemstream< char > imemstream
Definition: memstream.h:84
Traits_t traits_type
Definition: memstream.h:26
bool empty() const
Definition: memstream.h:36
char_type * cur
Definition: memstream.h:58
Traits_t traits_type
Definition: memstream.h:66
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
char_type * last
Definition: memstream.h:58
Traits_t::int_type int_type
Definition: memstream.h:65
Definition: memstream.h:15
char_type * begin() const
Definition: memstream.h:33
size_t size() const
Definition: memstream.h:35