View previous topic :: View next topic |
Author |
Message |
mbradley
Joined: 11 Jul 2009 Posts: 118 Location: California, USA
|
Pseudo Classes - Include two more copies of the same code. |
Posted: Mon Nov 14, 2011 10:47 pm |
|
|
I have rewritten my Class code to allow a type of code reuse in the CCS compiler.
Please let me know if you find this usefull or have any comments.
I maintain the source here:
http://www.mculabs.com/snippets/pclass.html
Imagine you have a nice piece of code, and you need more than one instance of it in your source, what do you do? In CCS you would (unless I hear another way) copy the file over, give it a new name, and rename all the functions and variables.
I had a new circular buffer piece of code that I wrote, and I needed two instances of it. So I just use this:
Code: |
#include "pclass.h"
ClassDef gpsBuffer
ClassInclude "cls_circbuffer.c"
ClassEnd
ClassDef compassBuffer
ClassInclude "cls_circbuffer.c"
ClassEnd
|
And to use the two instances, I can do this:
Code: |
g = gpsBuffer_getChr();
c = compassBuffer_getChr();
|
_________________ Michael Bradley
www.mculabs.com
Open Drivers and Projects |
|
|
MikeW
Joined: 15 Sep 2003 Posts: 184 Location: Warrington UK
|
thank you |
Posted: Tue Nov 15, 2011 5:32 am |
|
|
Michael,
thank you, keep em coming !!! |
|
|
n-squared
Joined: 03 Oct 2006 Posts: 99
|
|
Posted: Sun Nov 20, 2011 4:06 pm |
|
|
Hi
Great stuff!
Looks like I am going to use this method in many of my projects.
Thanks.
BR
Noam _________________ Every solution has a problem. |
|
|
mbradley
Joined: 11 Jul 2009 Posts: 118 Location: California, USA
|
|
Posted: Sun Nov 20, 2011 4:15 pm |
|
|
Thank you,
Although it makes for a bit of funky looking source, if you got a solid routine or set of functions, this works great for creating two copies of it.
I use it most for circular buffers, and for FIFO buffers. I have also used my old one for serial routines that I can use with chips with more than one uart. _________________ Michael Bradley
www.mculabs.com
Open Drivers and Projects |
|
|
Eduardo__
Joined: 23 Nov 2011 Posts: 197 Location: Brazil
|
Cool |
Posted: Thu Nov 24, 2011 6:54 am |
|
|
Cool!
I´m going to try it later.
I´ve not found information in CCS Help about these preprocessors. I mean ## preprocessors use in:
#define __mDEFCLASS(cls,def) ##cls##_##def
Another question, CCS compiler isolates the variable names inside the class?
Is it possible to create workspaces inside programs, like C++?
It would be good for creating universal drivers and another resources.
Thanks for information. _________________ Eduardo Guilherme Brandt |
|
|
mbradley
Joined: 11 Jul 2009 Posts: 118 Location: California, USA
|
|
Posted: Thu Nov 24, 2011 12:30 pm |
|
|
CCS is not isolating variable names with my macros....
In summarize what's going on, if you use my macros, it will prepend all functions and variables names, in essence creating unique names for these.
Any var inside a function is like any other var inside a function, unique to that function.
However, if you have any global vars needed for the class, preface them with clsVar() to make them unique to the class.
The ## is for concatenation,
So , if we say:
clsFn(myClass,myFunc(aVar))
{
}
Turns into:
__mDEFCLASS(myClass,myFunc(aVar))
{
}
And this turns expands to:
myClass_myFunc(aVar)
{
} _________________ Michael Bradley
www.mculabs.com
Open Drivers and Projects |
|
|
Eduardo__
Joined: 23 Nov 2011 Posts: 197 Location: Brazil
|
Classes |
Posted: Fri Nov 25, 2011 5:55 am |
|
|
Anyway, that´s a good code.
Thank you a lot! _________________ Eduardo Guilherme Brandt |
|
|
|