Embedded Template Library 1.0
Loading...
Searching...
No Matches
basic_format_spec.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) 2019 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_FORMAT_SPEC_INCLUDED
32#define ETL_BASIC_FORMAT_SPEC_INCLUDED
33
35
36#include "platform.h"
37#include "static_assert.h"
38#include "type_traits.h"
39#include "utility.h"
40
41namespace etl
42{
43 namespace private_basic_format_spec
44 {
45 //*******************************************************
46 // Structures returned by stream formatting manipulators.
47 //*******************************************************
48 struct base_spec
49 {
50 ETL_CONSTEXPR base_spec(uint_least8_t base_)
51 : base(base_)
52 {
53 }
54
55 const uint_least8_t base;
56 };
57
58 //*********************************
59 struct width_spec
60 {
61 ETL_CONSTEXPR width_spec(uint_least8_t width_)
62 : width(width_)
63 {
64 }
65
66 const uint_least8_t width;
67 };
68
69 //*********************************
70 template <typename TChar>
71 struct fill_spec
72 {
73 ETL_CONSTEXPR fill_spec(TChar fill_)
74 : fill(fill_)
75 {
76 }
77
78 const TChar fill;
79 };
80
81 //*********************************
82 struct precision_spec
83 {
84 ETL_CONSTEXPR precision_spec(uint_least8_t precision_)
85 : precision(precision_)
86 {
87 }
88
89 const uint_least8_t precision;
90 };
91
92 //*********************************
93 struct uppercase_spec
94 {
95 ETL_CONSTEXPR uppercase_spec(bool upper_case_)
96 : upper_case(upper_case_)
97 {
98 }
99
100 const bool upper_case;
101 };
102
103 //*********************************
104 struct boolalpha_spec
105 {
106 ETL_CONSTEXPR boolalpha_spec(bool boolalpha_)
107 : boolalpha(boolalpha_)
108 {
109 }
110
111 const bool boolalpha;
112 };
113
114 //*********************************
115 struct showbase_spec
116 {
117 ETL_CONSTEXPR showbase_spec(bool show_base_)
118 : show_base(show_base_)
119 {
120 }
121
122 const bool show_base;
123 };
124
125 //*********************************
127 {
128 };
129
130 //*********************************
132 {
133 };
134 } // namespace private_basic_format_spec
135
136 //***************************************************************************
137 // Stream formatting manipulators.
138 //***************************************************************************
139 static ETL_CONSTEXPR private_basic_format_spec::base_spec setbase(uint32_t base)
140 {
142 }
143
144 //*********************************
145 static ETL_CONSTEXPR private_basic_format_spec::width_spec setw(uint32_t width)
146 {
147 return private_basic_format_spec::width_spec(width);
148 }
149
150 //*********************************
151 template <typename TChar>
152 static ETL_CONSTEXPR private_basic_format_spec::fill_spec<TChar> setfill(TChar fill)
153 {
155 }
156
157 //*********************************
158 static ETL_CONSTEXPR private_basic_format_spec::precision_spec setprecision(uint32_t precision)
159 {
161 }
162
163 //*********************************
164 static ETL_CONSTANT private_basic_format_spec::base_spec bin(2U);
165
166 //*********************************
167 static ETL_CONSTANT private_basic_format_spec::base_spec oct(8U);
168
169 //*********************************
170 static ETL_CONSTANT private_basic_format_spec::base_spec dec(10U);
171
172 //*********************************
173 static ETL_CONSTANT private_basic_format_spec::base_spec hex(16U);
174
175 //*********************************
177
178 //*********************************
180
181 //*********************************
182 static ETL_CONSTANT private_basic_format_spec::boolalpha_spec boolalpha(true);
183
184 //*********************************
185 static ETL_CONSTANT private_basic_format_spec::boolalpha_spec noboolalpha(false);
186
187 //*********************************
188 static ETL_CONSTANT private_basic_format_spec::uppercase_spec uppercase(true);
189
190 //*********************************
191 static ETL_CONSTANT private_basic_format_spec::uppercase_spec nouppercase(false);
192
193 //*********************************
194 static ETL_CONSTANT private_basic_format_spec::showbase_spec showbase(true);
195
196 //*********************************
197 static ETL_CONSTANT private_basic_format_spec::showbase_spec noshowbase(false);
198
199 //***************************************************************************
201 //***************************************************************************
202 template <typename TString>
204 {
205 public:
206
207 //***************************************************************************
209 //***************************************************************************
210 ETL_CONSTEXPR basic_format_spec() ETL_NOEXCEPT
211 : base_(10U)
212 , width_(0U)
213 , precision_(0U)
214 , upper_case_(false)
215 , left_justified_(false)
216 , boolalpha_(false)
217 , show_base_(false)
218 , fill_(typename TString::value_type(' '))
219 {
220 }
221
222 //***************************************************************************
224 //***************************************************************************
225 ETL_CONSTEXPR basic_format_spec(uint_least8_t base__, uint_least8_t width__, uint_least8_t precision__, bool upper_case__, bool left_justified__,
226 bool boolalpha__, bool show_base__, typename TString::value_type fill__) ETL_NOEXCEPT
227 : base_(base__)
228 , width_(width__)
229 , precision_(precision__)
230 , upper_case_(upper_case__)
231 , left_justified_(left_justified__)
232 , boolalpha_(boolalpha__)
233 , show_base_(show_base__)
234 , fill_(fill__)
235 {
236 }
237
238 //***************************************************************************
240 //***************************************************************************
241 ETL_CONSTEXPR14 void clear() ETL_NOEXCEPT
242 {
243 base_ = 10U;
244 width_ = 0U;
245 precision_ = 0U;
246 upper_case_ = false;
247 left_justified_ = false;
248 boolalpha_ = false;
249 show_base_ = false;
250 fill_ = typename TString::value_type(' ');
251 }
252
253 //***************************************************************************
256 //***************************************************************************
257 ETL_CONSTEXPR14 basic_format_spec& base(uint32_t b) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
258 {
259 base_ = static_cast<uint_least8_t>(b);
260 return *this;
261 }
262
263#if ETL_USING_CPP11
265 ETL_CONSTEXPR14 basic_format_spec&& base(uint32_t b) ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
266 {
267 base_ = static_cast<uint_least8_t>(b);
268 return etl::move(*this);
269 }
270#endif
271
272 //***************************************************************************
275 //***************************************************************************
276 ETL_CONSTEXPR14 basic_format_spec& binary() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
277 {
278 return base(2);
279 }
280
281#if ETL_USING_CPP11
283 ETL_CONSTEXPR14 basic_format_spec&& binary() ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
284 {
285 return etl::move(base(2));
286 }
287#endif
288
289 //***************************************************************************
292 //***************************************************************************
293 ETL_CONSTEXPR14 basic_format_spec& octal() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
294 {
295 return base(8);
296 }
297
298#if ETL_USING_CPP11
300 ETL_CONSTEXPR14 basic_format_spec&& octal() ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
301 {
302 return etl::move(base(8));
303 }
304#endif
305
306 //***************************************************************************
309 //***************************************************************************
310 ETL_CONSTEXPR14 basic_format_spec& decimal() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
311 {
312 return base(10);
313 }
314
315#if ETL_USING_CPP11
317 ETL_CONSTEXPR14 basic_format_spec&& decimal() ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
318 {
319 return etl::move(base(10));
320 }
321#endif
322
323 //***************************************************************************
326 //***************************************************************************
327 ETL_CONSTEXPR14 basic_format_spec& hex() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
328 {
329 return base(16);
330 }
331
332#if ETL_USING_CPP11
334 ETL_CONSTEXPR14 basic_format_spec&& hex() ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
335 {
336 return etl::move(base(16));
337 }
338#endif
339
340 //***************************************************************************
342 //***************************************************************************
343 ETL_CONSTEXPR uint32_t get_base() const ETL_NOEXCEPT
344 {
345 return base_;
346 }
347
348 //***************************************************************************
351 //***************************************************************************
352 ETL_CONSTEXPR14 basic_format_spec& show_base(bool b) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
353 {
354 show_base_ = b;
355 return *this;
356 }
357
358#if ETL_USING_CPP11
360 ETL_CONSTEXPR14 basic_format_spec&& show_base(bool b) ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
361 {
362 show_base_ = b;
363 return etl::move(*this);
364 }
365#endif
366
367 //***************************************************************************
369 //***************************************************************************
370 ETL_CONSTEXPR bool is_show_base() const ETL_NOEXCEPT
371 {
372 return show_base_;
373 }
374
375 //***************************************************************************
378 //***************************************************************************
379 ETL_CONSTEXPR14 basic_format_spec& width(uint32_t w) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
380 {
381 width_ = static_cast<uint_least8_t>(w);
382 return *this;
383 }
384
385#if ETL_USING_CPP11
387 ETL_CONSTEXPR14 basic_format_spec&& width(uint32_t w) ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
388 {
389 width_ = static_cast<uint_least8_t>(w);
390 return etl::move(*this);
391 }
392#endif
393
394 //***************************************************************************
396 //***************************************************************************
397 ETL_CONSTEXPR uint32_t get_width() const ETL_NOEXCEPT
398 {
399 return width_;
400 }
401
402 //***************************************************************************
405 //***************************************************************************
406 ETL_CONSTEXPR14 basic_format_spec& precision(uint32_t p) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
407 {
408 precision_ = static_cast<uint_least8_t>(p);
409 return *this;
410 }
411
412#if ETL_USING_CPP11
414 ETL_CONSTEXPR14 basic_format_spec&& precision(uint32_t p) ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
415 {
416 precision_ = static_cast<uint_least8_t>(p);
417 return etl::move(*this);
418 }
419#endif
420
421 //***************************************************************************
423 //***************************************************************************
424 ETL_CONSTEXPR uint32_t get_precision() const ETL_NOEXCEPT
425 {
426 return precision_;
427 }
428
429 //***************************************************************************
432 //***************************************************************************
433 ETL_CONSTEXPR14 basic_format_spec& upper_case(bool u) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
434 {
435 upper_case_ = u;
436 return *this;
437 }
438
439#if ETL_USING_CPP11
441 ETL_CONSTEXPR14 basic_format_spec&& upper_case(bool u) ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
442 {
443 upper_case_ = u;
444 return etl::move(*this);
445 }
446#endif
447
448 //***************************************************************************
450 //***************************************************************************
451 ETL_CONSTEXPR bool is_upper_case() const ETL_NOEXCEPT
452 {
453 return upper_case_;
454 }
455
456 //***************************************************************************
459 //***************************************************************************
460 ETL_CONSTEXPR14 basic_format_spec& fill(typename TString::value_type c) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
461 {
462 fill_ = c;
463 return *this;
464 }
465
466#if ETL_USING_CPP11
468 ETL_CONSTEXPR14 basic_format_spec&& fill(typename TString::value_type c) ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
469 {
470 fill_ = c;
471 return etl::move(*this);
472 }
473#endif
474
475 //***************************************************************************
477 //***************************************************************************
478 ETL_CONSTEXPR typename TString::value_type get_fill() const ETL_NOEXCEPT
479 {
480 return fill_;
481 }
482
483 //***************************************************************************
486 //***************************************************************************
487 ETL_CONSTEXPR14 basic_format_spec& left() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
488 {
489 left_justified_ = true;
490 return *this;
491 }
492
493#if ETL_USING_CPP11
495 ETL_CONSTEXPR14 basic_format_spec&& left() ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
496 {
497 left_justified_ = true;
498 return etl::move(*this);
499 }
500#endif
501
502 //***************************************************************************
504 //***************************************************************************
505 ETL_CONSTEXPR bool is_left() const ETL_NOEXCEPT
506 {
507 return left_justified_;
508 }
509
510 //***************************************************************************
513 //***************************************************************************
514 ETL_CONSTEXPR14 basic_format_spec& right() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
515 {
516 left_justified_ = false;
517 return *this;
518 }
519
520#if ETL_USING_CPP11
522 ETL_CONSTEXPR14 basic_format_spec&& right() ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
523 {
524 left_justified_ = false;
525 return etl::move(*this);
526 }
527#endif
528
529 //***************************************************************************
531 //***************************************************************************
532 ETL_CONSTEXPR bool is_right() const ETL_NOEXCEPT
533 {
534 return !left_justified_;
535 }
536
537 //***************************************************************************
540 //***************************************************************************
541 ETL_CONSTEXPR14 basic_format_spec& boolalpha(bool b) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
542 {
543 boolalpha_ = b;
544 return *this;
545 }
546
547#if ETL_USING_CPP11
549 ETL_CONSTEXPR14 basic_format_spec&& boolalpha(bool b) ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
550 {
551 boolalpha_ = b;
552 return etl::move(*this);
553 }
554#endif
555
556 //***************************************************************************
558 //***************************************************************************
559 ETL_CONSTEXPR bool is_boolalpha() const ETL_NOEXCEPT
560 {
561 return boolalpha_;
562 }
563
564 //***************************************************************************
566 //***************************************************************************
567 ETL_CONSTEXPR friend bool operator==(const basic_format_spec& lhs, const basic_format_spec& rhs)
568 {
569 return (lhs.base_ == rhs.base_) && (lhs.width_ == rhs.width_) && (lhs.precision_ == rhs.precision_) && (lhs.upper_case_ == rhs.upper_case_)
570 && (lhs.left_justified_ == rhs.left_justified_) && (lhs.boolalpha_ == rhs.boolalpha_) && (lhs.show_base_ == rhs.show_base_)
571 && (lhs.fill_ == rhs.fill_);
572 }
573
574 //***************************************************************************
576 //***************************************************************************
577 ETL_CONSTEXPR friend bool operator!=(const basic_format_spec& lhs, const basic_format_spec& rhs)
578 {
579 return !(lhs == rhs);
580 }
581
582 private:
583
584 uint_least8_t base_;
585 uint_least8_t width_;
586 uint_least8_t precision_;
587 bool upper_case_;
588 bool left_justified_;
589 bool boolalpha_;
590 bool show_base_;
591 typename TString::value_type fill_;
592 };
593} // namespace etl
594
595#endif
basic_format_spec
Definition basic_format_spec.h:204
ETL_CONSTEXPR14 basic_format_spec & right() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:514
ETL_CONSTEXPR14 basic_format_spec & base(uint32_t b) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:257
ETL_CONSTEXPR14 basic_format_spec & fill(typename TString::value_type c) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:460
ETL_CONSTEXPR uint32_t get_base() const ETL_NOEXCEPT
Gets the base.
Definition basic_format_spec.h:343
ETL_CONSTEXPR14 basic_format_spec & width(uint32_t w) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:379
ETL_CONSTEXPR bool is_show_base() const ETL_NOEXCEPT
Gets the show base flag.
Definition basic_format_spec.h:370
ETL_CONSTEXPR bool is_right() const ETL_NOEXCEPT
Gets the right justify flag.
Definition basic_format_spec.h:532
ETL_CONSTEXPR friend bool operator!=(const basic_format_spec &lhs, const basic_format_spec &rhs)
Inequality operator.
Definition basic_format_spec.h:577
ETL_CONSTEXPR14 basic_format_spec & upper_case(bool u) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:433
ETL_CONSTEXPR bool is_upper_case() const ETL_NOEXCEPT
Gets the upper case flag.
Definition basic_format_spec.h:451
ETL_CONSTEXPR14 basic_format_spec & precision(uint32_t p) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:406
ETL_CONSTEXPR14 basic_format_spec & hex() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:327
ETL_CONSTEXPR friend bool operator==(const basic_format_spec &lhs, const basic_format_spec &rhs)
Equality operator.
Definition basic_format_spec.h:567
ETL_CONSTEXPR bool is_left() const ETL_NOEXCEPT
Gets the left justify flag.
Definition basic_format_spec.h:505
ETL_CONSTEXPR14 basic_format_spec & binary() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:276
ETL_CONSTEXPR basic_format_spec() ETL_NOEXCEPT
Default constructor.
Definition basic_format_spec.h:210
ETL_CONSTEXPR basic_format_spec(uint_least8_t base__, uint_least8_t width__, uint_least8_t precision__, bool upper_case__, bool left_justified__, bool boolalpha__, bool show_base__, typename TString::value_type fill__) ETL_NOEXCEPT
Constructor.
Definition basic_format_spec.h:225
ETL_CONSTEXPR14 basic_format_spec & octal() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:293
ETL_CONSTEXPR14 void clear() ETL_NOEXCEPT
Clears the format spec back to default.
Definition basic_format_spec.h:241
ETL_CONSTEXPR14 basic_format_spec & left() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:487
ETL_CONSTEXPR14 basic_format_spec & boolalpha(bool b) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:541
ETL_CONSTEXPR TString::value_type get_fill() const ETL_NOEXCEPT
Gets the fill character.
Definition basic_format_spec.h:478
ETL_CONSTEXPR bool is_boolalpha() const ETL_NOEXCEPT
Gets the boolalpha flag.
Definition basic_format_spec.h:559
ETL_CONSTEXPR uint32_t get_width() const ETL_NOEXCEPT
Gets the width.
Definition basic_format_spec.h:397
ETL_CONSTEXPR14 basic_format_spec & decimal() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:310
ETL_CONSTEXPR14 basic_format_spec & show_base(bool b) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:352
ETL_CONSTEXPR uint32_t get_precision() const ETL_NOEXCEPT
Gets the precision.
Definition basic_format_spec.h:424
bitset_ext
Definition absolute.h:40
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