| Author |
Message |
|
|
Posted: Jul 16, 2012 - 01:57 AM |
|

Joined: Oct 10, 2011
Posts: 242
Location: Sydney, Australia
|
|
I have written a template class for queue handling in an atmega16. the application instantiates 7 different queues. data is stored in ring buffers.
the template parameter is gType.
the class member variables are...
private:
unsigned char inp; // place to write
unsigned char outp; // place to read
unsigned char size; // size of the buffer
qType * buf;
public:
unsigned char max; // max number of buffer elements used
to avoid code bloat I have written a helper function outside the class to increment inp and outp and reset to 0 when they wrap.
currently I pass two parameters to this helper function - inp/outp and size.
what I would like to do is to pass the this pointer and then access the class member data by de-referencing.
the problem is that each class is a different type even though all of the data is teh same structure... except for the type of the data pointed to by * buf.
is there an obvious way to solve this problem that I am missing.
the code savings will probably only be 50-100 bytes but at this stage I am trying to understand whether there is a general solution to the question that i can use in the future.
thanks
Greg |
|
|
| |
|
|
|
|
|
Posted: Jul 16, 2012 - 02:06 AM |
|

Joined: Mar 09, 2012
Posts: 1452
Location: North Carolina, USA
|
|
Post however much of your code is necessary to reproduce the problem.
And do use the Code button to format your code. |
_________________ Sid
Life... is a state of mind
|
| |
|
|
|
|
|
Posted: Jul 16, 2012 - 03:12 AM |
|

Joined: Oct 10, 2011
Posts: 242
Location: Sydney, Australia
|
|
| but the problem is that I don't know how to write the code!! My question is whether there is some way to write the code:-) |
_________________ regards
Greg
|
| |
|
|
|
|
|
Posted: Jul 16, 2012 - 03:38 AM |
|

Joined: Nov 17, 2004
Posts: 13849
Location: Vancouver, BC
|
|
|
Quote:
what I would like to do is to pass the this pointer and then access the class member data by de-referencing.
And how are you going to do that when the members are private? Surely the way to do this is to put the helper function in the class. |
_________________ Regards,
Steve A.
The Board helps those that help themselves.
|
| |
|
|
|
|
|
Posted: Jul 16, 2012 - 03:42 AM |
|

Joined: Mar 09, 2012
Posts: 1452
Location: North Carolina, USA
|
|
|
Koshchi wrote:
Quote:
what I would like to do is to pass the this pointer and then access the class member data by de-referencing.
And how are you going to do that when the members are private?
A friend function would do it.
Koshchi wrote:
Surely the way to do this is to put the helper function in the class.
Yes, that is a better idea. |
|
|
| |
|
|
|
|
|
Posted: Jul 16, 2012 - 03:43 AM |
|

Joined: Mar 09, 2012
Posts: 1452
Location: North Carolina, USA
|
|
|
gregd99 wrote:
but the problem is that I don't know how to write the code!! My question is whether there is some way to write the code:-)
That seems likely.
If you make an attempt to write your own code, it also seems likely that somebody would be willing to put in the time and effort to help you. |
|
|
| |
|
|
|
|
|
Posted: Jul 16, 2012 - 03:54 AM |
|

Joined: Oct 29, 2006
Posts: 2655
|
|
|
gregd99 wrote:
I have written a template class for queue handling in an atmega16. the application instantiates 7 different queues. data is stored in ring buffers.
the template parameter is gType.
the class member variables are...
private:
unsigned char inp; // place to write
unsigned char outp; // place to read
unsigned char size; // size of the buffer
qType * buf;
public:
unsigned char max; // max number of buffer elements used
to avoid code bloat I have written a helper function outside the class to increment inp and outp and reset to 0 when they wrap.
currently I pass two parameters to this helper function - inp/outp and size.
what I would like to do is to pass the this pointer and then access the class member data by de-referencing.
Derive your template classes from a base class that does not include buf.
Methods that do not use or affect buf can be in the base class. |
_________________ Michael Hennebry
Iluvatar is the better part of Valar.
|
| |
|
|
|
|
|
Posted: Jul 16, 2012 - 04:03 AM |
|

