1 | /** @file
|
---|
2 | * IPRT / No-CRT - Minimal C++ ostream 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.virtualbox.org. 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_ostream
|
---|
27 | #define IPRT_INCLUDED_nocrt_ostream
|
---|
28 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
29 | # pragma once
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | /* Currently all in the ios header. */
|
---|
33 | #include <iprt/nocrt/ios>
|
---|
34 |
|
---|
35 | namespace std
|
---|
36 | {
|
---|
37 | /**
|
---|
38 | * Basic output stream.
|
---|
39 | */
|
---|
40 | template<typename a_CharType, typename a_CharTraits /*= std::char_traits<a_CharType>*/ >
|
---|
41 | class basic_ostream : public basic_ios<a_CharType, a_CharTraits>
|
---|
42 | {
|
---|
43 | protected:
|
---|
44 | /** Sentry class that performs pre and post output work. */
|
---|
45 | class sentry
|
---|
46 | {
|
---|
47 | private:
|
---|
48 | basic_ostream &m_rParent;
|
---|
49 |
|
---|
50 | public:
|
---|
51 | explicit sentry(basic_ostream &a_rParent)
|
---|
52 | : m_rParent(a_rParent)
|
---|
53 | {
|
---|
54 | if (a_rParent.good())
|
---|
55 | {
|
---|
56 | basic_ostream *pTiedStream = a_rParent.tie();
|
---|
57 | if (!pTiedStream)
|
---|
58 | { /* likely? */ }
|
---|
59 | else
|
---|
60 | {
|
---|
61 | pTiedStream->flush();
|
---|
62 | if (!pTiedStream->good())
|
---|
63 | a_rParent.setstate(failbit);
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | explicit operator bool() const
|
---|
69 | {
|
---|
70 | return m_rParent.good();
|
---|
71 | }
|
---|
72 |
|
---|
73 | ~sentry()
|
---|
74 | {
|
---|
75 | if ( (m_rParent.flags() & std::ios_base::unitbuf)
|
---|
76 | && m_rParent.good())
|
---|
77 | m_rParent.rdbuf()->pubsync();
|
---|
78 | }
|
---|
79 | };
|
---|
80 |
|
---|
81 | public:
|
---|
82 | explicit basic_ostream(std::basic_streambuf<a_CharType,a_CharTraits> *a_pBuf)
|
---|
83 | : basic_ios(a_pBuf)
|
---|
84 | { }
|
---|
85 | private:
|
---|
86 | basic_ostream(basic_ostream const &a_rSrc); /* not copyable */
|
---|
87 | basic_ostream &operator=(basic_ostream const &a_rSrc); /* not copyable */
|
---|
88 |
|
---|
89 | public:
|
---|
90 | virtual ~basic_ostream()
|
---|
91 | {
|
---|
92 | }
|
---|
93 |
|
---|
94 | public:
|
---|
95 | basic_ostream &put(char_type a_ch)
|
---|
96 | {
|
---|
97 | sentry PrePost(*this);
|
---|
98 | if (PrePost)
|
---|
99 | {
|
---|
100 | if (m_pBuf->sputc(a_ch) == traits_type::eof())
|
---|
101 | m_fState |= badbit;
|
---|
102 | }
|
---|
103 | return *this;
|
---|
104 | }
|
---|
105 |
|
---|
106 | basic_ostream &write(const char_type *a_pchSrc, std::streamsize a_cchToWrite)
|
---|
107 | {
|
---|
108 | sentry PrePost(*this);
|
---|
109 | if (PrePost)
|
---|
110 | {
|
---|
111 | std::streamsize cchWritten = m_pBuf->sputn(a_pchSrc, a_cchToWrite);
|
---|
112 | if (cchWritten != a_cchToWrite)
|
---|
113 | m_fState |= badbit;
|
---|
114 | }
|
---|
115 | return *this;
|
---|
116 | }
|
---|
117 |
|
---|
118 | basic_ostream &flush();
|
---|
119 | pos_type tellp() RT_NOEXCEPT;
|
---|
120 | basic_ostream &seekp(pos_type a_off) RT_NOEXCEPT;
|
---|
121 | basic_ostream &seekp(off_type a_off, seekdir enmDir) RT_NOEXCEPT;
|
---|
122 |
|
---|
123 | /** @name Internal support methods
|
---|
124 | * @{ */
|
---|
125 | basic_ostream &intWrite(const char *a_pchSrc, std::streamsize a_cchToWrite); /**< Internal method outputting char buffers. */
|
---|
126 |
|
---|
127 | /** @returns 8, 10 or 16. */
|
---|
128 | inline unsigned intGetIntegerBase() const RT_NOEXCEPT
|
---|
129 | {
|
---|
130 | switch (m_fFlags & basefield)
|
---|
131 | {
|
---|
132 | default:
|
---|
133 | case dec: return 10;
|
---|
134 | case hex: return 16;
|
---|
135 | case oct: return 8;
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | /** @returns RTSTR_F_XXX . */
|
---|
140 | inline unsigned intGetIntegerFlags() const RT_NOEXCEPT
|
---|
141 | {
|
---|
142 | unsigned fFlags = 0;
|
---|
143 | if (m_fFlags & uppercase)
|
---|
144 | fFlags |= RTSTR_F_CAPITAL;
|
---|
145 | if (m_fFlags & showbase)
|
---|
146 | fFlags |= RTSTR_F_SPECIAL;
|
---|
147 | if (m_fFlags & showpos)
|
---|
148 | fFlags |= RTSTR_F_PLUS;
|
---|
149 | return fFlags;
|
---|
150 | }
|
---|
151 |
|
---|
152 | basic_ostream &formatInteger(uint64_t a_uValue, uint32_t a_fFlags, unsigned a_uBase = 0)
|
---|
153 | {
|
---|
154 | a_fFlags |= intGetIntegerFlags();
|
---|
155 | char szTmp[72];
|
---|
156 | int cchTmp = RTStrFormatNumber(szTmp, a_uValue, !a_uBase ? intGetIntegerBase() : a_uBase, 0, 0, a_fFlags);
|
---|
157 |
|
---|
158 | /** @todo apply cchWidth and padding. */
|
---|
159 |
|
---|
160 | return intWrite(szTmp, cchTmp);
|
---|
161 | }
|
---|
162 |
|
---|
163 | /** @} */
|
---|
164 | };
|
---|
165 |
|
---|
166 | /** @name Character and string output.
|
---|
167 | * @{ */
|
---|
168 | /** @todo not sure if this really works... */
|
---|
169 | template<typename a_CharType, typename a_CharTraits = std::char_traits<a_CharType> >
|
---|
170 | basic_ostream<a_CharType, a_CharTraits> &operator<<(basic_ostream<a_CharType, a_CharTraits> &a_rDst, char a_ch)
|
---|
171 | {
|
---|
172 | return a_rDst.put(a_ch);
|
---|
173 | }
|
---|
174 |
|
---|
175 | template<typename a_CharType, typename a_CharTraits = std::char_traits<a_CharType> >
|
---|
176 | basic_ostream<a_CharType, a_CharTraits> &operator<<(basic_ostream<a_CharType, a_CharTraits> &a_rDst, const char *a_psz)
|
---|
177 | {
|
---|
178 | return a_rDst.intWrite(a_psz, strlen(a_psz));
|
---|
179 | }
|
---|
180 | /** @} */
|
---|
181 |
|
---|
182 | /** @name Integer formatting.
|
---|
183 | * @{ */
|
---|
184 | template<typename a_CharType, typename a_CharTraits = std::char_traits<a_CharType> >
|
---|
185 | basic_ostream<a_CharType, a_CharTraits> &operator<<(basic_ostream<a_CharType, a_CharTraits> &a_rDst, signed char a_iValue)
|
---|
186 | {
|
---|
187 | return a_rDst.formatInteger(a_iValue, RTSTR_F_8BIT | RTSTR_F_VALSIGNED);
|
---|
188 | }
|
---|
189 |
|
---|
190 | template<typename a_CharType, typename a_CharTraits = std::char_traits<a_CharType> >
|
---|
191 | basic_ostream<a_CharType, a_CharTraits> &operator<<(basic_ostream<a_CharType, a_CharTraits> &a_rDst, unsigned char a_uValue)
|
---|
192 | {
|
---|
193 | return a_rDst.formatInteger(a_uValue, RTSTR_F_8BIT);
|
---|
194 | }
|
---|
195 |
|
---|
196 | template<typename a_CharType, typename a_CharTraits = std::char_traits<a_CharType> >
|
---|
197 | basic_ostream<a_CharType, a_CharTraits> &operator<<(basic_ostream<a_CharType, a_CharTraits> &a_rDst, short a_iValue)
|
---|
198 | {
|
---|
199 | return a_rDst.formatInteger(a_iValue, RTSTR_F_16BIT | RTSTR_F_VALSIGNED);
|
---|
200 | }
|
---|
201 |
|
---|
202 | template<typename a_CharType, typename a_CharTraits = std::char_traits<a_CharType> >
|
---|
203 | basic_ostream<a_CharType, a_CharTraits> &operator<<(basic_ostream<a_CharType, a_CharTraits> &a_rDst, unsigned short a_uValue)
|
---|
204 | {
|
---|
205 | return a_rDst.formatInteger(a_uValue, RTSTR_F_16BIT);
|
---|
206 | }
|
---|
207 |
|
---|
208 | template<typename a_CharType, typename a_CharTraits = std::char_traits<a_CharType> >
|
---|
209 | basic_ostream<a_CharType, a_CharTraits> &operator<<(basic_ostream<a_CharType, a_CharTraits> &a_rDst, int a_iValue)
|
---|
210 | {
|
---|
211 | return a_rDst.formatInteger(a_iValue, RTSTR_F_32BIT | RTSTR_F_VALSIGNED);
|
---|
212 | }
|
---|
213 |
|
---|
214 | template<typename a_CharType, typename a_CharTraits = std::char_traits<a_CharType> >
|
---|
215 | basic_ostream<a_CharType, a_CharTraits> &operator<<(basic_ostream<a_CharType, a_CharTraits> &a_rDst, unsigned int a_uValue)
|
---|
216 | {
|
---|
217 | return a_rDst.formatInteger(a_uValue, RTSTR_F_32BIT | RTSTR_F_VALSIGNED);
|
---|
218 | }
|
---|
219 |
|
---|
220 | template<typename a_CharType, typename a_CharTraits = std::char_traits<a_CharType> >
|
---|
221 | basic_ostream<a_CharType, a_CharTraits> &operator<<(basic_ostream<a_CharType, a_CharTraits> &a_rDst, long a_iValue)
|
---|
222 | {
|
---|
223 | return a_rDst.formatInteger(a_iValue, (sizeof(a_iValue) > sizeof(int32_t) ? RTSTR_F_64BIT : RTSTR_F_32BIT));
|
---|
224 | }
|
---|
225 |
|
---|
226 | template<typename a_CharType, typename a_CharTraits = std::char_traits<a_CharType> >
|
---|
227 | basic_ostream<a_CharType, a_CharTraits> &operator<<(basic_ostream<a_CharType, a_CharTraits> &a_rDst, unsigned long a_uValue)
|
---|
228 | {
|
---|
229 | return a_rDst.formatInteger(a_uValue,
|
---|
230 | RTSTR_F_VALSIGNED | (sizeof(a_uValue) > sizeof(uint32_t) ? RTSTR_F_64BIT : RTSTR_F_32BIT));
|
---|
231 | }
|
---|
232 |
|
---|
233 | template<typename a_CharType, typename a_CharTraits = std::char_traits<a_CharType> >
|
---|
234 | basic_ostream<a_CharType, a_CharTraits> &operator<<(basic_ostream<a_CharType, a_CharTraits> &a_rDst, void const *a_pvValue)
|
---|
235 | {
|
---|
236 | return a_rDst.formatInteger((uintptr_t)a_pvValue,
|
---|
237 | (sizeof(a_pvValue) > sizeof(uint32_t) ? RTSTR_F_64BIT : RTSTR_F_32BIT), 16);
|
---|
238 | }
|
---|
239 | /** @} */
|
---|
240 |
|
---|
241 | template<>
|
---|
242 | basic_ostream<char> &basic_ostream<char>::intWrite(const char *a_pchSrc, std::streamsize a_cchToWrite)
|
---|
243 | {
|
---|
244 | return write(a_pchSrc, a_cchToWrite);
|
---|
245 | }
|
---|
246 |
|
---|
247 |
|
---|
248 | }
|
---|
249 |
|
---|
250 | #endif /* !IPRT_INCLUDED_nocrt_ostream */
|
---|
251 |
|
---|