hobbes
a language, embedded compiler, and runtime for efficient dynamic expression evaluation, data storage and analysis
stream.H
Go to the documentation of this file.
1 
2 #ifndef HOBBES_UTIL_STREAM_HPP_INCLUDED
3 #define HOBBES_UTIL_STREAM_HPP_INCLUDED
4 
5 #include <iostream>
6 #include <vector>
7 
8 namespace hobbes { namespace stream {
9 
10 typedef std::vector<uint8_t> RawData;
11 
12 template < typename Char = char, typename Traits = std::char_traits<Char> >
13  class raw_ostream_buffer : public std::basic_streambuf<Char, Traits> {
14  public:
15  typedef std::basic_streambuf<Char, Traits> BaseT;
16  typedef typename BaseT::int_type int_type;
17  typedef typename BaseT::char_type char_type;
18 
19  raw_ostream_buffer(RawData* d) : d(d) {
20  }
21  private:
22  RawData* d;
23 
24  int_type overflow(int_type c) {
25  if (c != Traits::eof()) {
26  d->push_back((uint8_t)Traits::to_char_type(c));
27  }
28  return Traits::not_eof(c);
29  }
30  };
31 
32 template < typename Char = char, typename Traits = std::char_traits<Char> >
33  class raw_ostream : public std::basic_ostream<Char, Traits> {
34  public:
35  raw_ostream(RawData* d) : std::basic_ostream<Char, Traits>(&buffer), buffer(d) {
36  }
37  private:
39 
40  raw_ostream();
43  };
44 
45 template < typename Char = char, typename Traits = std::char_traits<Char> >
46  class raw_istream_buffer : public std::basic_streambuf<Char, Traits> {
47  public:
48  typedef std::basic_streambuf<Char, Traits> BaseT;
49 
50  raw_istream_buffer(const RawData& d) {
51  if (d.size() == 0) {
52  BaseT::setg(0,0,0);
53  } else {
54  BaseT::setg((Char*)&d[0], (Char*)&d[0], (Char*)(&d[0] + d.size()));
55  }
56  }
57  };
58 
59 template < typename Char = char, typename Traits = std::char_traits<Char> >
60  class raw_istream : public std::basic_istream<Char, Traits> {
61  public:
62  raw_istream(const RawData& d) : std::basic_istream<Char, Traits>(&buffer), buffer(d) {
63  }
64  private:
66 
67  raw_istream();
70  };
71 
72 }}
73 
74 #endif
75 
Definition: expr.H:148
RawData * d
Definition: stream.H:22
std::basic_streambuf< Char, Traits > BaseT
Definition: stream.H:15
raw_istream(const RawData &d)
Definition: stream.H:62
Definition: pattern.H:281
raw_ostream(RawData *d)
Definition: stream.H:35
Definition: stream.H:33
Definition: boot.H:7
raw_istream_buffer< Char, Traits > buffer
Definition: stream.H:65
BaseT::int_type int_type
Definition: stream.H:16
raw_ostream_buffer< Char, Traits > buffer
Definition: stream.H:38
Definition: stream.H:60
raw_ostream_buffer(RawData *d)
Definition: stream.H:19
std::vector< uint8_t > RawData
Definition: stream.H:10
Definition: stream.H:13
int_type overflow(int_type c)
Definition: stream.H:24
BaseT::char_type char_type
Definition: stream.H:17
Definition: stream.H:46
raw_istream_buffer(const RawData &d)
Definition: stream.H:50
std::basic_streambuf< Char, Traits > BaseT
Definition: stream.H:48