VirtualBox

source: vbox/trunk/include/iprt/nocrt/ios@ 95985

Last change on this file since 95985 was 95985, checked in by vboxsync, 3 years ago

include/iprt/nocrt: Some early & incomplete ostream code. bugref:10261

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.4 KB
Line 
1/** @file
2 * IPRT / No-CRT - Minimal C++ ios header.
3 */
4
5/*
6 * Copyright (C) 2022 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.215389.xyz. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef IPRT_INCLUDED_nocrt_ios
27#define IPRT_INCLUDED_nocrt_ios
28#ifndef RT_WITHOUT_PRAGMA_ONCE
29# pragma once
30#endif
31
32#include <iprt/nocrt/iosfwd>
33#include <iprt/nocrt/string>
34
35/** @todo something for cdecl.h */
36#define RTNOCRT_IOS_ENUM_BIT_OPS(a_EnumType, a_IntType) \
37 inline a_EnumType operator~(a_EnumType a_fLeft) RT_NOEXCEPT \
38 { return a_EnumType(~static_cast<a_IntType>(a_fLeft)); } \
39 \
40 inline a_EnumType operator&(a_EnumType a_fLeft, a_EnumType a_fRight) RT_NOEXCEPT \
41 { return a_EnumType(static_cast<a_IntType>(a_fLeft) & static_cast<a_IntType>(a_fRight)); } \
42 inline a_EnumType operator|(a_EnumType a_fLeft, a_EnumType a_fRight) RT_NOEXCEPT \
43 { return a_EnumType(static_cast<a_IntType>(a_fLeft) | static_cast<a_IntType>(a_fRight)); } \
44 inline a_EnumType operator^(a_EnumType a_fLeft, a_EnumType a_fRight) RT_NOEXCEPT \
45 { return a_EnumType(static_cast<a_IntType>(a_fLeft) ^ static_cast<a_IntType>(a_fRight)); } \
46 \
47 inline const a_EnumType &operator&=(a_EnumType &a_rfLeft, a_EnumType a_fRight) RT_NOEXCEPT \
48 { return a_rfLeft = a_rfLeft & a_fRight; } \
49 inline const a_EnumType &operator|=(a_EnumType &a_rfLeft, a_EnumType a_fRight) RT_NOEXCEPT \
50 { return a_rfLeft = a_rfLeft | a_fRight; } \
51 inline const a_EnumType &operator^=(a_EnumType &a_rfLeft, a_EnumType a_fRight) RT_NOEXCEPT \
52 { return a_rfLeft = a_rfLeft ^ a_fRight; } \
53
54namespace std
55{
56 typedef RTFOFF streamsize;
57
58 /**
59 * I/O stream format flags.
60 */
61 enum rtNoCrtIosFmtFlags
62 {
63 /* int: */
64 dec = 0x00000001,
65 oct = 0x00000002,
66 hex = 0x00000004,
67 basefield = 0x00000007,
68 /* float: */
69 scientific = 0x00000010,
70 fixed = 0x00000020,
71 floatfield = 0x00000030,
72 /* int and float output tweaks: */
73 showbase = 0x00000100,
74 showpoint = 0x00000200,
75 showpos = 0x00000400,
76 /* bool: */
77 boolalpha = 0x00000800,
78 /* adjustment: */
79 left = 0x00001000,
80 right = 0x00002000,
81 internal = 0x00004000,
82 adjustfield = 0x00007000,
83 /* misc: */
84 skipws = 0x00010000,
85 unitbuf = 0x00020000,
86 uppercase = 0x00040000,
87 };
88 RTNOCRT_IOS_ENUM_BIT_OPS(rtNoCrtIosFmtFlags, int)
89
90 enum rtNoCrtIosSeekDir
91 {
92 beg = 1,
93 end,
94 cur,
95 };
96 RTNOCRT_IOS_ENUM_BIT_OPS(rtNoCrtIosSeekDir, int)
97
98 enum rtNoCrtIosOpenMode
99 {
100 app = 1,
101 binary,
102 in,
103 out,
104 trunc,
105 ate
106 };
107 RTNOCRT_IOS_ENUM_BIT_OPS(rtNoCrtIosOpenMode, int)
108
109 enum rtNoCrtIosState
110 {
111 goodbit = 0,
112 badbit = 1,
113 failbit = 2,
114 eofbit = 4
115 };
116 RTNOCRT_IOS_ENUM_BIT_OPS(rtNoCrtIosState, int)
117
118
119 /**
120 * I/O stream base class.
121 */
122 class ios_base
123 {
124 public:
125 typedef rtNoCrtIosFmtFlags fmtflags;
126 typedef rtNoCrtIosSeekDir seekdir;
127 typedef rtNoCrtIosOpenMode openmode;
128 typedef rtNoCrtIosState iostate;
129
130 protected:
131 streamsize m_cWidth;
132 streamsize m_cPrecision;
133 fmtflags m_fFlags;
134 iostate m_fState;
135
136 protected:
137 ios_base()
138 : m_cWidth(0)
139 , m_cPrecision(0)
140 , m_fFlags(dec | skipws)
141 , m_fState(goodbit)
142 {
143 }
144 private:
145 ios_base(const ios_base &); /* not copyable */
146 ios_base &operator=(const ios_base &); /* not copyable */
147
148 public:
149 virtual ~ios_base()
150 {
151 }
152
153 streamsize width() const RT_NOEXCEPT
154 {
155 return m_cWidth;
156 }
157
158 streamsize width(streamsize a_cWidth) RT_NOEXCEPT
159 {
160 streamsize cOldWidth = m_cWidth;
161 m_cWidth = a_cWidth;
162 return cOldWidth;
163 }
164
165 streamsize precision() const RT_NOEXCEPT
166 {
167 return m_cPrecision;
168 }
169
170 streamsize precision(streamsize a_cPrecision) RT_NOEXCEPT
171 {
172 streamsize cOldPrecision = m_cPrecision;
173 m_cPrecision = a_cPrecision;
174 return cOldPrecision;
175 }
176
177 fmtflags flags() const RT_NOEXCEPT
178 {
179 return m_fFlags;
180 }
181
182 fmtflags flags(fmtflags a_fNew) RT_NOEXCEPT
183 {
184 fmtflags const fOld = m_fFlags;
185 m_fFlags = a_fNew;
186 return fOld;
187 }
188
189 fmtflags setf(fmtflags a_fAdd) RT_NOEXCEPT
190 {
191 fmtflags const fOld = m_fFlags;
192 m_fFlags = static_cast<fmtflags>(fOld | a_fAdd);
193 return fOld;
194 }
195
196 fmtflags setf(fmtflags a_fAdd, fmtflags a_fMask) RT_NOEXCEPT
197 {
198 fmtflags const fOld = m_fFlags;
199 m_fFlags = static_cast<fmtflags>((fOld & ~a_fMask) | (a_fAdd & a_fMask));
200 return fOld;
201 }
202
203 void unsetf(fmtflags a_fClear) RT_NOEXCEPT
204 {
205 m_fFlags = static_cast<fmtflags>(m_fFlags & ~a_fClear);
206 }
207 };
208
209
210 /**
211 * Stream buffer.
212 */
213 template<typename a_CharType, typename a_CharTraits /*= std::char_traits<a_CharType>*/ >
214 class basic_streambuf
215 {
216 public:
217 typedef a_CharType char_type;
218 typedef a_CharTraits traits_type;
219 typedef typename a_CharTraits::int_type int_type;
220 typedef typename a_CharTraits::pos_type pos_type;
221 typedef typename a_CharTraits::off_type off_type;
222
223 protected:
224 basic_streambuf()
225 {
226 }
227
228 basic_streambuf(const basic_streambuf &a_rSrc)
229 {
230 }
231
232 public:
233 virtual ~basic_streambuf()
234 {
235 }
236
237 /** @name Positioning
238 * @{ */
239 protected:
240 virtual basic_streambuf *setbuf(char_type *a_pchBuf, std::streamsize a_cchBuf)
241 {
242 RT_NOREF(a_pchBuf, a_cchBuf);
243 return this;
244 }
245 public:
246 basic_streambuf *pubsetbuf(char_type *a_pchBuf, std::streamsize a_cchBuf)
247 {
248 return setbuf(a_off, a_enmDir, a_enmTarget);
249 }
250
251 protected:
252 virtual pos_type seekoff(off_type a_off, std::ios_base::seekdir a_enmDir,
253 std::ios_base::openmode a_enmTarget = ios_base::in | ios_base::out)
254 {
255 RT_NOREF(a_off, a_enmDir, out);
256 return pos_type(off_type(-1));
257 }
258 public:
259 pos_type pubseekoff(off_type a_off, std::ios_base::seekdir a_enmDir,
260 std::ios_base::openmode a_enmTarget = ios_base::in | ios_base::out)
261 {
262 return seekoff(a_off, a_enmDir, a_enmTarget);
263 }
264
265 protected:
266 virtual pos_type seekpos(pos_type a_pos, std::ios_base::openmode a_enmTarget = ios_base::in | ios_base::out)
267 {
268 RT_NOREF(a_off, a_enmDir, out);
269 return pos_type(off_type(-1));
270 }
271 public:
272 pos_type pubseekpos(pos_type a_pos, std::ios_base::openmode a_enmTarget = ios_base::in | ios_base::out)
273 {
274 return seekpos(a_pos, a_enmTarget);
275 }
276
277 protected:
278 virtual int sync()
279 {
280 return 0;
281 }
282 public:
283 pos_type pubsync()
284 {
285 return sync();
286 }
287 /** @} */
288
289 /** @todo add the remainin members... */
290 };
291
292
293 /**
294 * Basic I/O stream.
295 */
296 template<typename a_CharType, typename a_CharTraits /*= std::char_traits<a_CharType>*/ >
297 class basic_ios : public ios_base
298 {
299 public:
300 typedef a_CharType char_type;
301 typedef a_CharTraits traits_type;
302 typedef typename a_CharTraits::int_type int_type;
303 typedef typename a_CharTraits::pos_type pos_type;
304 typedef typename a_CharTraits::off_type off_type;
305
306 protected:
307 basic_streambuf<a_CharType, a_CharTraits> *m_pBuf;
308
309 protected:
310 void init(const std::basic_streambuf<a_CharType, a_CharTraits> *a_pBuf)
311 {
312 m_pBuf = a_pBuf;
313 m_cWidth = 0;
314 m_cPrecision = 6;
315 m_fFlags = ios_base::dec | ios_base::wskip;
316 m_fState = ios_base::goodbit;
317 }
318
319 public:
320 basic_ios()
321 : m_pBuf(NULL)
322 , ios_base()
323 {
324 }
325
326 basic_ios(const std::basic_streambuf<a_CharType, a_CharTraits> *a_pBuf)
327 : m_pBuf(NULL)
328 , ios_base()
329 {
330 init(a_pBuf)
331 }
332 private:
333 basic_ios(const basic_ios &a_rSrc); /* not copyable */
334 basic_ios &operator=(const basic_ios &a_rSrc); /* not copyable */
335
336 public:
337 virtual ~basic_ios()
338 {
339 }
340
341 /** @name State methods
342 * @{ */
343 bool good() const RT_NOEXCEPT { return m_fState == ios_base::goodbit; }
344 bool fail() const RT_NOEXCEPT { return (m_fState & (ios_base::failbit | ios_base::badbit)) != ios_base::goodbit; }
345 bool bad() const RT_NOEXCEPT { return (m_fState & ios_base::badbit) == ios_base::badbit; }
346 bool eof() const RT_NOEXCEPT { return (m_fState & ios_base::eofbit) != ios_base::eofbit; }
347#if RT_CPLUSPLUS_PREREQ(201100)
348 operator bool() const RT_NOEXCEPT { return good(); }
349#else
350 operator void*() const RT_NOEXCEPT { return good() ? NULL : this; }
351#endif
352 bool operator!() const RT_NOEXCEPT { return fail(); }
353
354 iostate rdstate() const RT_NOEXCEPT
355 {
356 return m_fState;
357 }
358
359 void clear(iostate a_fNewState = goodbit)
360 {
361 m_fState = a_fNewState;
362 if (!m_pBuf)
363 m_fState |= badbit;
364 /** @todo failure exception */
365 }
366
367 void setstate(iostate a_fNewState)
368 {
369 clear(m_fState | a_fNewState);
370 }
371 /** @} */
372
373 /** @name Misc
374 * @{ */
375 std::basic_streambuf<a_CharType, a_CharTraits> *rdbuf() const RT_NOEXCEPT
376 {
377 return m_pBuf;
378 }
379
380 std::basic_streambuf<a_CharType, a_CharTraits> *rdbuf(std::basic_streambuf<a_CharType, a_CharTraits> *a_pNewbuf) RT_NOEXCEPT
381 {
382 std::basic_streambuf<a_CharType, a_CharTraits> *pOldBuf = m_pBuf;
383 m_pBuf = a_pNewBuf;
384 return pOldBuf;
385 }
386 /** @} */
387
388 /** @todo implement the rest... */
389 };
390}
391
392#endif /* !IPRT_INCLUDED_nocrt_ios */
393
394
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette