I think it would be great to create a null serial class like bellow and to do that all serial drivers (hard and soft) inherit from it.
This would all to maid reference to Serial ports in a transparents way (usefull when you want to move your application from a serial port to an other, or to have a Debug serial you can hide).
This is a few code to write and it works perfectly.
class NSerial : public Print
{
public:
virtual void begin(long) {}
virtual uint8_t available(void) {}
virtual int read(void) {}
virtual void flush(void) {}
virtual void write(uint8_t) {}
using Print::write; // pull in write(str) and write(buf, size) from Print
};
I think all functions should be pure virtual and I would like a different name.
But the idea [which we have discussed before either here or in the google issuelist] is a good one.
Does the compiler warn about those (e.g. class cannot be instantiated because it's abstract)? Is there any run-time support for trapping a call to a pure virtual method?
The compiler SHOULD prevent instantiating a pure virtual class. I'm not certain that gcc does, but I'd be very surprised (and disappointed) if it didn't. The same holds true for calls to pure virtual methods of a non-abstract class.
Some of PC compilers (e.g. Visual C and Borland C) I've used put stubs in place of pure virtual functions. The stubs generate either a run time error or an exception (depending on the compiler and version). If an abstract method is invoked at run-time, instead of an access violation, the program outputs a (sometimes) meaningful error message. But none of this matters if the compiler won't allow abstract classes to be created.
A quick test of the Ardunio compiler reveals that it does not allow the creation of abstract classes. So, the answer to my questions...
Does the compiler warn about those (e.g. class cannot be instantiated because it's abstract)?
No. The compiler produces an error. Creating instances of an abstract class is not possible.
Is there any run-time support for trapping a call to a pure virtual method?