aboutsummaryrefslogtreecommitdiff
path: root/cutl/compiler/code-stream.hxx
blob: 4b792445dbe5dcc3725b102f3781383b4d7bf0b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// file      : cutl/compiler/code-stream.hxx
// copyright : Copyright (c) 2009-2017 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#ifndef CUTL_COMPILER_CODE_STREAM_HXX
#define CUTL_COMPILER_CODE_STREAM_HXX

#include <memory>  // std::auto_ptr
#include <ostream>

#include <cutl/exception.hxx>

namespace cutl
{
  namespace compiler
  {
    //
    //
    template <typename C>
    class code_stream
    {
    public:
      code_stream () {}

      virtual
      ~code_stream ();

    public:
      virtual void
      put (C) = 0;

      // Unbuffer flushes internal formatting buffers (if any).
      // Note that unbuffer is not exactly flushing since it can
      // result in formatting errors and in general can not be
      // called at arbitrary points. Natural use case would be
      // to call unbuffer at the end of the stream when no more
      // data is expected.
      //
      virtual void
      unbuffer () = 0;

    private:
      code_stream (code_stream const&);

      code_stream&
      operator= (code_stream const&);
    };

    //
    //
    template <typename C>
    class from_streambuf_adapter: public code_stream<C>
    {
    public:
      typedef typename std::basic_streambuf<C>::traits_type traits_type;
      typedef typename std::basic_streambuf<C>::int_type int_type;

      class eof: exception {};
      class sync: exception {};

    public:
      from_streambuf_adapter (std::basic_streambuf<C>& stream)
          : stream_ (stream)
      {
      }

    private:
      from_streambuf_adapter (from_streambuf_adapter const&);

      from_streambuf_adapter&
      operator= (from_streambuf_adapter const&);

    public:
      virtual void
      put (C c);

      virtual void
      unbuffer ();

    private:
      std::basic_streambuf<C>& stream_;
    };

    //
    //
    template <typename C>
    class to_streambuf_adapter: public std::basic_streambuf<C>
    {
    public:
      typedef typename std::basic_streambuf<C>::traits_type traits_type;
      typedef typename std::basic_streambuf<C>::int_type int_type;

    public:
      to_streambuf_adapter (code_stream<C>& stream)
          : stream_ (stream)
      {
      }

    private:
      to_streambuf_adapter (to_streambuf_adapter const&);

      to_streambuf_adapter&
      operator= (to_streambuf_adapter const&);

    public:
      virtual int_type
      overflow (int_type i);

      // Does nothing since calling unbuffer here would be dangerous.
      // See the note in code_stream.
      //
      virtual int
      sync ();

    private:
      code_stream<C>& stream_;
    };

    //
    //
    template <template <typename> class S, typename C>
    class ostream_filter
    {
    public:
      typedef S<C> stream_type;

      ostream_filter (std::basic_ostream<C>& os);
      ~ostream_filter () /*noexcept (false)*/;

      stream_type&
      stream ()
      {
        return stream_;
      }

    private:
      ostream_filter (ostream_filter const&);

      ostream_filter&
      operator= (ostream_filter const&);

    private:
      std::basic_ostream<C>& os_;
      std::basic_streambuf<C>* prev_;
      from_streambuf_adapter<C> from_adapter_;
      stream_type stream_;
      to_streambuf_adapter<C> to_adapter_;
    };
  }
}

#include <cutl/compiler/code-stream.txx>

#endif // CUTL_COMPILER_CODE_STREAM_HXX