L4Re - L4 Runtime Environment
basic_ostream
Go to the documentation of this file.
1 // vi:set ft=cpp: -*- Mode: C++ -*-
2 /**
3  * \file
4  * \brief Basic IO stream
5  */
6 /*
7  * (c) 2009 Alexander Warg <warg@os.inf.tu-dresden.de>
8  * economic rights: Technische Universit├Ąt Dresden (Germany)
9  *
10  * This file is part of TUD:OS and distributed under the terms of the
11  * GNU General Public License 2.
12  * Please see the COPYING-GPL-2 file for details.
13  *
14  * As a special exception, you may use this file as part of a free software
15  * library without restriction. Specifically, if other files instantiate
16  * templates or use macros or inline functions from this file, or you compile
17  * this file and link it with other files to produce an executable, this
18  * file does not by itself cause the resulting executable to be covered by
19  * the GNU General Public License. This exception does not however
20  * invalidate any other reasons why the executable file might be covered by
21  * the GNU General Public License.
22  */
23 #pragma once
24 
25 namespace L4 {
26 
27  /**
28  * \brief Modifier class for the IO stream.
29  *
30  * An IO Modifier can be used to change properties of an IO stream
31  * for example the number format.
32  */
33  class IOModifier
34  {
35  public:
36  IOModifier( int x ) : mod(x) {}
37  bool operator == (IOModifier o) { return mod == o.mod; }
38  bool operator != (IOModifier o) { return mod != o.mod; }
39  int mod;
40  };
41 
42  /**
43  * \internal
44  * \brief Backend to write or read stream data.
45  */
46  class IOBackend
47  {
48  public:
49  typedef int Mode;
50 
51  protected:
52  friend class BasicOStream;
53 
54  IOBackend()
55  : int_mode(10)
56  {}
57 
58  virtual ~IOBackend() {}
59 
60  virtual void write(char const *str, unsigned len) = 0;
61 
62  private:
63  void write(IOModifier m);
64  void write(long long int c, int len);
65  void write(long long unsigned c, int len);
66  void write(long long unsigned c, unsigned char base = 10,
67  unsigned char len = 0, char pad = ' ');
68 
69  Mode mode() const
70  { return int_mode; }
71 
72  void mode(Mode m)
73  { int_mode = m; }
74 
75  int int_mode;
76  };
77 
78  /**
79  * \internal
80  * \brief Write-only backend for stream data.
81  */
82  class BasicOStream
83  {
84  public:
85  BasicOStream(IOBackend *b)
86  : iob(b)
87  {}
88 
89  void write(char const *str, unsigned len)
90  { if(iob) iob->write(str,len); }
91 
92  void write(long long int c, int len)
93  { if(iob) iob->write(c,len); }
94 
95  void write(long long unsigned c, unsigned char base = 10,
96  unsigned char len = 0, char pad = ' ')
97  { if(iob) iob->write(c, base, len, pad); }
98 
99  void write(long long unsigned c, int len)
100  { if(iob) iob->write(c,len); }
101 
102  void write(IOModifier m)
103  { if(iob) iob->write(m); }
104 
105  IOBackend::Mode be_mode() const
106  { if(iob) return iob->mode(); return 0; }
107 
108  void be_mode(IOBackend::Mode m)
109  { if(iob) iob->mode(m); }
110 
111  private:
112  IOBackend *iob;
113 
114  };
115 
116  /**
117  * \internal
118  * \brief Container class describing a the number format.
119  */
120  class IONumFmt
121  {
122  public:
123  IONumFmt(unsigned long long n, unsigned char base = 10,
124  unsigned char len = 0, char pad = ' ')
125  : n(n), base(base), len(len), pad(pad)
126  {}
127 
128  BasicOStream &print(BasicOStream &o) const;
129 
130  private:
131  unsigned long long n;
132  unsigned char base, len;
133  char pad;
134  };
135 
136  inline IONumFmt n_hex(unsigned long long n) { return IONumFmt(n, 16); }
137 
138  /**
139  * \brief Modifies the stream to print numbers as hexadecimal values.
140  */
141  extern IOModifier const hex;
142 
143  /**
144  * \brief Modifies the stream to print numbers as decimal values.
145  */
146  extern IOModifier const dec;
147 
148  inline
149  BasicOStream &IONumFmt::print(BasicOStream &o) const
150  {
151  o.write(n, base, len, pad);
152  return o;
153  }
154 
155 }
156 
157 
158 // implementation
159 
160 inline
161 L4::BasicOStream &
162 operator << (L4::BasicOStream &s, char const * const str)
163 {
164  if (!str)
165  {
166  s.write("(NULL)", 6);
167  return s;
168  }
169 
170  unsigned l=0;
171  for(; str[l]!=0; l++)
172  ;
173  s.write(str,l);
174  return s;
175 }
176 
177 inline
178 L4::BasicOStream &
179 operator << (L4::BasicOStream &s, signed short u)
180 {
181  s.write((long long signed)u,-1);
182  return s;
183 }
184 
185 inline
186 L4::BasicOStream &
187 operator << (L4::BasicOStream &s, signed u)
188 {
189  s.write((long long signed)u,-1);
190  return s;
191 }
192 
193 inline
194 L4::BasicOStream &
195 operator << (L4::BasicOStream &s, signed long u)
196 {
197  s.write((long long signed)u,-1);
198  return s;
199 }
200 
201 inline
202 L4::BasicOStream &
203 operator << (L4::BasicOStream &s, signed long long u)
204 {
205  s.write(u,-1);
206  return s;
207 }
208 
209 inline
210 L4::BasicOStream &
211 operator << (L4::BasicOStream &s, unsigned short u)
212 {
213  s.write((long long unsigned)u,-1);
214  return s;
215 }
216 
217 inline
218 L4::BasicOStream &
219 operator << (L4::BasicOStream &s, unsigned u)
220 {
221  s.write((long long unsigned)u,-1);
222  return s;
223 }
224 
225 inline
226 L4::BasicOStream &
227 operator << (L4::BasicOStream &s, unsigned long u)
228 {
229  s.write((long long unsigned)u,-1);
230  return s;
231 }
232 
233 inline
234 L4::BasicOStream &
235 operator << (L4::BasicOStream &s, unsigned long long u)
236 {
237  s.write(u,-1);
238  return s;
239 }
240 
241 inline
242 L4::BasicOStream &
243 operator << (L4::BasicOStream &s, void const *u)
244 {
245  long unsigned x = (long unsigned)u;
246  L4::IOBackend::Mode mode = s.be_mode();
247  s.write(L4::hex);
248  s.write((long long unsigned)x,-1);
249  s.be_mode(mode);
250  return s;
251 }
252 
253 inline
254 L4::BasicOStream &
255 operator << (L4::BasicOStream &s, L4::IOModifier m)
256 {
257  s.write(m);
258  return s;
259 }
260 
261 inline
262 L4::BasicOStream &
263 operator << (L4::BasicOStream &s, char c)
264 {
265  s.write( &c, 1 );
266  return s;
267 }
268 
269 
270 
271 inline
272 L4::BasicOStream &
273 operator << (L4::BasicOStream &o, L4::IONumFmt const &n)
274 { return n.print(o); }
275