Embedded Template Library 1.0
Loading...
Searching...
No Matches
basic_string_stream.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2020 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_BASIC_STRING_STREAM_INCLUDED
32#define ETL_BASIC_STRING_STREAM_INCLUDED
33
35
36#include "platform.h"
37#include "to_string.h"
38#include "to_u16string.h"
39#include "to_u32string.h"
40#include "to_u8string.h"
41#include "to_wstring.h"
42
43namespace etl
44{
45 template <typename TFormat, typename TIString, typename TStringView>
47 {
48 public:
49
50 typedef TFormat format_spec_type;
51 typedef TIString istring_type;
52 typedef typename TIString::value_type value_type;
53 typedef typename TIString::pointer pointer;
54 typedef typename TIString::const_pointer const_pointer;
55
56 //*************************************************************************
58 //*************************************************************************
59 explicit basic_string_stream(TIString& text_)
60 : text(text_)
61 {
62 }
63
64 //*************************************************************************
66 //*************************************************************************
67 basic_string_stream(TIString& text_, const TFormat& spec_)
68 : text(text_)
69 , format(spec_)
70 {
71 }
72
73 //*************************************************************************
75 //*************************************************************************
76 void set_format(const TFormat& spec_)
77 {
78 format = spec_;
79 }
80
81 //*************************************************************************
83 //*************************************************************************
84 const TFormat& get_format() const
85 {
86 return format;
87 }
88
89 //*************************************************************************
91 //*************************************************************************
92 TIString& str()
93 {
94 return text;
95 }
96
97 //*************************************************************************
99 //*************************************************************************
100 const TIString& str() const
101 {
102 return text;
103 }
104
105 //*************************************************************************
107 //*************************************************************************
108 void str(const value_type* p)
109 {
110 text.assign(p);
111 }
112
113 //*************************************************************************
115 //*************************************************************************
116 void str(const TIString& is)
117 {
118 text.assign(is);
119 }
120
121 //*************************************************************************
123 //*************************************************************************
124
125 //*********************************
127 //*********************************
128 friend basic_string_stream& operator<<(basic_string_stream& ss, const TFormat& fmt)
129 {
130 ss.format = fmt;
131 return ss;
132 }
133
134 //*********************************
137 //*********************************
139 {
140 ss.format.base(fmt.base);
141 return ss;
142 }
143
144 //*********************************
146 //*********************************
148 {
149 ss.format.width(fmt.width);
150 return ss;
151 }
152
153 //*********************************
155 //*********************************
156 template <typename TChar>
158 {
159 ss.format.fill(fmt.fill);
160 return ss;
161 }
162
163 //*********************************
165 //*********************************
167 {
168 ss.format.precision(fmt.precision);
169 return ss;
170 }
171
172 //*********************************
175 //*********************************
177 {
178 ss.format.boolalpha(fmt.boolalpha);
179 return ss;
180 }
181
182 //*********************************
185 //*********************************
187 {
188 ss.format.upper_case(fmt.upper_case);
189 return ss;
190 }
191
192 //*********************************
195 //*********************************
197 {
198 ss.format.show_base(fmt.show_base);
199 return ss;
200 }
201
202 //*********************************
204 //*********************************
206 {
207 ss.format.left();
208 return ss;
209 }
210
211 //*********************************
213 //*********************************
215 {
216 ss.format.right();
217 return ss;
218 }
219
220 //*********************************
222 //*********************************
224 {
225 etl::to_string(view, ss.text, ss.format, true);
226 return ss;
227 }
228
229 //*********************************
231 //*********************************
233 {
234 TStringView view(p);
235 ss << view;
236 return ss;
237 }
238
239 //*********************************
241 //*********************************
243 {
244 TStringView view(p);
245 ss << view;
246 return ss;
247 }
248
249 //*********************************
251 //*********************************
252 friend basic_string_stream& operator<<(basic_string_stream& ss, const TIString& t)
253 {
254 etl::to_string(t, ss.text, ss.format, true);
255 return ss;
256 }
257
258 //*********************************
260 //*********************************
261 template <template <size_t> class TString, size_t SIZE>
262 friend basic_string_stream& operator<<(basic_string_stream& ss, const TString<SIZE>& t)
263 {
264 const TIString& itext = t;
265 etl::to_string(itext, ss.str(), ss.get_format(), true);
266 return ss;
267 }
268
269 //*********************************
271 //*********************************
272 template <typename T>
274 {
275 etl::to_string(value, ss.text, ss.format, true);
276 return ss;
277 }
278
279 private:
280
281 TIString& text;
282 TFormat format;
283
284 basic_string_stream(const basic_string_stream&) ETL_DELETE;
285 basic_string_stream& operator=(const basic_string_stream&) ETL_DELETE;
286 };
287} // namespace etl
288
289#endif
Definition basic_string_stream.h:47
void str(const value_type *p)
Resets the stream to the supplied string.
Definition basic_string_stream.h:108
friend basic_string_stream & operator<<(basic_string_stream &ss, etl::private_basic_format_spec::precision_spec fmt)
etl::precision_spec from etl::setprecision stream manipulator
Definition basic_string_stream.h:166
friend basic_string_stream & operator<<(basic_string_stream &ss, etl::private_basic_format_spec::uppercase_spec fmt)
Definition basic_string_stream.h:186
friend basic_string_stream & operator<<(basic_string_stream &ss, pointer p)
From a character pointer to a string.
Definition basic_string_stream.h:232
basic_string_stream(TIString &text_)
Construct from text.
Definition basic_string_stream.h:59
friend basic_string_stream & operator<<(basic_string_stream &ss, const TString< SIZE > &t)
From a string.
Definition basic_string_stream.h:262
friend basic_string_stream & operator<<(basic_string_stream &ss, const TIString &t)
From a string interface.
Definition basic_string_stream.h:252
friend basic_string_stream & operator<<(basic_string_stream &ss, etl::private_basic_format_spec::showbase_spec fmt)
Definition basic_string_stream.h:196
friend basic_string_stream & operator<<(basic_string_stream &ss, etl::private_basic_format_spec::left_spec)
etl::left_spec from etl::left stream manipulator
Definition basic_string_stream.h:205
void str(const TIString &is)
Resets the stream to the supplied string.
Definition basic_string_stream.h:116
friend basic_string_stream & operator<<(basic_string_stream &ss, const TFormat &fmt)
Stream operators.
Definition basic_string_stream.h:128
friend basic_string_stream & operator<<(basic_string_stream &ss, const_pointer p)
From a const character pointer to a string.
Definition basic_string_stream.h:242
friend basic_string_stream & operator<<(basic_string_stream &ss, const T &value)
From anything else.
Definition basic_string_stream.h:273
friend basic_string_stream & operator<<(basic_string_stream &ss, etl::private_basic_format_spec::width_spec fmt)
etl::width_spec from etl::setw stream manipulator
Definition basic_string_stream.h:147
friend basic_string_stream & operator<<(basic_string_stream &ss, etl::private_basic_format_spec::base_spec fmt)
Definition basic_string_stream.h:138
TIString & str()
Get a reference to the current string.
Definition basic_string_stream.h:92
friend basic_string_stream & operator<<(basic_string_stream &ss, etl::private_basic_format_spec::boolalpha_spec fmt)
Definition basic_string_stream.h:176
void set_format(const TFormat &spec_)
Set the format fmt.
Definition basic_string_stream.h:76
friend basic_string_stream & operator<<(basic_string_stream &ss, TStringView view)
From a string view.
Definition basic_string_stream.h:223
const TIString & str() const
Get a const reference to the current string.
Definition basic_string_stream.h:100
const TFormat & get_format() const
Get a const reference to the format fmt.
Definition basic_string_stream.h:84
friend basic_string_stream & operator<<(basic_string_stream &ss, etl::private_basic_format_spec::right_spec)
etl::right_spec from etl::left stream manipulator
Definition basic_string_stream.h:214
basic_string_stream(TIString &text_, const TFormat &spec_)
Construct from text and format fmt.
Definition basic_string_stream.h:67
friend basic_string_stream & operator<<(basic_string_stream &ss, etl::private_basic_format_spec::fill_spec< TChar > fmt)
etl::fill_spec from etl::setfill stream manipulator
Definition basic_string_stream.h:157
bitset_ext
Definition absolute.h:40
etl::enable_if<!etl::is_same< T, etl::istring >::value &&!etl::is_same< T, etl::string_view >::value, constetl::istring & >::type to_string(const T value, etl::istring &str, bool append=false)
Definition to_string.h:50
Definition basic_format_spec.h:49
Definition basic_format_spec.h:105
Definition basic_format_spec.h:72
Definition basic_format_spec.h:127
Definition basic_format_spec.h:83
Definition basic_format_spec.h:132
Definition basic_format_spec.h:116
Definition basic_format_spec.h:94
Definition basic_format_spec.h:60