Embedded Template Library  1.0
basic_format_spec.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) 2019 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_FORMAT_SPEC_INCLUDED
32 #define ETL_BASIC_FORMAT_SPEC_INCLUDED
33 
35 
36 #include "platform.h"
37 #include "type_traits.h"
38 #include "static_assert.h"
39 
40 namespace etl
41 {
42  namespace private_basic_format_spec
43  {
44  //*******************************************************
45  // Structures returned by stream formatting manipulators.
46  //*******************************************************
47  struct base_spec
48  {
49  ETL_CONSTEXPR base_spec(uint_least8_t base_)
50  : base(base_)
51  {
52  }
53 
54  const uint_least8_t base;
55  };
56 
57  //*********************************
58  struct width_spec
59  {
60  ETL_CONSTEXPR width_spec(uint_least8_t width_)
61  : width(width_)
62  {
63  }
64 
65  const uint_least8_t width;
66  };
67 
68  //*********************************
69  template <typename TChar>
70  struct fill_spec
71  {
72  ETL_CONSTEXPR fill_spec(TChar fill_)
73  : fill(fill_)
74  {
75  }
76 
77  const TChar fill;
78  };
79 
80  //*********************************
82  {
83  ETL_CONSTEXPR precision_spec(uint_least8_t precision_)
84  : precision(precision_)
85  {
86  }
87 
88  const uint_least8_t precision;
89  };
90 
91  //*********************************
93  {
94  ETL_CONSTEXPR uppercase_spec(bool upper_case_)
95  : upper_case(upper_case_)
96  {
97  }
98 
99  const bool upper_case;
100  };
101 
102  //*********************************
104  {
105  ETL_CONSTEXPR boolalpha_spec(bool boolalpha_)
106  : boolalpha(boolalpha_)
107  {
108  }
109 
110  const bool boolalpha;
111  };
112 
113  //*********************************
115  {
116  ETL_CONSTEXPR showbase_spec(bool show_base_)
117  : show_base(show_base_)
118  {
119  }
120 
121  const bool show_base;
122  };
123 
124  //*********************************
125  struct left_spec
126  {
127  };
128 
129  //*********************************
130  struct right_spec
131  {
132  };
133  }
134 
135  //***************************************************************************
136  // Stream formatting manipulators.
137  //***************************************************************************
138  static ETL_CONSTEXPR private_basic_format_spec::base_spec setbase(uint32_t base)
139  {
141  }
142 
143  //*********************************
144  static ETL_CONSTEXPR private_basic_format_spec::width_spec setw(uint32_t width)
145  {
146  return private_basic_format_spec::width_spec(width);
147  }
148 
149  //*********************************
150  template <typename TChar>
151  static ETL_CONSTEXPR private_basic_format_spec::fill_spec<TChar> setfill(TChar fill)
152  {
153  return private_basic_format_spec::fill_spec<TChar>(fill);
154  }
155 
156  //*********************************
157  static ETL_CONSTEXPR private_basic_format_spec::precision_spec setprecision(uint32_t precision)
158  {
159  return private_basic_format_spec::precision_spec(precision);
160  }
161 
162  //*********************************
163  static ETL_CONSTANT private_basic_format_spec::base_spec bin = { 2U };
164 
165  //*********************************
166  static ETL_CONSTANT private_basic_format_spec::base_spec oct = { 8U };
167 
168  //*********************************
169  static ETL_CONSTANT private_basic_format_spec::base_spec dec = { 10U };
170 
171  //*********************************
172  static ETL_CONSTANT private_basic_format_spec::base_spec hex = { 16U };
173 
174  //*********************************
175  static ETL_CONSTANT private_basic_format_spec::left_spec left;
176 
177  //*********************************
178  static ETL_CONSTANT private_basic_format_spec::right_spec right;
179 
180  //*********************************
181  static ETL_CONSTANT private_basic_format_spec::boolalpha_spec boolalpha = { true };
182 
183  //*********************************
184  static ETL_CONSTANT private_basic_format_spec::boolalpha_spec noboolalpha = { false };
185 
186  //*********************************
187  static ETL_CONSTANT private_basic_format_spec::uppercase_spec uppercase = { true };
188 
189  //*********************************
190  static ETL_CONSTANT private_basic_format_spec::uppercase_spec nouppercase = { false };
191 
192  //*********************************
193  static ETL_CONSTANT private_basic_format_spec::showbase_spec showbase = { true };
194 
195  //*********************************
196  static ETL_CONSTANT private_basic_format_spec::showbase_spec noshowbase = { false };
197 
198  //***************************************************************************
200  //***************************************************************************
201  template <typename TString>
203  {
204  public:
205 
206  //***************************************************************************
208  //***************************************************************************
209  ETL_CONSTEXPR basic_format_spec()
210  : base_(10U)
211  , width_(0U)
212  , precision_(0U)
213  , upper_case_(false)
214  , left_justified_(false)
215  , boolalpha_(false)
216  , show_base_(false)
217  , fill_(typename TString::value_type(' '))
218  {
219  }
220 
221  //***************************************************************************
223  //***************************************************************************
224  ETL_CONSTEXPR basic_format_spec(uint_least8_t base__,
225  uint_least8_t width__,
226  uint_least8_t precision__,
227  bool upper_case__,
228  bool left_justified__,
229  bool boolalpha__,
230  bool show_base__,
231  typename TString::value_type fill__)
232  : base_(base__)
233  , width_(width__)
234  , precision_(precision__)
235  , upper_case_(upper_case__)
236  , left_justified_(left_justified__)
237  , boolalpha_(boolalpha__)
238  , show_base_(show_base__)
239  , fill_(fill__)
240  {
241  }
242 
243  //***************************************************************************
245  //***************************************************************************
246  ETL_CONSTEXPR14 void clear()
247  {
248  base_ = 10U;
249  width_ = 0U;
250  precision_ = 0U;
251  upper_case_ = false;
252  left_justified_ = false;
253  boolalpha_ = false;
254  show_base_ = false;
255  fill_ = typename TString::value_type(' ');
256  }
257 
258  //***************************************************************************
261  //***************************************************************************
262  ETL_CONSTEXPR14 basic_format_spec& base(uint32_t b)
263  {
264  base_ = static_cast<uint_least8_t>(b);
265  return *this;
266  }
267 
268  //***************************************************************************
271  //***************************************************************************
272  ETL_CONSTEXPR14 basic_format_spec& binary()
273  {
274  base(2);
275  return *this;
276  }
277 
278  //***************************************************************************
281  //***************************************************************************
282  ETL_CONSTEXPR14 basic_format_spec& octal()
283  {
284  base(8);
285  return *this;
286  }
287 
288  //***************************************************************************
291  //***************************************************************************
292  ETL_CONSTEXPR14 basic_format_spec& decimal()
293  {
294  base(10);
295  return *this;
296  }
297 
298  //***************************************************************************
301  //***************************************************************************
302  ETL_CONSTEXPR14 basic_format_spec& hex()
303  {
304  base(16);
305  return *this;
306  }
307 
308  //***************************************************************************
310  //***************************************************************************
311  ETL_CONSTEXPR uint32_t get_base() const
312  {
313  return base_;
314  }
315 
316  //***************************************************************************
319  //***************************************************************************
320  ETL_CONSTEXPR14 basic_format_spec& show_base(bool b)
321  {
322  show_base_ = b;
323  return *this;
324  }
325 
326  //***************************************************************************
328  //***************************************************************************
329  ETL_CONSTEXPR bool is_show_base() const
330  {
331  return show_base_;
332  }
333 
334  //***************************************************************************
337  //***************************************************************************
338  ETL_CONSTEXPR14 basic_format_spec& width(uint32_t w)
339  {
340  width_ = static_cast<uint_least8_t>(w);
341  return *this;
342  }
343 
344  //***************************************************************************
346  //***************************************************************************
347  ETL_CONSTEXPR uint32_t get_width() const
348  {
349  return width_;
350  }
351 
352  //***************************************************************************
355  //***************************************************************************
356  ETL_CONSTEXPR14 basic_format_spec& precision(uint32_t p)
357  {
358  precision_ = static_cast<uint_least8_t>(p);
359  return *this;
360  }
361 
362  //***************************************************************************
364  //***************************************************************************
365  ETL_CONSTEXPR uint32_t get_precision() const
366  {
367  return precision_;
368  }
369 
370  //***************************************************************************
373  //***************************************************************************
374  ETL_CONSTEXPR14 basic_format_spec& upper_case(bool u)
375  {
376  upper_case_ = u;
377  return *this;
378  }
379 
380  //***************************************************************************
382  //***************************************************************************
383  ETL_CONSTEXPR bool is_upper_case() const
384  {
385  return upper_case_;
386  }
387 
388  //***************************************************************************
391  //***************************************************************************
392  ETL_CONSTEXPR14 basic_format_spec& fill(typename TString::value_type c)
393  {
394  fill_ = c;
395  return *this;
396  }
397 
398  //***************************************************************************
400  //***************************************************************************
401  ETL_CONSTEXPR typename TString::value_type get_fill() const
402  {
403  return fill_;
404  }
405 
406  //***************************************************************************
409  //***************************************************************************
410  ETL_CONSTEXPR14 basic_format_spec& left()
411  {
412  left_justified_ = true;
413  return *this;
414  }
415 
416  //***************************************************************************
418  //***************************************************************************
419  ETL_CONSTEXPR bool is_left() const
420  {
421  return left_justified_;
422  }
423 
424  //***************************************************************************
427  //***************************************************************************
428  ETL_CONSTEXPR14 basic_format_spec& right()
429  {
430  left_justified_ = false;
431  return *this;
432  }
433 
434  //***************************************************************************
436  //***************************************************************************
437  ETL_CONSTEXPR bool is_right() const
438  {
439  return !left_justified_;
440  }
441 
442  //***************************************************************************
445  //***************************************************************************
446  ETL_CONSTEXPR14 basic_format_spec& boolalpha(bool b)
447  {
448  boolalpha_ = b;
449  return *this;
450  }
451 
452  //***************************************************************************
454  //***************************************************************************
455  ETL_CONSTEXPR bool is_boolalpha() const
456  {
457  return boolalpha_;
458  }
459 
460  //***************************************************************************
462  //***************************************************************************
463  ETL_CONSTEXPR friend bool operator ==(const basic_format_spec& lhs, const basic_format_spec& rhs)
464  {
465  return (lhs.base_ == rhs.base_) &&
466  (lhs.width_ == rhs.width_) &&
467  (lhs.precision_ == rhs.precision_) &&
468  (lhs.upper_case_ == rhs.upper_case_) &&
469  (lhs.left_justified_ == rhs.left_justified_) &&
470  (lhs.boolalpha_ == rhs.boolalpha_) &&
471  (lhs.show_base_ == rhs.show_base_) &&
472  (lhs.fill_ == rhs.fill_);
473  }
474 
475  //***************************************************************************
477  //***************************************************************************
478  ETL_CONSTEXPR friend bool operator !=(const basic_format_spec& lhs, const basic_format_spec& rhs)
479  {
480  return !(lhs == rhs);
481  }
482 
483  private:
484 
485  uint_least8_t base_;
486  uint_least8_t width_;
487  uint_least8_t precision_;
488  bool upper_case_;
489  bool left_justified_;
490  bool boolalpha_;
491  bool show_base_;
492  typename TString::value_type fill_;
493  };
494 }
495 
496 #endif
basic_format_spec
Definition: basic_format_spec.h:203
ETL_CONSTEXPR bool is_boolalpha() const
Gets the boolalpha flag.
Definition: basic_format_spec.h:455
ETL_CONSTEXPR14 basic_format_spec & fill(typename TString::value_type c)
Definition: basic_format_spec.h:392
ETL_CONSTEXPR uint32_t get_width() const
Gets the width.
Definition: basic_format_spec.h:347
ETL_CONSTEXPR14 basic_format_spec & upper_case(bool u)
Definition: basic_format_spec.h:374
ETL_CONSTEXPR friend bool operator!=(const basic_format_spec &lhs, const basic_format_spec &rhs)
Inequality operator.
Definition: basic_format_spec.h:478
ETL_CONSTEXPR14 basic_format_spec & show_base(bool b)
Definition: basic_format_spec.h:320
ETL_CONSTEXPR14 basic_format_spec & right()
Definition: basic_format_spec.h:428
ETL_CONSTEXPR14 void clear()
Clears the format spec back to default.
Definition: basic_format_spec.h:246
ETL_CONSTEXPR friend bool operator==(const basic_format_spec &lhs, const basic_format_spec &rhs)
Equality operator.
Definition: basic_format_spec.h:463
ETL_CONSTEXPR14 basic_format_spec & boolalpha(bool b)
Definition: basic_format_spec.h:446
ETL_CONSTEXPR uint32_t get_base() const
Gets the base.
Definition: basic_format_spec.h:311
ETL_CONSTEXPR14 basic_format_spec & octal()
Definition: basic_format_spec.h:282
ETL_CONSTEXPR14 basic_format_spec & decimal()
Definition: basic_format_spec.h:292
ETL_CONSTEXPR TString::value_type get_fill() const
Gets the fill character.
Definition: basic_format_spec.h:401
ETL_CONSTEXPR14 basic_format_spec & binary()
Definition: basic_format_spec.h:272
ETL_CONSTEXPR bool is_show_base() const
Gets the show base flag.
Definition: basic_format_spec.h:329
ETL_CONSTEXPR bool is_right() const
Gets the right justify flag.
Definition: basic_format_spec.h:437
ETL_CONSTEXPR14 basic_format_spec & width(uint32_t w)
Definition: basic_format_spec.h:338
ETL_CONSTEXPR uint32_t get_precision() const
Gets the precision.
Definition: basic_format_spec.h:365
ETL_CONSTEXPR14 basic_format_spec & left()
Definition: basic_format_spec.h:410
ETL_CONSTEXPR14 basic_format_spec & precision(uint32_t p)
Definition: basic_format_spec.h:356
ETL_CONSTEXPR14 basic_format_spec & base(uint32_t b)
Definition: basic_format_spec.h:262
ETL_CONSTEXPR basic_format_spec()
Default constructor.
Definition: basic_format_spec.h:209
ETL_CONSTEXPR14 basic_format_spec & hex()
Definition: basic_format_spec.h:302
ETL_CONSTEXPR bool is_left() const
Gets the left justify flag.
Definition: basic_format_spec.h:419
ETL_CONSTEXPR bool is_upper_case() const
Gets the upper case flag.
Definition: basic_format_spec.h:383
Definition: absolute.h:37
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