Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Making Print::write(char *) non-virtual.

  • Loading branch information...
commit 7d8d20894c7420ad2fea81d80b66075806476179 1 parent 3cbbb17
@damellis damellis authored
View
1  hardware/arduino/cores/arduino/Client.h
@@ -10,7 +10,6 @@ class Client : public Stream {
virtual int connect(IPAddress ip, uint16_t port) =0;
virtual int connect(const char *host, uint16_t port) =0;
virtual size_t write(uint8_t) =0;
- virtual size_t write(const char *str) =0;
virtual size_t write(const uint8_t *buf, size_t size) =0;
virtual int available() = 0;
virtual int read() = 0;
View
10 hardware/arduino/cores/arduino/Print.cpp
@@ -30,16 +30,6 @@
// Public Methods //////////////////////////////////////////////////////////////
/* default implementation: may be overridden */
-size_t Print::write(const char *str)
-{
- size_t n = 0;
- while (*str) {
- n += write(*str++);
- }
- return n;
-}
-
-/* default implementation: may be overridden */
size_t Print::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
View
2  hardware/arduino/cores/arduino/Print.h
@@ -46,7 +46,7 @@ class Print
void clearWriteError() { setWriteError(0); }
virtual size_t write(uint8_t) = 0;
- virtual size_t write(const char *str);
+ size_t write(const char *str) { return write((const uint8_t *)str, strlen(str)); }
virtual size_t write(const uint8_t *buffer, size_t size);
size_t print(const __FlashStringHelper *);
View
2  hardware/arduino/cores/arduino/Server.h
@@ -1,7 +1,7 @@
#ifndef server_h
#define server_h
-class Server {
+class Server : public Print {
public:
virtual void begin() =0;
};
View
2  hardware/arduino/cores/arduino/Udp.h
@@ -57,8 +57,6 @@ class UDP : public Stream {
virtual int endPacket() =0;
// Write a single byte into the packet
virtual size_t write(uint8_t) =0;
- // Write a string of characters into the packet
- virtual size_t write(const char *str) =0;
// Write size bytes from buffer into the packet
virtual size_t write(const uint8_t *buffer, size_t size) =0;
View
4 libraries/Ethernet/EthernetClient.cpp
@@ -74,10 +74,6 @@ size_t EthernetClient::write(uint8_t b) {
return write(&b, 1);
}
-size_t EthernetClient::write(const char *str) {
- return write((const uint8_t *) str, strlen(str));
-}
-
size_t EthernetClient::write(const uint8_t *buf, size_t size) {
if (_sock == MAX_SOCK_NUM) {
setWriteError();
View
3  libraries/Ethernet/EthernetClient.h
@@ -15,7 +15,6 @@ class EthernetClient : public Client {
virtual int connect(IPAddress ip, uint16_t port);
virtual int connect(const char *host, uint16_t port);
virtual size_t write(uint8_t);
- virtual size_t write(const char *str);
virtual size_t write(const uint8_t *buf, size_t size);
virtual int available();
virtual int read();
@@ -27,6 +26,8 @@ class EthernetClient : public Client {
virtual operator bool();
friend class EthernetServer;
+
+ using Print::write;
private:
static uint16_t _srcport;
View
5 libraries/Ethernet/EthernetServer.cpp
@@ -72,11 +72,6 @@ size_t EthernetServer::write(uint8_t b)
write(&b, 1);
}
-size_t EthernetServer::write(const char *str)
-{
- write((const uint8_t *)str, strlen(str));
-}
-
size_t EthernetServer::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
View
2  libraries/Ethernet/EthernetServer.h
@@ -15,8 +15,8 @@ public Server {
EthernetClient available();
virtual void begin();
virtual size_t write(uint8_t);
- virtual size_t write(const char *str);
virtual size_t write(const uint8_t *buf, size_t size);
+ using Print::write;
};
#endif
View
6 libraries/Ethernet/EthernetUdp.cpp
@@ -107,12 +107,6 @@ size_t EthernetUDP::write(uint8_t byte)
return write(&byte, 1);
}
-size_t EthernetUDP::write(const char *str)
-{
- size_t len = strlen(str);
- return write((const uint8_t *)str, len);
-}
-
size_t EthernetUDP::write(const uint8_t *buffer, size_t size)
{
uint16_t bytes_written = bufferData(_sock, _offset, buffer, size);
View
4 libraries/Ethernet/EthernetUdp.h
@@ -67,10 +67,10 @@ class EthernetUDP : public UDP {
virtual int endPacket();
// Write a single byte into the packet
virtual size_t write(uint8_t);
- // Write a string of characters into the packet
- virtual size_t write(const char *str);
// Write size bytes from buffer into the packet
virtual size_t write(const uint8_t *buffer, size_t size);
+
+ using Print::write;
// Start processing the next available incoming packet
// Returns the size of the packet in bytes, or 0 if no packets are available
View
2  libraries/LiquidCrystal/LiquidCrystal.h
@@ -81,6 +81,8 @@ class LiquidCrystal : public Print {
void setCursor(uint8_t, uint8_t);
virtual size_t write(uint8_t);
void command(uint8_t);
+
+ using Print::write;
private:
void send(uint8_t, uint8_t);
void write4bits(uint8_t);
View
4 libraries/SD/File.cpp
@@ -62,10 +62,6 @@ size_t File::write(uint8_t val) {
return write(&val, 1);
}
-size_t File::write(const char *str) {
- return write((const uint8_t *) str, strlen(str));
-}
-
size_t File::write(const uint8_t *buf, size_t size) {
size_t t;
if (!_file) {
View
3  libraries/SD/SD.h
@@ -33,7 +33,6 @@ class File : public Stream {
File(void); // 'empty' constructor
~File(void); // destructor
virtual size_t write(uint8_t);
- virtual size_t write(const char *str);
virtual size_t write(const uint8_t *buf, size_t size);
virtual int read();
virtual int peek();
@@ -50,6 +49,8 @@ class File : public Stream {
boolean isDirectory(void);
File openNextFile(uint8_t mode = O_RDONLY);
void rewindDirectory(void);
+
+ using Print::write;
};
class SDClass {
View
2  libraries/SoftwareSerial/SoftwareSerial.h
@@ -93,6 +93,8 @@ class SoftwareSerial : public Stream
virtual int read();
virtual int available();
virtual void flush();
+
+ using Print::write;
// public only for easy access by interrupt handlers
static inline void handle_interrupt();
View
8 libraries/Wire/Wire.cpp
@@ -165,14 +165,6 @@ size_t TwoWire::write(const uint8_t *data, size_t quantity)
}
// must be called in:
-// slave tx event callback
-// or after beginTransmission(address)
-size_t TwoWire::write(const char *data)
-{
- return write((uint8_t*)data, strlen(data));
-}
-
-// must be called in:
// slave rx event callback
// or after requestFrom(address, numBytes)
int TwoWire::available(void)
View
3  libraries/Wire/Wire.h
@@ -53,7 +53,6 @@ class TwoWire : public Stream
uint8_t requestFrom(uint8_t, uint8_t);
uint8_t requestFrom(int, int);
virtual size_t write(uint8_t);
- virtual size_t write(const char *);
virtual size_t write(const uint8_t *, size_t);
virtual int available(void);
virtual int read(void);
@@ -61,6 +60,8 @@ class TwoWire : public Stream
virtual void flush(void);
void onReceive( void (*)(int) );
void onRequest( void (*)(void) );
+
+ using Print::write;
};
extern TwoWire Wire;

4 comments on commit 7d8d208

@JmkSunnyAZ

Keep getting error C2011 Print type class redefinition. The LCD function uses its own version of print routine but also uses Print.h file. It has something to do with namespaces and being able to use the keyword print in more than one class. Does anyone know why the printable class was implemented in the Print.h definitions?

@matthijskooijman
Collaborator

Sorry, your comment doesn't quite make sense to me.

What LCD function are you talking about (link to code would help)? The official LiquidCrystal library? That one just defines a class that derives from the Print class (iow, implements the Print interface).

Does anyone know why the printable class was implemented in the Print.h definitions?

Print.h declares the Print class, so other classes can derive from it. The actual implementation for the Print class is in Print.cpp, though.

@JmkSunnyAZ

Thank you matthisjskooijman. I understand the need for the printable class now. Still getting errors. I am using the example from http://ms-iot.github.io/content/16x2LCD.htm to include the LiquidCrystal library. The errors have something to do with redefinining the Print class during implementation. Printable.h creates a Print Class that encapsulates Print.h functions. In the Print.h class this virtual function is overwritten from Stream.h to print
// needs to be overwritten in order to provide Print functionality
virtual size_t write(uint8_t) = 0;
. This is causing the compiler to redefine the print class in Stream.h giving an error of
error C2504: 'Print' : base class undefined

From Stream.h
class Stream : public Print
{
public:
virtual size_t write(const uint8_t *buffer, size_t size) = 0;
size_t write(uint8_t b)
{
return write(&b, 1);
}

virtual int available() = 0;
virtual int read() = 0;

};
How can I properly use the print class within the LiquidCrystal library in Visual Studio to not redefine the class itself and to allow other objects to inherit the print functionality? The library itself works in the Arduino IDE because it runs Java and the different compiler allows for easier coding. Is there a proper way to do this in Visual Studio?

@matthijskooijman
Collaborator

error C2504: 'Print' : base class undefined

This suggests that the Print class is not defined yet, as opposed to being defined twice like you say. This is weird, since Stream.h includes Print.h, so it should have the Print class available there.

However, you say that it doesn't work with Visual Studio, but works with the Arduino IDE. This probably means that there is something, like the include path, wrongly setup in your VS environment, but I can't really help you with that. This also means that this github repository isn't really the right place to report this problem.

Your best bet is to contact whoever maintains the Arduino-VS integration tool you are you using, or ask on the Arduino forums where others with experience with VS hang around.

Please sign in to comment.
Something went wrong with that request. Please try again.