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 // $Id: memstream.h,v 1.2 2007/05/25 16:37:58 saout Exp $
12 //
13 
14 #include <iostream>
15 
16 namespace ext {
17 
18 // provide STL stream to read/write into memory buffers
19 //
20 // constructors both take pointer to a buffer and buffer size
21 
22 template<typename Item_t, typename Traits_t = std::char_traits<Item_t>,
23  typename Allocator_t = std::allocator<Item_t> >
24 class basic_omemstream : private std::basic_streambuf<Item_t, Traits_t>,
25  public std::basic_ostream<Item_t, Traits_t> {
26  public:
27  typedef typename Traits_t::char_type char_type;
28  typedef typename Traits_t::int_type int_type;
29  typedef Traits_t traits_type;
30 
32  std::basic_ostream<Item_t, Traits_t>(this),
33  buffer(buf), cur(buf), last(buf + size)
34  { this->exceptions(std::ios_base::badbit); }
35 
36  char_type* begin() const { return buffer; }
37  char_type* end() const { return cur; }
38  size_t size() const { return cur - buffer; }
39  bool empty() const { return cur == buffer; }
40 
41  private:
42  std::streamsize xsputn(char_type const *data, std::streamsize size) {
43  size_t n = std::min<size_t>(last - cur, size);
44  traits_type::copy(cur, data, n);
45  cur += n;
46  return n;
47  }
48 
50  {
51  if (!traits_type::eq_int_type(c, traits_type::eof())) {
52  char_type t = traits_type::to_char_type(c);
53  if (xsputn(&t, 1) < 1)
54  return traits_type::eof();
55  }
56 
57  return c;
58  }
59 
60  int sync() { return 0; }
61 
63 };
64 
65 template<typename Item_t, typename Traits_t = std::char_traits<Item_t>,
66  typename Allocator_t = std::allocator<Item_t> >
67 class basic_imemstream : private std::basic_streambuf<Item_t, Traits_t>,
68  public std::basic_istream<Item_t, Traits_t> {
69  public:
70  typedef typename Traits_t::char_type char_type;
71  typedef typename Traits_t::int_type int_type;
72  typedef Traits_t traits_type;
73 
74  basic_imemstream(const char_type *buf, size_t size) :
75  std::basic_istream<Item_t, Traits_t>(this)
76  {
77  this->exceptions(std::ios_base::badbit);
78  setg(const_cast<char_type*>(buf),
79  const_cast<char_type*>(buf),
80  const_cast<char_type*>(buf + size));
81  }
82 
83  private:
85  {
86  if (this->gptr() && this->gptr() < this->egptr())
87  return traits_type::to_int_type(*this->gptr());
88 
89  return traits_type::eof();
90  }
91 };
92 
97 
98 } // namespace ext
99 
100 #endif // PhysicsTools_MVAComputer_memstream_h
Traits_t::char_type char_type
Definition: memstream.h:70
char_type * buffer
Definition: memstream.h:62
basic_imemstream(const char_type *buf, size_t size)
Definition: memstream.h:74
int_type overflow(int_type c)
Definition: memstream.h:49
Traits_t::char_type char_type
Definition: memstream.h:27
basic_imemstream< wchar_t > wimemstream
Definition: memstream.h:96
Traits_t::int_type int_type
Definition: memstream.h:28
char_type * end() const
Definition: memstream.h:37
basic_omemstream< char > omemstream
Definition: memstream.h:93
basic_omemstream< wchar_t > womemstream
Definition: memstream.h:94
int_type underflow()
Definition: memstream.h:84
basic_omemstream(char_type *buf, size_t size)
Definition: memstream.h:31
basic_imemstream< char > imemstream
Definition: memstream.h:95
Traits_t traits_type
Definition: memstream.h:29
bool empty() const
Definition: memstream.h:39
char_type * cur
Definition: memstream.h:62
Traits_t traits_type
Definition: memstream.h:72
char_type * last
Definition: memstream.h:62
Traits_t::int_type int_type
Definition: memstream.h:71
std::streamsize xsputn(char_type const *data, std::streamsize size)
Definition: memstream.h:42
char_type * begin() const
Definition: memstream.h:36
tuple size
Write out results.
size_t size() const
Definition: memstream.h:38