Joined: Oct 10, 2011
Posts: 242
Location: Sydney, Australia
|
|
|
Quote:
Derive your template classes from a base class that does not include buf.
Methods that do not use or affect buf can be in the base class.
I guess this is the right approach in lots of ways. The reason I didn't do this is because the as6 debugger gives no visibility into the members of the base class so you are reduced to examining iram.
this is the subject of a separate discussion with an improvement request raised by atmel. |
|
|
| |
|
|
|
|
|
Posted: Jul 16, 2012 - 04:22 AM |
|

Joined: Jan 02, 2010
Posts: 100
Location: Brovary, Ukraine
|
|
|
Koshchi wrote:
Surely the way to do this is to put the helper function in the class.
But
gregd99 wrote:
to avoid code bloat I have written a helper function outside the class
because functions in tempalte class are duplicated for class specialisations.
gregd99, helper class is needed.
Create non-template class queue_base with inp/outp/size fields (even without storage).
Make template queue class as public queue_base.
Code:
template<class qType, unsigned char qSize> class queue: public queue_base
{
public:
queue() : queue_base(qSize) {}
//... put/get functions use queue_base inp/outp fields
// and helper functions
private:
qType storage[qSize];
}
All tempalte specialisations will use the same increment-wrap functions from the same base class.
Although I am not shure that the way will give effect for small queue class, non-template base class with common template parameter independent functions preserves the common functions from duplicating. |
_________________ "...my spelling is Wobbly. It's good spelling but it Wobbles, and the letters get in the wrong places." (C) Winnie-the-Pooh
|
| |
|
|
|
|
|
Posted: Jul 16, 2012 - 05:37 AM |
|

Joined: Mar 09, 2012
Posts: 1452
Location: North Carolina, USA
|
|
|
ReAlAl wrote:
gregd99 wrote:
to avoid code bloat I have written a helper function outside the class
because functions in tempalte class are duplicated for class specialisations.
In theory, yes.
In practise, that depends on the code at hand, the compiler used and how suitable for inlining those functions are. |
|
|
| |
|
|
|
|
|
Posted: Jul 17, 2012 - 02:35 AM |
|

Joined: Sep 07, 2004
Posts: 2527
Location: New York State
|
|
I don't know what your situation is.
Are you familiar with inheritance and virtual functions? I use that a lot. By having a small "base" class that is inherited by various disparate classes can make all those classes appear as one class, namely the "base" class. |
|
|
| |
|
|
|
|
|
Posted: Jul 17, 2012 - 03:05 AM |
|

Joined: Mar 09, 2012
Posts: 1452
Location: North Carolina, USA
|
|
| Virtual functions have their place, but they come at a price. At least in an embedded setting, don't add them if you don't need them. |
_________________ Sid
Life... is a state of mind
|
| |
|
|
|
|
|
Posted: Jul 17, 2012 - 01:40 PM |
|

Joined: Sep 07, 2004
Posts: 2527
Location: New York State
|
|
All code comes at a price. Is the price of virtual functions greater than the price of alternatives?
Virtual functions make it easier and faster to produce cleaner and less buggy software. |
|
|
| |
|
|
|
|
|
Posted: Jul 17, 2012 - 05:05 PM |
|

Joined: Mar 09, 2012
Posts: 1452
Location: North Carolina, USA
|
|
|
steve17 wrote:
All code comes at a price. Is the price of virtual functions greater than the price of alternatives?
That depends on what you use them for. If you use them when they are not called for, then yes - the price is greater than that of the alternatives.
steve17 wrote:
Virtual functions make it easier and faster to produce cleaner and less buggy software.
Only when they are used correctly. |
|
|
| |
|
|
|
|
|
Posted: Jul 17, 2012 - 08:12 PM |
|

Joined: Oct 29, 2006
Posts: 2655
|
|
|
gregd99 wrote:
Quote:
Derive your template classes from a base class that does not include buf.
Methods that do not use or affect buf can be in the base class.
I guess this is the right approach in lots of ways. The reason I didn't do this is because the as6 debugger gives no visibility into the members of the base class so you are reduced to examining iram.
Somewhat klunkier,
one could make the base into a member and do explicit forwarding.
Would that help visibility?
Using inheritance can ensure that code is not duplicated on a per class basis,
but it is probably not necessary.
The functions will probably be inlined anyway.
I don't see how virtual functions could be useful here. |
_________________ Michael Hennebry
Iluvatar is the better part of Valar.
|
| |
|
|
|
|
|