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 "type_traits.h"
38#include "utility.h"
39
40namespace 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 //*********************************
81 struct precision_spec
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 //*********************************
92 struct uppercase_spec
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 //*********************************
103 struct boolalpha_spec
104 {
105 ETL_CONSTEXPR boolalpha_spec(bool boolalpha_)
106 : boolalpha(boolalpha_)
107 {
108 }
109
110 const bool boolalpha;
111 };
112
113 //*********************************
114 struct showbase_spec
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 //*********************************
126 {
127 };
128
129 //*********************************
131 {
132 };
133 } // namespace private_basic_format_spec
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 {
154 }
155
156 //*********************************
157 static ETL_CONSTEXPR private_basic_format_spec::precision_spec setprecision(uint32_t precision)
158 {
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 //*********************************
176
177 //*********************************
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() ETL_NOEXCEPT
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 , scientific_(false)
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__, bool scientific__ = false) 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 , scientific_(scientific__)
236 {
237 }
238
239 //***************************************************************************
241 //***************************************************************************
242 ETL_CONSTEXPR14 void clear() ETL_NOEXCEPT
243 {
244 base_ = 10U;
245 width_ = 0U;
246 precision_ = 0U;
247 upper_case_ = false;
248 left_justified_ = false;
249 boolalpha_ = false;
250 show_base_ = false;
251 scientific_ = false;
252 fill_ = typename TString::value_type(' ');
253 }
254
255 //***************************************************************************
258 //***************************************************************************
259 ETL_CONSTEXPR14 basic_format_spec& base(uint32_t b) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
260 {
261 base_ = static_cast<uint_least8_t>(b);
262 return *this;
263 }
264
265#if ETL_USING_CPP11
267 ETL_CONSTEXPR14 basic_format_spec&& base(uint32_t b) ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
268 {
269 base_ = static_cast<uint_least8_t>(b);
270 return etl::move(*this);
271 }
272#endif
273
274 //***************************************************************************
277 //***************************************************************************
278 ETL_CONSTEXPR14 basic_format_spec& binary() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
279 {
280 return base(2);
281 }
282
283#if ETL_USING_CPP11
285 ETL_CONSTEXPR14 basic_format_spec&& binary() ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
286 {
287 return etl::move(base(2));
288 }
289#endif
290
291 //***************************************************************************
294 //***************************************************************************
295 ETL_CONSTEXPR14 basic_format_spec& octal() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
296 {
297 return base(8);
298 }
299
300#if ETL_USING_CPP11
302 ETL_CONSTEXPR14 basic_format_spec&& octal() ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
303 {
304 return etl::move(base(8));
305 }
306#endif
307
308 //***************************************************************************
311 //***************************************************************************
312 ETL_CONSTEXPR14 basic_format_spec& decimal() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
313 {
314 return base(10);
315 }
316
317#if ETL_USING_CPP11
319 ETL_CONSTEXPR14 basic_format_spec&& decimal() ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
320 {
321 return etl::move(base(10));
322 }
323#endif
324
325 //***************************************************************************
328 //***************************************************************************
329 ETL_CONSTEXPR14 basic_format_spec& hex() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
330 {
331 return base(16);
332 }
333
334#if ETL_USING_CPP11
336 ETL_CONSTEXPR14 basic_format_spec&& hex() ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
337 {
338 return etl::move(base(16));
339 }
340#endif
341
342 //***************************************************************************
344 //***************************************************************************
345 ETL_CONSTEXPR uint32_t get_base() const ETL_NOEXCEPT
346 {
347 return base_;
348 }
349
350 //***************************************************************************
353 //***************************************************************************
354 ETL_CONSTEXPR14 basic_format_spec& show_base(bool b) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
355 {
356 show_base_ = b;
357 return *this;
358 }
359
360#if ETL_USING_CPP11
362 ETL_CONSTEXPR14 basic_format_spec&& show_base(bool b) ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
363 {
364 show_base_ = b;
365 return etl::move(*this);
366 }
367#endif
368
369 //***************************************************************************
371 //***************************************************************************
372 ETL_CONSTEXPR bool is_show_base() const ETL_NOEXCEPT
373 {
374 return show_base_;
375 }
376
377 //***************************************************************************
380 //***************************************************************************
381 ETL_CONSTEXPR14 basic_format_spec& width(uint32_t w) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
382 {
383 width_ = static_cast<uint_least8_t>(w);
384 return *this;
385 }
386
387#if ETL_USING_CPP11
389 ETL_CONSTEXPR14 basic_format_spec&& width(uint32_t w) ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
390 {
391 width_ = static_cast<uint_least8_t>(w);
392 return etl::move(*this);
393 }
394#endif
395
396 //***************************************************************************
398 //***************************************************************************
399 ETL_CONSTEXPR uint32_t get_width() const ETL_NOEXCEPT
400 {
401 return width_;
402 }
403
404 //***************************************************************************
407 //***************************************************************************
408 ETL_CONSTEXPR14 basic_format_spec& precision(uint32_t p) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
409 {
410 precision_ = static_cast<uint_least8_t>(p);
411 return *this;
412 }
413
414#if ETL_USING_CPP11
416 ETL_CONSTEXPR14 basic_format_spec&& precision(uint32_t p) ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
417 {
418 precision_ = static_cast<uint_least8_t>(p);
419 return etl::move(*this);
420 }
421#endif
422
423 //***************************************************************************
425 //***************************************************************************
426 ETL_CONSTEXPR uint32_t get_precision() const ETL_NOEXCEPT
427 {
428 return precision_;
429 }
430
431 //***************************************************************************
434 //***************************************************************************
435 ETL_CONSTEXPR14 basic_format_spec& upper_case(bool u) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
436 {
437 upper_case_ = u;
438 return *this;
439 }
440
441#if ETL_USING_CPP11
443 ETL_CONSTEXPR14 basic_format_spec&& upper_case(bool u) ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
444 {
445 upper_case_ = u;
446 return etl::move(*this);
447 }
448#endif
449
450 //***************************************************************************
452 //***************************************************************************
453 ETL_CONSTEXPR bool is_upper_case() const ETL_NOEXCEPT
454 {
455 return upper_case_;
456 }
457
458 //***************************************************************************
461 //***************************************************************************
462 ETL_CONSTEXPR14 basic_format_spec& fill(typename TString::value_type c) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
463 {
464 fill_ = c;
465 return *this;
466 }
467
468#if ETL_USING_CPP11
470 ETL_CONSTEXPR14 basic_format_spec&& fill(typename TString::value_type c) ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
471 {
472 fill_ = c;
473 return etl::move(*this);
474 }
475#endif
476
477 //***************************************************************************
479 //***************************************************************************
480 ETL_CONSTEXPR typename TString::value_type get_fill() const ETL_NOEXCEPT
481 {
482 return fill_;
483 }
484
485 //***************************************************************************
488 //***************************************************************************
489 ETL_CONSTEXPR14 basic_format_spec& left() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
490 {
491 left_justified_ = true;
492 return *this;
493 }
494
495#if ETL_USING_CPP11
497 ETL_CONSTEXPR14 basic_format_spec&& left() ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
498 {
499 left_justified_ = true;
500 return etl::move(*this);
501 }
502#endif
503
504 //***************************************************************************
506 //***************************************************************************
507 ETL_CONSTEXPR bool is_left() const ETL_NOEXCEPT
508 {
509 return left_justified_;
510 }
511
512 //***************************************************************************
515 //***************************************************************************
516 ETL_CONSTEXPR14 basic_format_spec& right() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
517 {
518 left_justified_ = false;
519 return *this;
520 }
521
522#if ETL_USING_CPP11
524 ETL_CONSTEXPR14 basic_format_spec&& right() ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
525 {
526 left_justified_ = false;
527 return etl::move(*this);
528 }
529#endif
530
531 //***************************************************************************
533 //***************************************************************************
534 ETL_CONSTEXPR bool is_right() const ETL_NOEXCEPT
535 {
536 return !left_justified_;
537 }
538
539 //***************************************************************************
542 //***************************************************************************
543 ETL_CONSTEXPR14 basic_format_spec& boolalpha(bool b) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
544 {
545 boolalpha_ = b;
546 return *this;
547 }
548
549#if ETL_USING_CPP11
551 ETL_CONSTEXPR14 basic_format_spec&& boolalpha(bool b) ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
552 {
553 boolalpha_ = b;
554 return etl::move(*this);
555 }
556#endif
557
558 //***************************************************************************
560 //***************************************************************************
561 ETL_CONSTEXPR bool is_boolalpha() const ETL_NOEXCEPT
562 {
563 return boolalpha_;
564 }
565
566 //***************************************************************************
569 //***************************************************************************
570 ETL_CONSTEXPR14 basic_format_spec& scientific(bool b) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
571 {
572 scientific_ = b;
573 return *this;
574 }
575
576#if ETL_USING_CPP11
578 ETL_CONSTEXPR14 basic_format_spec&& scientific(bool b) ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
579 {
580 scientific_ = b;
581 return etl::move(*this);
582 }
583#endif
584
585 //***************************************************************************
587 //***************************************************************************
588 ETL_CONSTEXPR bool is_scientific() const ETL_NOEXCEPT
589 {
590 return scientific_;
591 }
592
593 //***************************************************************************
595 //***************************************************************************
596 ETL_CONSTEXPR friend bool operator==(const basic_format_spec& lhs, const basic_format_spec& rhs)
597 {
598 return (lhs.base_ == rhs.base_) && (lhs.width_ == rhs.width_) && (lhs.precision_ == rhs.precision_) && (lhs.upper_case_ == rhs.upper_case_)
599 && (lhs.left_justified_ == rhs.left_justified_) && (lhs.boolalpha_ == rhs.boolalpha_) && (lhs.show_base_ == rhs.show_base_)
600 && (lhs.fill_ == rhs.fill_) && (lhs.scientific_ == rhs.scientific_);
601 }
602
603 //***************************************************************************
605 //***************************************************************************
606 ETL_CONSTEXPR friend bool operator!=(const basic_format_spec& lhs, const basic_format_spec& rhs)
607 {
608 return !(lhs == rhs);
609 }
610
611 private:
612
613 uint_least8_t base_;
614 uint_least8_t width_;
615 uint_least8_t precision_;
616 bool upper_case_;
617 bool left_justified_;
618 bool boolalpha_;
619 bool show_base_;
620 typename TString::value_type fill_;
621 bool scientific_;
622 };
623} // namespace etl
624
625#endif
basic_format_spec
Definition basic_format_spec.h:203
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__, bool scientific__=false) ETL_NOEXCEPT
Constructor.
Definition basic_format_spec.h:225
ETL_CONSTEXPR14 basic_format_spec & right() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:516
ETL_CONSTEXPR14 basic_format_spec & base(uint32_t b) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:259
ETL_CONSTEXPR14 basic_format_spec & fill(typename TString::value_type c) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:462
ETL_CONSTEXPR uint32_t get_base() const ETL_NOEXCEPT
Gets the base.
Definition basic_format_spec.h:345
ETL_CONSTEXPR14 basic_format_spec & width(uint32_t w) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:381
ETL_CONSTEXPR bool is_show_base() const ETL_NOEXCEPT
Gets the show base flag.
Definition basic_format_spec.h:372
ETL_CONSTEXPR bool is_right() const ETL_NOEXCEPT
Gets the right justify flag.
Definition basic_format_spec.h:534
ETL_CONSTEXPR friend bool operator!=(const basic_format_spec &lhs, const basic_format_spec &rhs)
Inequality operator.
Definition basic_format_spec.h:606
ETL_CONSTEXPR14 basic_format_spec & upper_case(bool u) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:435
ETL_CONSTEXPR bool is_upper_case() const ETL_NOEXCEPT
Gets the upper case flag.
Definition basic_format_spec.h:453
ETL_CONSTEXPR14 basic_format_spec & precision(uint32_t p) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:408
ETL_CONSTEXPR14 basic_format_spec & hex() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:329
ETL_CONSTEXPR bool is_scientific() const ETL_NOEXCEPT
Gets the scientific flag.
Definition basic_format_spec.h:588
ETL_CONSTEXPR friend bool operator==(const basic_format_spec &lhs, const basic_format_spec &rhs)
Equality operator.
Definition basic_format_spec.h:596
ETL_CONSTEXPR bool is_left() const ETL_NOEXCEPT
Gets the left justify flag.
Definition basic_format_spec.h:507
ETL_CONSTEXPR14 basic_format_spec & binary() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:278
ETL_CONSTEXPR basic_format_spec() ETL_NOEXCEPT
Default constructor.
Definition basic_format_spec.h:209
ETL_CONSTEXPR14 basic_format_spec & octal() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:295
ETL_CONSTEXPR14 void clear() ETL_NOEXCEPT
Clears the format spec back to default.
Definition basic_format_spec.h:242
ETL_CONSTEXPR14 basic_format_spec & left() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:489
ETL_CONSTEXPR14 basic_format_spec & boolalpha(bool b) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:543
ETL_CONSTEXPR TString::value_type get_fill() const ETL_NOEXCEPT
Gets the fill character.
Definition basic_format_spec.h:480
ETL_CONSTEXPR bool is_boolalpha() const ETL_NOEXCEPT
Gets the boolalpha flag.
Definition basic_format_spec.h:561
ETL_CONSTEXPR uint32_t get_width() const ETL_NOEXCEPT
Gets the width.
Definition basic_format_spec.h:399
ETL_CONSTEXPR14 basic_format_spec & scientific(bool b) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:570
ETL_CONSTEXPR14 basic_format_spec & decimal() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:312
ETL_CONSTEXPR14 basic_format_spec & show_base(bool b) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:354
ETL_CONSTEXPR uint32_t get_precision() const ETL_NOEXCEPT
Gets the precision.
Definition basic_format_spec.h:426
Definition absolute.h:40
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