Pages: [1]   Go Down
Author Topic: Suggest : Parent serial class  (Read 719 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 1
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
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
};

Regards.
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I completely agree.

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.

Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 210
Posts: 13030
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I think all functions should be pure virtual
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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 50331
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is there any run-time support for trapping a call to a pure virtual method?

What do you mean?  smiley
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 210
Posts: 13030
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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...

Quote
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.

Quote
Is there any run-time support for trapping a call to a pure virtual method?
It does not matter.

Quote
I think all functions should be pure virtual
Given the stuff above, I agree.
Logged

Pages: [1]   Go Up
Jump to: