CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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>,
22  typename Allocator_t = std::allocator<Item_t> >
23 class basic_omemstream : private std::basic_streambuf<Item_t, Traits_t>,
24  public std::basic_ostream<Item_t, Traits_t> {
25  public:
26  typedef typename Traits_t::char_type char_type;
27  typedef typename Traits_t::int_type int_type;
28  typedef Traits_t traits_type;
29 
31  std::basic_ostream<Item_t, Traits_t>(this),
32  buffer(buf), cur(buf), last(buf + size)
33  { this->exceptions(std::ios_base::badbit); }
34 
35  char_type* begin() const { return buffer; }
36  char_type* end() const { return cur; }
37  size_t size() const { return cur - buffer; }
38  bool empty() const { return cur == buffer; }
39 
40  private:
41  std::streamsize xsputn(char_type const *data, std::streamsize size) {
42  size_t n = std::min<size_t>(last - cur, size);
43  traits_type::copy(cur, data, n);
44  cur += n;
45  return n;
46  }
47 
49  {
50  if (!traits_type::eq_int_type(c, traits_type::eof())) {
51  char_type t = traits_type::to_char_type(c);
52  if (xsputn(&t, 1) < 1)
53  return traits_type::eof();
54  }
55 
56  return c;
57  }
58 
59  int sync() { return 0; }
60 
62 };
63 
64 template<typename Item_t, typename Traits_t = std::char_traits<Item_t>,
65  typename Allocator_t = std::allocator<Item_t> >
66 class basic_imemstream : private std::basic_streambuf<Item_t, Traits_t>,
67  public std::basic_istream<Item_t, Traits_t> {
68  public:
69  typedef typename Traits_t::char_type char_type;
70  typedef typename Traits_t::int_type int_type;
71  typedef Traits_t traits_type;
72 
73  basic_imemstream(const char_type *buf, size_t size) :
74  std::basic_istream<Item_t, Traits_t>(this)
75  {
76  this->exceptions(std::ios_base::badbit);
77  this->setg(const_cast<char_type*>(buf),
78  const_cast<char_type*>(buf),
79  const_cast<char_type*>(buf + size));
80  }
81 
82  private:
84  {
85  if (this->gptr() && this->gptr() < this->egptr())
86  return traits_type::to_int_type(*this->gptr());
87 
88  return traits_type::eof();
89  }
90 };
91 
96 
97 } // namespace ext
98 
99 #endif // PhysicsTools_MVAComputer_memstream_h
Traits_t::char_type char_type
Definition: memstream.h:69
char_type * buffer
Definition: memstream.h:61
basic_imemstream(const char_type *buf, size_t size)
Definition: memstream.h:73
int_type overflow(int_type c)
Definition: memstream.h:48
Traits_t::char_type char_type
Definition: memstream.h:26
basic_imemstream< wchar_t > wimemstream
Definition: memstream.h:95
Traits_t::int_type int_type
Definition: memstream.h:27
char_type * end() const
Definition: memstream.h:36
basic_omemstream< char > omemstream
Definition: memstream.h:92
basic_omemstream< wchar_t > womemstream
Definition: memstream.h:93
int_type underflow()
Definition: memstream.h:83
basic_omemstream(char_type *buf, size_t size)
Definition: memstream.h:30
basic_imemstream< char > imemstream
Definition: memstream.h:94
Traits_t traits_type
Definition: memstream.h:28
bool empty() const
Definition: memstream.h:38
char_type * cur
Definition: memstream.h:61
Traits_t traits_type
Definition: memstream.h:71
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
char_type * last
Definition: memstream.h:61
Traits_t::int_type int_type
Definition: memstream.h:70
std::streamsize xsputn(char_type const *data, std::streamsize size)
Definition: memstream.h:41
char_type * begin() const
Definition: memstream.h:35
tuple size
Write out results.
size_t size() const
Definition: memstream.h:37