Debian Codegraph @

cutl/compiler/code-stream.hxx

raw
1 // file : cutl/compiler/code-stream.hxx
2 // copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
3 // license : MIT; see accompanying LICENSE file
4
5 #ifndef CUTL_COMPILER_CODE_STREAM_HXX
6 #define CUTL_COMPILER_CODE_STREAM_HXX
7
8 #include <memory> // std::auto_ptr
9 #include <ostream>
10
11 #include <cutl/exception.hxx>
12
13 namespace cutl
14 {
15 namespace compiler
16 {
17 //
18 //
19 template <typename C>
20 class code_stream
21 {
22 public:
23 code_stream () {}
24
25 virtual
26 ~code_stream ();
27
28 public:
29 virtual void
30 put (C) = 0;
31
32 // Unbuffer flushes internal formatting buffers (if any).
33 // Note that unbuffer is not exactly flushing since it can
34 // result in formatting errors and in general can not be
35 // called at arbitrary points. Natural use case would be
36 // to call unbuffer at the end of the stream when no more
37 // data is expected.
38 //
39 virtual void
40 unbuffer () = 0;
41
42 private:
43 code_stream (code_stream const&);
44
45 code_stream&
46 operator= (code_stream const&);
47 };
48
49 //
50 //
51 template <typename C>
52 class from_streambuf_adapter: public code_stream<C>
53 {
54 public:
55 typedef typename std::basic_streambuf<C>::traits_type traits_type;
56 typedef typename std::basic_streambuf<C>::int_type int_type;
57
58 class eof: exception {};
59 class sync: exception {};
60
61 public:
62 from_streambuf_adapter (std::basic_streambuf<C>& stream)
63 : stream_ (stream)
64 {
65 }
66
67 private:
68 from_streambuf_adapter (from_streambuf_adapter const&);
69
70 from_streambuf_adapter&
71 operator= (from_streambuf_adapter const&);
72
73 public:
74 virtual void
75 put (C c);
76
77 virtual void
78 unbuffer ();
79
80 private:
81 std::basic_streambuf<C>& stream_;
82 };
83
84 //
85 //
86 template <typename C>
87 class to_streambuf_adapter: public std::basic_streambuf<C>
88 {
89 public:
90 typedef typename std::basic_streambuf<C>::traits_type traits_type;
91 typedef typename std::basic_streambuf<C>::int_type int_type;
92
93 public:
94 to_streambuf_adapter (code_stream<C>& stream)
95 : stream_ (stream)
96 {
97 }
98
99 private:
100 to_streambuf_adapter (to_streambuf_adapter const&);
101
102 to_streambuf_adapter&
103 operator= (to_streambuf_adapter const&);
104
105 public:
106 virtual int_type
107 overflow (int_type i);
108
109 // Does nothing since calling unbuffer here would be dangerous.
110 // See the note in code_stream.
111 //
112 virtual int
113 sync ();
114
115 private:
116 code_stream<C>& stream_;
117 };
118
119 //
120 //
121 template <template <typename> class S, typename C>
122 class ostream_filter
123 {
124 public:
125 typedef S<C> stream_type;
126
127 ostream_filter (std::basic_ostream<C>& os);
128 ~ostream_filter ();
129
130 stream_type&
131 stream ()
132 {
133 return stream_;
134 }
135
136 private:
137 ostream_filter (ostream_filter const&);
138
139 ostream_filter&
140 operator= (ostream_filter const&);
141
142 private:
143 std::basic_ostream<C>& os_;
144 std::basic_streambuf<C>* prev_;
145 from_streambuf_adapter<C> from_adapter_;
146 stream_type stream_;
147 to_streambuf_adapter<C> to_adapter_;
148 };
149 }
150 }
151
152 #include <cutl/compiler/code-stream.txx>
153
154 #endif // CUTL_COMPILER_CODE_STREAM_HXX