aboutsummaryrefslogtreecommitdiff
path: root/libxsd/xsd/cxx/zc-istream.hxx
blob: f7838465c95715cb6648a3d894e0e8522ea114a3 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// file      : xsd/cxx/zc-istream.hxx
// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#ifndef XSD_CXX_ZC_ISTREAM_HXX
#define XSD_CXX_ZC_ISTREAM_HXX

#include <string>
#include <istream>

#include <xsd/cxx/ro-string.hxx>

namespace xsd
{
  namespace cxx
  {
    // Input streambuffer that does not copy the underlying
    // buffer (zero copy).
    //
    template <typename C>
    class zc_streambuf: public std::basic_streambuf<C>
    {
    public:
      typedef typename std::basic_streambuf<C>::int_type int_type;
      typedef typename std::basic_streambuf<C>::traits_type traits_type;

    public:
      zc_streambuf (const ro_string<C>&);
      zc_streambuf (const std::basic_string<C>&);

    protected:
      virtual std::streamsize
      showmanyc ();

      virtual int_type
      underflow ();

    private:
      void
      init ();

    private:
      zc_streambuf (const zc_streambuf&);

      zc_streambuf&
      operator= (const zc_streambuf&);

    private:
      ro_string<C> str_;
    };


    // Input string stream that does not copy the underlying string.
    //
    template <typename C>
    class zc_istream_base
    {
    protected:
      zc_istream_base (const ro_string<C>&);
      zc_istream_base (const std::basic_string<C>&);

    protected:
      zc_streambuf<C> buf_;
    };

    template <typename C>
    class zc_istream: protected zc_istream_base<C>,
                      public std::basic_istream<C>
    {
      typedef std::basic_istream<C> base;

    public:
      zc_istream (const ro_string<C>&);
      zc_istream (const std::basic_string<C>&);

      bool
      exhausted ()
      {
        return this->get () == std::basic_istream<C>::traits_type::eof ();
      }

      zc_istream&
      operator>> (unsigned char& x)
      {
        if (check_unsigned ())
          static_cast<base&> (*this) >> x;

        return *this;
      }

      zc_istream&
      operator>> (signed char& x)
      {
        if (check_signed ())
          static_cast<base&> (*this) >> x;

        return *this;
      }

      zc_istream&
      operator>> (unsigned short& x)
      {
        if (check_unsigned ())
          static_cast<base&> (*this) >> x;

        return *this;
      }

      zc_istream&
      operator>> (short& x)
      {
        if (check_signed ())
          static_cast<base&> (*this) >> x;

        return *this;
      }

      zc_istream&
      operator>> (unsigned int& x)
      {
        if (check_unsigned ())
          static_cast<base&> (*this) >> x;

        return *this;
      }

      zc_istream&
      operator>> (int& x)
      {
        if (check_signed ())
          static_cast<base&> (*this) >> x;

        return *this;
      }

      zc_istream&
      operator>> (unsigned long& x)
      {
        if (check_unsigned ())
          static_cast<base&> (*this) >> x;

        return *this;
      }

      zc_istream&
      operator>> (long& x)
      {
        if (check_signed ())
          static_cast<base&> (*this) >> x;

        return *this;
      }

      zc_istream&
      operator>> (unsigned long long& x)
      {
        if (check_unsigned ())
          static_cast<base&> (*this) >> x;

        return *this;
      }

      zc_istream&
      operator>> (long long& x)
      {
        if (check_signed ())
          static_cast<base&> (*this) >> x;

        return *this;
      }

      template <typename X>
      zc_istream&
      operator>> (X& x)
      {
        static_cast<base&> (*this) >> x;
        return *this;
      }

    private:
      bool
      check_signed ()
      {
        typename std::basic_istream<C>::traits_type::int_type p (this->peek ());
        bool r ((p >= C ('0') && p <= C ('9')) || p == C ('-') || p == C ('+'));

        if (!r)
          this->setstate (std::ios_base::failbit);

        return r;
      }

      bool
      check_unsigned ()
      {
        typename std::basic_istream<C>::traits_type::int_type p (this->peek ());
        bool r ((p >= C ('0') && p <= C ('9')) || p == C ('+'));

        if (!r)
          this->setstate (std::ios_base::failbit);

        return r;
      }

    private:
      zc_istream (const zc_istream&);

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

#include <xsd/cxx/zc-istream.txx>

#endif  // XSD_CXX_ZC_ISTREAM_HXX