Skip to content

Commit 288d947

Browse files
WIP
1 parent fa23c18 commit 288d947

File tree

1 file changed

+29
-44
lines changed

1 file changed

+29
-44
lines changed

cores/arduino/Print.h

+29-44
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,15 @@ class DefaultFormatter : public Print::Formatter {
245245
uint8_t value;
246246
};
247247
constexpr DefaultFormatter operator+(FormatOptionPrecision o) {
248-
return {this->base, o.value, this->precision};
248+
return {this->base, this->min_width, o.value};
249249
}
250250

251251
struct FormatOptionMinWidth : FormatterOption {
252252
constexpr FormatOptionMinWidth(uint8_t value) : value(value) { }
253253
uint8_t value;
254254
};
255255
constexpr DefaultFormatter operator+(FormatOptionMinWidth o) {
256-
return {this->base, this->min_width, o.value};
256+
return {this->base, o.value, this->precision};
257257
}
258258

259259
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)
394394
/* End of OptionList stuff *
395395
******************************************************************/
396396

397-
#if 0
398-
inline DefaultFormatter DefaultFormatterFor(Any /* value */, FormatOptionMinWidth) {
399-
return {};
400-
}
397+
} // namespace Formatters
401398

402-
inline DefaultFormatter DefaultFormatterFor(Any /* value */, FormatOptionBase) {
403-
return {};
404-
}
405-
#endif
406399
template<typename T>
407-
inline DefaultFormatter DefaultFormatterFor(T, DefaultFormatter::FormatterOption) {
400+
inline Formatters::DefaultFormatter DefaultFormatterFor(T, Formatters::DefaultFormatter::FormatterOption) {
408401
return {};
409402
}
410403

411-
/*
412404
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) {
421406
return {};
422407
}
423-
*/
424-
425-
} // namespace Formatters
426408

427409
#undef HEX
428410
inline constexpr Formatters::DefaultFormatter::FormatOptionMinWidth FORMAT_MIN_WIDTH(uint8_t min_width) { return {min_width}; }
429411
inline constexpr Formatters::DefaultFormatter::FormatOptionBase FORMAT_BASE(uint8_t base) { return {base}; }
430412
inline constexpr Formatters::DefaultFormatter::FormatOptionPrecision FORMAT_PRECISION(uint8_t prec) { return {prec}; }
431413
constexpr Formatters::DefaultFormatter::FormatOptionBase HEX = FORMAT_BASE(16);
432-
433414
//constexpr auto FORMAT_PRECISION = Formatters::DefaultFormatter::FORMAT_PRECISION;
434415

416+
// Compatibility with previous versions, where base and precision were just numbers
435417
inline size_t Print::print( signed char n, int base) { return print(n, FORMAT_BASE(base)); }
436418
inline size_t Print::print( signed short n, int base) { return print(n, FORMAT_BASE(base)); }
437419
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_
453435
// declaration...).
454436
class PrintHelper {
455437
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.
456444
template<typename T, typename ...Ts>
457445
static _always_inline size_t printTo(Print * p, const T &arg, const Ts &...args) {
458446
// This might lead to infinite template instantion
459447
//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...);
469449
}
470-
*/
471450

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)
474456
-> enable_if_valid_t<decltype(accepts_formatter(&formatter)), size_t> {
475457
//-> enable_if_valid_t<decltype(formatter.printTo(p, arg)), size_t> {
476458
size_t n = formatter.printTo(p, arg);
477459
return n + printTo(p, args...);
478460
}
479461

480462

463+
// A value, a formatter and an option is specified: Apply the option
464+
// to the formatter.
481465
template<typename T, typename TFormatter, typename TOption, typename ...Ts>
482466
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> {
484468
-> enable_if_valid_t<decltype(accepts_formatter(&formatter), accepts_option(&option)), size_t> {
485-
//return printTo(p, arg, formatter.addOption(option), args...);
486469
return printTo(p, arg, formatter + option, args...);
487470
}
488471

489472

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> {
493478
-> enable_if_valid_t<decltype(accepts_option(&option)), size_t> {
494479
auto formatter = DefaultFormatterFor(arg, option);
495-
//return printTo(p, arg, formatter + option, args...);
496480
return printTo(p, arg, formatter, option, args...);
497481
}
498482

@@ -507,6 +491,9 @@ class PrintHelper {
507491
//
508492
// If we keep these, OptionList::addToFormatter and the related
509493
// 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.
510497
template<typename T, typename TFormatter, typename THead, typename TTail, typename ...Ts>
511498
static _always_inline auto printTo(Print * p, const T &arg, const TFormatter &formatter, const Formatters::OptionList<THead, TTail> &list, const Ts &...args)
512499
//-> enable_if_valid_t<decltype(formatter.addOption(list.head)), size_t> {
@@ -522,8 +509,6 @@ class PrintHelper {
522509
return printTo(p, arg, formatter, list.head, args...);
523510
}
524511

525-
// Base case for the argument recursion - no more things to print
526-
static _always_inline size_t printTo(Print *) { return 0; }
527512
};
528513

529514
template<typename ...Ts>

0 commit comments

Comments
 (0)