Skip to content

Commit 87a04fc

Browse files
committed
Added Printable interface class to allow printing of classes such as IPAddress
1 parent 2711c99 commit 87a04fc

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed

hardware/arduino/cores/arduino/Print.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ void Print::println(const __FlashStringHelper *ifsh)
117117
println();
118118
}
119119

120+
void Print::print(const Printable& x)
121+
{
122+
x.printTo(*this);
123+
}
124+
120125
void Print::println(void)
121126
{
122127
print('\r');
@@ -177,6 +182,12 @@ void Print::println(double n, int digits)
177182
println();
178183
}
179184

185+
void Print::println(const Printable& x)
186+
{
187+
print(x);
188+
println();
189+
}
190+
180191
// Private Methods /////////////////////////////////////////////////////////////
181192

182193
void Print::printNumber(unsigned long n, uint8_t base) {

hardware/arduino/cores/arduino/Print.h

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <stdio.h> // for size_t
2525

2626
#include "WString.h"
27+
#include "Printable.h"
2728

2829
#define DEC 10
2930
#define HEX 16
@@ -50,6 +51,7 @@ class Print
5051
void print(long, int = DEC);
5152
void print(unsigned long, int = DEC);
5253
void print(double, int = 2);
54+
void print(const Printable&);
5355

5456
void println(const __FlashStringHelper *);
5557
void println(const String &s);
@@ -61,6 +63,7 @@ class Print
6163
void println(long, int = DEC);
6264
void println(unsigned long, int = DEC);
6365
void println(double, int = 2);
66+
void println(const Printable&);
6467
void println(void);
6568
};
6669

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Printable.h - Interface class that allows printing of complex types
3+
Copyright (c) 2011 Adrian McEwen. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef Printable_h
21+
#define Printable_h
22+
23+
class Print;
24+
25+
/** The Printable class provides a way for new classes to allow themselves to be printed.
26+
By deriving from Printable and implementing the printTo method, it will then be possible
27+
for users to print out instances of this class by passing them into the usual
28+
Print::print and Print::println methods.
29+
*/
30+
class Printable
31+
{
32+
public:
33+
virtual void printTo(Print& p) const = 0;
34+
};
35+
36+
#endif
37+

libraries/Ethernet/IPAddress.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,13 @@ bool IPAddress::operator==(const uint8_t* addr)
4242
return memcmp(addr, _address, sizeof(_address)) == 0;
4343
}
4444

45+
void IPAddress::printTo(Print& p) const
46+
{
47+
for (int i =0; i < 3; i++)
48+
{
49+
p.print(_address[i], DEC);
50+
p.print('.');
51+
}
52+
p.print(_address[3], DEC);
53+
}
54+

libraries/Ethernet/IPAddress.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
#ifndef IPAddress_h
2727
#define IPAddress_h
2828

29+
#include <Printable.h>
30+
2931
// A class to make it easier to handle and pass around IP addresses
3032

31-
class IPAddress {
33+
class IPAddress : public Printable {
3234
private:
3335
uint8_t _address[4]; // IPv4 address
3436
// Access the raw byte array containing the address. Because this returns a pointer
@@ -58,6 +60,8 @@ class IPAddress {
5860
IPAddress& operator=(const uint8_t *address);
5961
IPAddress& operator=(uint32_t address);
6062

63+
virtual void printTo(Print& p) const;
64+
6165
friend class EthernetClass;
6266
friend class UDP;
6367
friend class Client;

0 commit comments

Comments
 (0)