aboutsummaryrefslogtreecommitdiff
path: root/cutl/compiler/code-stream.txx
blob: ff7b96d5054f9a85a792c01b35d8cfb1e7b6adf6 (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
// file      : cutl/compiler/code-stream.txx
// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

namespace cutl
{
  namespace compiler
  {
    // code_stream
    //

    template <typename C>
    code_stream<C>::~code_stream ()
    {
    }

    // from_streambuf_adapter
    //

    template <typename C>
    void from_streambuf_adapter<C>::
    put (C c)
    {
      int_type i (stream_.sputc (c));

      if (i == traits_type::eof ())
        throw eof ();
    }

    template <typename C>
    void from_streambuf_adapter<C>::
    unbuffer ()
    {
      if (stream_.pubsync () != 0)
        throw sync ();
    }

    // to_streambuf_adapter
    //

    template <typename C>
    typename to_streambuf_adapter<C>::int_type to_streambuf_adapter<C>::
    overflow (int_type i)
    {
      try
      {
        stream_.put (traits_type::to_char_type (i));
        return i;
      }
      catch (typename from_streambuf_adapter<C>::eof const&)
      {
        return traits_type::eof ();
      }
    }

    template <typename C>
    int to_streambuf_adapter<C>::
    sync ()
    {
      return 0;
    }

    // ostream_filter
    //

    template <template <typename> class S, typename C>
    ostream_filter<S, C>::
    ostream_filter (std::basic_ostream<C>& os)
        : os_ (os),
          prev_ (os_.rdbuf ()),
          from_adapter_ (*prev_),
          stream_ (from_adapter_),
          to_adapter_ (stream_)
    {
      os_.rdbuf (&to_adapter_);
    }

    template <template <typename> class S, typename C>
    ostream_filter<S, C>::
    ~ostream_filter ()
    {
      try
      {
        stream_.unbuffer ();
      }
      catch (...)
      {
        os_.rdbuf (prev_);
      }

      os_.rdbuf (prev_);
    }
  }
}