Embedded Template Library  1.0
basic_string_stream.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 https://github.com/ETLCPP/etl
8 https://www.etlcpp.com
9 
10 Copyright(c) 2020 jwellbelove
11 
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files(the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions :
18 
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21 
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29 ******************************************************************************/
30 
31 #ifndef ETL_BASIC_STRING_STREAM_INCLUDED
32 #define ETL_BASIC_STRING_STREAM_INCLUDED
33 
35 
36 #include "platform.h"
37 
38 namespace etl
39 {
40  template <typename TFormat, typename TIString, typename TStringView>
42  {
43  public:
44 
45  typedef TFormat format_spec_type;
46  typedef TIString istring_type;
47  typedef typename TIString::value_type value_type;
48  typedef typename TIString::const_pointer const_pointer;
49 
50  //*************************************************************************
52  //*************************************************************************
53  explicit basic_string_stream(TIString& text_)
54  : text(text_)
55  {
56  }
57 
58  //*************************************************************************
60  //*************************************************************************
61  basic_string_stream(TIString& text_, const TFormat& spec_)
62  : text(text_)
63  , spec(spec_)
64  {
65  }
66 
67  //*************************************************************************
69  //*************************************************************************
70  void set_format(const TFormat& spec_)
71  {
72  spec = spec_;
73  }
74 
75  //*************************************************************************
77  //*************************************************************************
78  const TFormat& get_format() const
79  {
80  return spec;
81  }
82 
83  //*************************************************************************
85  //*************************************************************************
86  TIString& str()
87  {
88  return text;
89  }
90 
91  //*************************************************************************
93  //*************************************************************************
94  const TIString& str() const
95  {
96  return text;
97  }
98 
99  //*************************************************************************
101  //*************************************************************************
102  void str(const value_type* p)
103  {
104  text.assign(p);
105  }
106 
107  //*************************************************************************
109  //*************************************************************************
110  void str(const TIString& is)
111  {
112  text.assign(is);
113  }
114 
115  //*************************************************************************
117  //*************************************************************************
118 
119  //*********************************
121  //*********************************
122  friend basic_string_stream& operator <<(basic_string_stream& ss, const TFormat& spec)
123  {
124  ss.spec = spec;
125  return ss;
126  }
127 
128  //*********************************
130  //*********************************
132  {
133  ss.spec.base(spec.base);
134  return ss;
135  }
136 
137  //*********************************
139  //*********************************
141  {
142  ss.spec.width(spec.width);
143  return ss;
144  }
145 
146  //*********************************
148  //*********************************
149  template <typename TChar>
151  {
152  ss.spec.fill(spec.fill);
153  return ss;
154  }
155 
156  //*********************************
158  //*********************************
160  {
161  ss.spec.precision(spec.precision);
162  return ss;
163  }
164 
165  //*********************************
167  //*********************************
169  {
170  ss.spec.boolalpha(spec.boolalpha);
171  return ss;
172  }
173 
174  //*********************************
176  //*********************************
178  {
179  ss.spec.upper_case(spec.upper_case);
180  return ss;
181  }
182 
183  //*********************************
185  //*********************************
187  {
188  ss.spec.show_base(spec.show_base);
189  return ss;
190  }
191 
192  //*********************************
194  //*********************************
196  {
197  ss.spec.left();
198  return ss;
199  }
200 
201  //*********************************
203  //*********************************
205  {
206  ss.spec.right();
207  return ss;
208  }
209 
210  //*********************************
212  //*********************************
213  friend basic_string_stream& operator <<(basic_string_stream& ss, TStringView view)
214  {
215  etl::to_string(view, ss.text, ss.spec, true);
216  return ss;
217  }
218 
219  //*********************************
221  //*********************************
223  {
224  TStringView view(p);
225  ss << view;
226  return ss;
227  }
228 
229  //*********************************
231  //*********************************
232  friend basic_string_stream& operator <<(basic_string_stream& ss, const TIString& text)
233  {
234  etl::to_string(text, ss.text, ss.spec, true);
235  return ss;
236  }
237 
238  //*********************************
240  //*********************************
241  template <template <size_t> class TString, size_t SIZE>
242  friend basic_string_stream& operator <<(basic_string_stream& ss, const TString<SIZE>& text)
243  {
244  const TIString& itext = text;
245  etl::to_string(itext, ss.str(), ss.get_format(), true);
246  return ss;
247  }
248 
249  //*********************************
251  //*********************************
252  template <typename T>
254  {
255  etl::to_string(value, ss.text, ss.spec, true);
256  return ss;
257  }
258 
259  private:
260 
261  TIString& text;
262  TFormat spec;
263 
264  basic_string_stream(const basic_string_stream&) ETL_DELETE;
265  basic_string_stream& operator =(const basic_string_stream&) ETL_DELETE;
266  };
267 }
268 
269 #endif
Definition: basic_string_stream.h:42
void str(const value_type *p)
Resets the stream to the supplied string.
Definition: basic_string_stream.h:102
basic_string_stream(TIString &text_)
Construct from text.
Definition: basic_string_stream.h:53
friend basic_string_stream & operator<<(basic_string_stream &ss, const TFormat &spec)
Stream operators.
Definition: basic_string_stream.h:122
void str(const TIString &is)
Resets the stream to the supplied string.
Definition: basic_string_stream.h:110
TIString & str()
Get a reference to the current string.
Definition: basic_string_stream.h:86
const TIString & str() const
Get a const reference to the current string.
Definition: basic_string_stream.h:94
void set_format(const TFormat &spec_)
Set the format spec.
Definition: basic_string_stream.h:70
const TFormat & get_format() const
Get a const reference to the format spec.
Definition: basic_string_stream.h:78
basic_string_stream(TIString &text_, const TFormat &spec_)
Construct from text and format spec.
Definition: basic_string_stream.h:61
Definition: absolute.h:37
etl::enable_if<!etl::is_same< T, etl::istring >::value &&!etl::is_same< T, etl::string_view >::value, const etl::istring & >::type to_string(const T value, etl::istring &str, bool append=false)
Definition: to_string.h:50
Definition: basic_format_spec.h:48
Definition: basic_format_spec.h:104
Definition: basic_format_spec.h:71
Definition: basic_format_spec.h:126
Definition: basic_format_spec.h:82
Definition: basic_format_spec.h:131
Definition: basic_format_spec.h:115
Definition: basic_format_spec.h:93
Definition: basic_format_spec.h:59