Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 67ad205

Browse files
WIP comments
1 parent 11a3dc9 commit 67ad205

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

cores/arduino/Print.h

+31-9
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,25 @@
3636

3737
#define _always_inline __attribute__ ((__always_inline__)) // undefined at end
3838

39+
40+
// Namespace to hide implementation details into. Its contents should
41+
// not be used outside of this file.
3942
namespace detail {
4043
// Returns a value of the given type. No implementation, so this is
4144
// meant for use in decltype only. Identical to std::declval.
4245
template <typename T> T declval();
4346

44-
// Function that accept a pointer of the given type. Can be used to
45-
// detect if another type is convertible to T.
47+
// Function that accepts a pointer of the given type. Can be used to
48+
// detect if another type is convertible to T. No implementation, so
49+
// intended to be used in declval only.
4650
template<typename T>
4751
void accepts_pointer_of_type(const T*);
4852

4953
// Simplified and integrated version of std::enable_if_t and
5054
// std::is_base_of (which are not available on AVR). Integrating them
5155
// makes things more specific and thus simpler.
56+
// This type alias resolves to `void` when `Base` is a base class of
57+
// `Derived`, or fails to resolve otherwise.
5258
// TODO: This check does not detect private base classes. To do so, a
5359
// more complicated check is needed, something like
5460
// https://stackoverflow.com/questions/2910979/how-does-is-base-of-work
@@ -64,6 +70,7 @@ namespace Formatters {
6470
class DefaultFormatter;
6571
}
6672

73+
// Forward declaration
6774
template<typename T>
6875
Formatters::DefaultFormatter DefaultFormatterFor(T);
6976

@@ -182,14 +189,15 @@ class Print
182189
// operator+ to apply each of its arguments to the formatter in
183190
// turn), but without these the error messages are less clear if
184191
// incompatible options are mixed (with the below overloads the
185-
// error shows the formatter and the incompatible option, while // without them only the formatter and the entire option list are
192+
// error shows the formatter and the incompatible option, while
193+
// without them only the formatter and the entire option list are
186194
// shown.
187195
//
188196
// If we keep these, OptionList::addToFormatter and the related
189197
// operator+ overload can be removed.
190198
//
191199
// If we add one more overload for (Value, OptionList, ...), we can
192-
// also remove the DefaultFormatterForm definition for OptionList.
200+
// also remove the DefaultFormatterFor definition for OptionList.
193201
template<
194202
typename T,
195203
typename TFormatter,
@@ -459,17 +467,31 @@ auto DefaultFormatterFor(TValue value, OptionList<THead, TTail> list)
459467

460468
} // namespace Formatters
461469

470+
// The DefaultFormatterFor function is used to define what formatter to
471+
// use for a value when no formatter is explicitly specified. This is
472+
// primarily intended to pick a formatter based on the value type (first
473+
// argument), but it is also possible to look at the value itself when
474+
// building the formatter. Note that it is not possible to return a
475+
// different *type* of formatter based on the value, it is possible to
476+
// set options in the formatter based on the value.
477+
// TODO: Document ADL stuff?
462478
template<typename T>
463-
inline Formatters::DefaultFormatter DefaultFormatterFor(T, Formatters::DefaultFormatter::FormatterOption) {
479+
inline Formatters::DefaultFormatter DefaultFormatterFor(T) {
464480
return {};
465481
}
466482

483+
// Overload that decides on a formatter to use based on the value to
484+
// print as well as the (first) formatter option passed. See
485+
// `DefaultFormatterFor(T)` overload for more details.
467486
template<typename T>
468-
inline Formatters::DefaultFormatter DefaultFormatterFor(T) {
487+
inline Formatters::DefaultFormatter DefaultFormatterFor(T, Formatters::DefaultFormatter::FormatterOption) {
469488
return {};
470489
}
471490

491+
// TODO: Fix this in Arduino.h
472492
#undef HEX
493+
494+
// TODO: Finalize options
473495
inline constexpr Formatters::DefaultFormatter::FormatOptionMinWidth FORMAT_MIN_WIDTH(uint8_t min_width) { return {min_width}; }
474496
inline constexpr Formatters::DefaultFormatter::FormatOptionBase FORMAT_BASE(uint8_t base) { return {base}; }
475497
inline constexpr Formatters::DefaultFormatter::FormatOptionPrecision FORMAT_PRECISION(uint8_t prec) { return {prec}; }
@@ -571,7 +593,8 @@ inline size_t Print::print( double n, int prec) { return print(n, FORMAT_
571593
// live in a namespace). In practice, this means that replacing the
572594
// default formatter for a native type (without any options) is not
573595
// possible, since the reference to e.g. DefaultFormatterFor(int) is
574-
// looked up at template definition time, not instantiation time.
596+
// looked up in the set of overloads that were defined at template
597+
// definition time, not at instantiation time.
575598
//
576599
// Two possible workarounds for this would be to add a wrapper class
577600
// around values before passing them to DefaultFormatterFor, or adding
@@ -596,6 +619,7 @@ inline size_t Print::print( double n, int prec) { return print(n, FORMAT_
596619
// SomeOption)", rather than "no such operator
597620
// operator+(DefaultFormatter, SomeOption)"), so we should probably do
598621
// that.
622+
// TODO: This was changed to applyOption, reword
599623
//
600624
// Formatters and options are currently passed around as const
601625
// references, meaning that their printTo methods and addition operators
@@ -623,8 +647,6 @@ inline size_t Print::print( double n, int prec) { return print(n, FORMAT_
623647
// TODO: Use NoOption dummy argument to DefaultFormatterFor to force
624648
// ADL?
625649
//
626-
// TODO: Convert operator+ to applyOption?
627-
//
628650
// TODO: Naming of option classes (shows up in error message)
629651
//
630652
// TODO: Getters and setters for DefaultFormatter options? Helps with

0 commit comments

Comments
 (0)