BasicMicro - Forums

www.basicmicro.com
It is currently Sat Feb 04, 2012 7:01 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: C++ with virtual functions on a Bap...
PostPosted: Thu Aug 19, 2010 6:01 pm 
Offline
Master

Joined: Tue Nov 21, 2006 9:34 am
Posts: 500
I have been playing around with my versions of the C/C++ libraries that I am developing that run on the Bap28 and I thought I would make some of my functions more Arduino like.

So I thought I would try creating a couple of serial output classes that are wrappers of my current stuff (Hardware serial, Timer based bit bang).

But when I define the classes, that look something like:
Code:
class Print
{
  public:
    virtual void write(byte) = 0;
    virtual void write(const char *str);
    virtual void write(const byte *buffer, byte size);

    void print(const char[]);
   // Are more members but reduced for test case...
};

class HardwareSerial : public Print
{
   private:
      byte _bWhich;      //
   public:
      HardwareSerial(byte bWhich=0);   // Which port 0=HSERIAL 2=HSERIAL2
      // removed stuff for simplicity
      virtual void write(byte);
      using Print::write; // pull in write(str) and write(buf, size) from Print
};

class SoftwareSerial : public Print
{
   private:
      byte _bPinOut;   // May define with one or two pins...
      byte _bPinIn;   //
      word _wMode;   // what mode are we outputting...
   public:
      SoftwareSerial(byte bPinIn, byte bPinOut);   // Which IO Pins
      //...
      virtual void write(byte);
      using Print::write; // pull in write(str) and write(buf, size) from Print
};


I can get it to compile and the like, but my version of the C Phoenix code no longer fits on a Bap28. I found with these virtual functions, the compiler and linker brought in the kitchen sink. A lot of exception processing for Try/Catch... In a previous life I semi remember that there are a few functions you can define your own light weight functions to avoid this.

In this case I could probably get around this by not having virtual functions. Although it might be nice to be able to pass a pointer to a print object to parts of the program and have it abstract away if it is a hardware or serial port...

Kurt

Suggestions?

Kurt


Top
 Profile  
 
 Post subject: Re: C++ with virtual functions on a Bap...
PostPosted: Thu Aug 19, 2010 8:42 pm 
Offline
Master

Joined: Tue Jun 22, 2010 1:15 pm
Posts: 198
Create your own structs in C, with function pointers and variables?

Alan KM6VV

_________________
Visit:
http://groups.yahoo.com/group/SherlineCNC/
http://tech.groups.yahoo.com/group/HexapodRobotIK/


Top
 Profile  
 
 Post subject: Re: C++ with virtual functions on a Bap...
PostPosted: Fri Aug 20, 2010 7:34 am 
Offline
Master

Joined: Tue Nov 21, 2006 9:34 am
Posts: 500
Thanks Alan,

That is the way I would have done it in the past when I was a professional... :)

One thing I found so far is that the default pure-virtual function, whose whole purpose is to handle the case where you call through a class to a base class function that is defined virtual with no default handler (ie handler set to 0).

So in a test program, that simply defined those classes I mentioned plus defined a static instance of each one. The code size was: 18, 830.

I then defined a nothing handler:
Code:
extern "C" void __cxa_pure_virtual(void)
{
}

Now the code size is: 9,798

Will now go back to my library and see if things compile again...

Kurt


Top
 Profile  
 
 Post subject: Re: C++ with virtual functions on a Bap...
PostPosted: Fri Aug 20, 2010 8:09 am 
Offline
Master

Joined: Tue Nov 21, 2006 9:34 am
Posts: 500
The code is still too big. I will probably hack it up some more... I think I will get rid of the virtual functions. I will change the base class in this case Print, to have an init function that I will pass in for example a pointer to a function to do Write 1 byte... Maybe a second pointer to Write Multiple bytes... The low level function will then simply call this. (IE, more or less what you mentioned Alan)

I know that this will be a lot smaller. I got rid of the virtual in my test program, put in a dummy write function (did not do anything yet), size now 950

Kurt


Top
 Profile  
 
 Post subject: Re: C++ with virtual functions on a Bap...
PostPosted: Fri Aug 20, 2010 1:45 pm 
Offline
Master

Joined: Tue Jun 22, 2010 1:15 pm
Posts: 198
I seem to remember the same C++ "code bloat" problem on a commercial project. I wanted to create a virtual function, and overload (?) it later, the Borland C++ compiler made the binary file considerably bigger, for no gain in functionality. Templates, while they sound great, will likely also give you considerable code bloat.

Alan KM6VV

_________________
Visit:
http://groups.yahoo.com/group/SherlineCNC/
http://tech.groups.yahoo.com/group/HexapodRobotIK/


Top
 Profile  
 
 Post subject: Re: C++ with virtual functions on a Bap...
PostPosted: Sat Aug 21, 2010 7:35 pm 
Offline
Master

Joined: Tue Nov 21, 2006 9:34 am
Posts: 500
Yep, I have seen code bloat in the past but often times there are ways to minimize this. That is for example if your classes only derive from one base class, than you should be able to minimize the overhead. That is there only needs to be one vtable and you should be able to minimize/short circuit the casting code... There may be compiler options/Pragmas to tell the system to not generate exceptions if a cast fails and not bring all this code in as well as Try/Catch, memory management...

But if I instead had both a Print base class and maybe a Read base class and the HardwareSerial class was derived from both, than casting code is more complex with multiple vtables... Been awhile since I dealt with this.

I am able to work around it. I simply call a function to register my low level write a byte function, and then I can still use the code for the underlying Print class...

Kurt


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: Bing [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

phpBB SEO