@@ -245,15 +245,15 @@ class DefaultFormatter : public Print::Formatter {
245
245
uint8_t value;
246
246
};
247
247
constexpr DefaultFormatter operator +(FormatOptionPrecision o) {
248
- return {this ->base , o. value , this ->precision };
248
+ return {this ->base , this ->min_width , o. value };
249
249
}
250
250
251
251
struct FormatOptionMinWidth : FormatterOption {
252
252
constexpr FormatOptionMinWidth (uint8_t value) : value(value) { }
253
253
uint8_t value;
254
254
};
255
255
constexpr DefaultFormatter operator +(FormatOptionMinWidth o) {
256
- return {this ->base , this -> min_width , o.value };
256
+ return {this ->base , o.value , this -> precision };
257
257
}
258
258
259
259
constexpr DefaultFormatter (uint8_t base = 10 , uint8_t min_width = 0 , uint8_t precision = 2 )
@@ -394,44 +394,26 @@ auto DefaultFormatterFor(TValue value, OptionList<THead, TTail> list)
394
394
/* End of OptionList stuff *
395
395
******************************************************************/
396
396
397
- #if 0
398
- inline DefaultFormatter DefaultFormatterFor(Any /* value */, FormatOptionMinWidth) {
399
- return {};
400
- }
397
+ } // namespace Formatters
401
398
402
- inline DefaultFormatter DefaultFormatterFor(Any /* value */, FormatOptionBase) {
403
- return {};
404
- }
405
- #endif
406
399
template <typename T>
407
- inline DefaultFormatter DefaultFormatterFor (T, DefaultFormatter::FormatterOption) {
400
+ inline Formatters:: DefaultFormatter DefaultFormatterFor (T, Formatters:: DefaultFormatter::FormatterOption) {
408
401
return {};
409
402
}
410
403
411
- /*
412
404
template <typename T>
413
- T declval();
414
-
415
- template<typename TValue,
416
- typename TOption,
417
- typename Test1 = decltype(declval<DefaultFormatter>().addOption(declval<TOption>())),
418
- typename Test2 = decltype(declval<DefaultFormatter>().printTo(declval<Print*>(), declval<TValue>()))
419
- >
420
- inline DefaultFormatter DefaultFormatterFor(TValue, TOption) {
405
+ inline Formatters::DefaultFormatter DefaultFormatterFor (T) {
421
406
return {};
422
407
}
423
- */
424
-
425
- } // namespace Formatters
426
408
427
409
#undef HEX
428
410
inline constexpr Formatters::DefaultFormatter::FormatOptionMinWidth FORMAT_MIN_WIDTH (uint8_t min_width) { return {min_width}; }
429
411
inline constexpr Formatters::DefaultFormatter::FormatOptionBase FORMAT_BASE (uint8_t base) { return {base}; }
430
412
inline constexpr Formatters::DefaultFormatter::FormatOptionPrecision FORMAT_PRECISION (uint8_t prec) { return {prec}; }
431
413
constexpr Formatters::DefaultFormatter::FormatOptionBase HEX = FORMAT_BASE(16 );
432
-
433
414
// constexpr auto FORMAT_PRECISION = Formatters::DefaultFormatter::FORMAT_PRECISION;
434
415
416
+ // Compatibility with previous versions, where base and precision were just numbers
435
417
inline size_t Print::print ( signed char n, int base) { return print (n, FORMAT_BASE (base)); }
436
418
inline size_t Print::print ( signed short n, int base) { return print (n, FORMAT_BASE (base)); }
437
419
inline size_t Print::print ( signed int n, int base) { return print (n, FORMAT_BASE (base)); }
@@ -453,46 +435,48 @@ inline size_t Print::print( double n, int prec) { return print(n, FORMAT_
453
435
// declaration...).
454
436
class PrintHelper {
455
437
public:
438
+ // Base case for the argument recursion - no more things to print
439
+ static _always_inline size_t printTo (Print *) { return 0 ; }
440
+
441
+ // Simplest case: Just a value, no formatters or options (used when
442
+ // none of the below overloads match): Look up the default formatter
443
+ // for this value, and add it to the argument list.
456
444
template <typename T, typename ...Ts>
457
445
static _always_inline size_t printTo (Print * p, const T &arg, const Ts &...args) {
458
446
// This might lead to infinite template instantion
459
447
// return print(arg, DefaultFormatter<>(), args...);
460
- size_t n = Formatters::DefaultFormatter ().printTo (p, arg);
461
- return n + printTo (p, args...);
462
- }
463
- /*
464
- template<typename T, typename T2, typename ...Ts>
465
- static _always_inline auto printTo(Print * p, const T &arg, const T2 &arg2, const Ts &...args)
466
- -> enable_if_valid_t<decltype(arg2.printTo(p, arg)), size_t> {
467
- size_t n = arg2.printTo(p, arg);
468
- return n + printTo(p, args...);
448
+ return printTo (p, arg, DefaultFormatterFor (arg), args...);
469
449
}
470
- */
471
450
472
- template <typename T, typename T2, typename ...Ts>
473
- static _always_inline auto printTo (Print * p, const T &arg, const T2 &formatter, const Ts &...args)
451
+ // A value and a formatter is specified without any options (or the
452
+ // below overloads have applied the options): Let the formatter
453
+ // print the value.
454
+ template <typename T, typename TFormatter, typename ...Ts>
455
+ static _always_inline auto printTo (Print * p, const T &arg, const TFormatter &formatter, const Ts &...args)
474
456
-> enable_if_valid_t<decltype(accepts_formatter(&formatter)), size_t> {
475
457
// -> enable_if_valid_t<decltype(formatter.printTo(p, arg)), size_t> {
476
458
size_t n = formatter.printTo (p, arg);
477
459
return n + printTo (p, args...);
478
460
}
479
461
480
462
463
+ // A value, a formatter and an option is specified: Apply the option
464
+ // to the formatter.
481
465
template <typename T, typename TFormatter, typename TOption, typename ...Ts>
482
466
static _always_inline auto printTo (Print * p, const T &arg, const TFormatter &formatter, const TOption &option, const Ts &...args)
483
- // -> enable_if_valid_t<decltype(formatter.addOption( option) ), size_t> {
467
+ // -> enable_if_valid_t<decltype(formatter + option), size_t> {
484
468
-> enable_if_valid_t<decltype(accepts_formatter(&formatter), accepts_option(&option)), size_t> {
485
- // return printTo(p, arg, formatter.addOption(option), args...);
486
469
return printTo (p, arg, formatter + option, args...);
487
470
}
488
471
489
472
490
- template <typename T, typename T2, typename ...Ts>
491
- static _always_inline auto printTo (Print * p, const T &arg, const T2 &option, const Ts &...args)
492
- // -> enable_if_valid_t<decltype(typename T2::Formatter().addOption(option)), size_t> {
473
+ // A value and an option is specified: Look up the default formatter
474
+ // for this value and option and add it to the argument list.
475
+ template <typename T, typename TOption, typename ...Ts>
476
+ static _always_inline auto printTo (Print * p, const T &arg, const TOption &option, const Ts &...args)
477
+ // -> enable_if_valid_t<decltype(DefaultFormatterFor(arg, option)), size_t> {
493
478
-> enable_if_valid_t<decltype(accepts_option(&option)), size_t> {
494
479
auto formatter = DefaultFormatterFor (arg, option);
495
- // return printTo(p, arg, formatter + option, args...);
496
480
return printTo (p, arg, formatter, option, args...);
497
481
}
498
482
@@ -507,6 +491,9 @@ class PrintHelper {
507
491
//
508
492
// If we keep these, OptionList::addToFormatter and the related
509
493
// operator+ overload can be removed.
494
+ //
495
+ // If we add one more overload for (Value, OptionList, ...), we can
496
+ // also remove the DefaultFormatterForm definition for OptionList.
510
497
template <typename T, typename TFormatter, typename THead, typename TTail, typename ...Ts>
511
498
static _always_inline auto printTo (Print * p, const T &arg, const TFormatter &formatter, const Formatters::OptionList<THead, TTail> &list, const Ts &...args)
512
499
// -> enable_if_valid_t<decltype(formatter.addOption(list.head)), size_t> {
@@ -522,8 +509,6 @@ class PrintHelper {
522
509
return printTo (p, arg, formatter, list.head , args...);
523
510
}
524
511
525
- // Base case for the argument recursion - no more things to print
526
- static _always_inline size_t printTo (Print *) { return 0 ; }
527
512
};
528
513
529
514
template <typename ...Ts>
0 commit comments