Use Print& instead of Print*

This commit is contained in:
Benoit Blanchon
2014-11-06 14:29:29 +01:00
parent 6e67bc442f
commit e7864c9566
6 changed files with 21 additions and 21 deletions

View File

@ -21,7 +21,7 @@ template <typename T>
class JsonPrintable { class JsonPrintable {
public: public:
size_t printTo(Print &print) const { size_t printTo(Print &print) const {
JsonWriter writer(&print); JsonWriter writer(print);
downcast().writeTo(writer); downcast().writeTo(writer);
return writer.bytesWritten(); return writer.bytesWritten();
} }
@ -32,7 +32,7 @@ class JsonPrintable {
} }
size_t prettyPrintTo(IndentedPrint &print) const { size_t prettyPrintTo(IndentedPrint &print) const {
PrettyJsonWriter writer(&print); PrettyJsonWriter writer(print);
downcast().writeTo(writer); downcast().writeTo(writer);
return writer.bytesWritten(); return writer.bytesWritten();
} }

View File

@ -20,7 +20,7 @@ namespace Internals {
// indentation. // indentation.
class JsonWriter { class JsonWriter {
public: public:
explicit JsonWriter(Print *sink) : _sink(sink), _length(0) {} explicit JsonWriter(Print &sink) : _sink(sink), _length(0) {}
// Returns the number of bytes sent to the Print implementation. // Returns the number of bytes sent to the Print implementation.
// This is very handy for implementations of printTo() that must return the // This is very handy for implementations of printTo() that must return the
@ -44,10 +44,10 @@ class JsonWriter {
void writeComma() { write(','); } void writeComma() { write(','); }
protected: protected:
void write(char c) { _length += _sink->write(c); } void write(char c) { _length += _sink.write(c); }
void write(const char *s) { _length += _sink->print(s); } void write(const char *s) { _length += _sink.print(s); }
Print *_sink; Print &_sink;
size_t _length; size_t _length;
}; };
} }

View File

@ -15,7 +15,7 @@ namespace Internals {
// An indented version of JsonWriter. // An indented version of JsonWriter.
class PrettyJsonWriter : public JsonWriter { class PrettyJsonWriter : public JsonWriter {
public: public:
explicit PrettyJsonWriter(IndentedPrint *sink) explicit PrettyJsonWriter(IndentedPrint &sink)
: JsonWriter(sink), _indenter(sink) {} : JsonWriter(sink), _indenter(sink) {}
void beginArray() { void beginArray() {
@ -47,18 +47,18 @@ class PrettyJsonWriter : public JsonWriter {
private: private:
void indent() { void indent() {
_indenter->indent(); _indenter.indent();
newline(); newline();
} }
void unindent() { void unindent() {
newline(); newline();
_indenter->unindent(); _indenter.unindent();
} }
void newline() { _length += _indenter->println(); } void newline() { _length += _indenter.println(); }
IndentedPrint *_indenter; IndentedPrint &_indenter;
}; };
} }
} }

View File

@ -17,7 +17,7 @@ class QuotedString {
// Writes a doubly-quote string to a Print implementation. // Writes a doubly-quote string to a Print implementation.
// It adds the double quotes (") at the beginning and the end of the string. // It adds the double quotes (") at the beginning and the end of the string.
// It escapes the special characters as required by the JSON specifications. // It escapes the special characters as required by the JSON specifications.
static size_t printTo(const char *, Print *); static size_t printTo(const char *, Print &);
// Reads a doubly-quoted string from a buffer. // Reads a doubly-quoted string from a buffer.
// It removes the double quotes ("). // It removes the double quotes (").

View File

@ -14,12 +14,12 @@ void JsonWriter::writeString(char const *value) {
_length += QuotedString::printTo(value, _sink); _length += QuotedString::printTo(value, _sink);
} }
void JsonWriter::writeInteger(long value) { _length += _sink->print(value); } void JsonWriter::writeInteger(long value) { _length += _sink.print(value); }
void JsonWriter::writeBoolean(bool value) { void JsonWriter::writeBoolean(bool value) {
_length += _sink->print(value ? "true" : "false"); _length += _sink.print(value ? "true" : "false");
} }
void JsonWriter::writeDouble(double value, uint8_t decimals) { void JsonWriter::writeDouble(double value, uint8_t decimals) {
_length += _sink->print(value, decimals); _length += _sink.print(value, decimals);
} }

View File

@ -20,22 +20,22 @@ static inline char getSpecialChar(char c) {
return p[1]; return p[1];
} }
static inline size_t printCharTo(char c, Print *p) { static inline size_t printCharTo(char c, Print &p) {
char specialChar = getSpecialChar(c); char specialChar = getSpecialChar(c);
return specialChar ? p->write('\\') + p->write(specialChar) : p->write(c); return specialChar ? p.write('\\') + p.write(specialChar) : p.write(c);
} }
size_t QuotedString::printTo(const char *s, Print *p) { size_t QuotedString::printTo(const char *s, Print &p) {
if (!s) return p->print("null"); if (!s) return p.print("null");
size_t n = p->write('\"'); size_t n = p.write('\"');
while (*s) { while (*s) {
n += printCharTo(*s++, p); n += printCharTo(*s++, p);
} }
return n + p->write('\"'); return n + p.write('\"');
} }
static char unescapeChar(char c) { static char unescapeChar(char c) {