Encapsulation & Macro Functions

T

Thomas Matthews

Hi,

I have several translation units (modules) which contain static {local}
variables. These function have short global accessor functions.
I would like to change these functions into macros, but still preserve
the data hiding issue. How do I construct a macro to access static
{local} variables in a module? The macro will be in a header file
that is included in several modules.

Example: timer_isr.c
static unsigned int timer_ticks = 0;

void Timer_Isr(void)
{
++timer_ticks;
}

unsigned int Get_Timer_Ticks(void)
{
return timer_ticks;
}

void Reset_Timer_Ticks(void)
{
timer_ticks = 0;
}

In the above case, I would like to convert the Get_Timer_Ticks
function into a macro. The overhead of calling this function
(on my platform) outweighs the cost of a single-line of execution.

My attempt is this:
#define Macro_Get_Timer_Ticks() \
{ \
/* [1] */ extern unsigned int timer_ticks; \
/* [2] */ timer_ticks; \
}

There is an error at [1] since the "timer_ticks" variable is
defined as static, it can't be seen from external modules,
and thus can't be extern'd.

I believe there may be a problem with [2] since I'm not sure
that the evaluation of this statment block will be the
result of line[2].

Any ideas on how to convert the function to a macro while
preserving data hiding?

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
C

CBFalconer

Thomas said:
I have several translation units (modules) which contain static
{local} variables. These function have short global accessor
functions. I would like to change these functions into macros,
but still preserve the data hiding issue. How do I construct a
macro to access static {local} variables in a module? The macro
will be in a header file that is included in several modules.

The short answer is that you don't. The purpose of 'static' is to
hide the variables (assumed of file scope) from the outside world.

What you CAN do is create an initialization function that
allocates a struct containing the appropriate information, and
return a pointer to it. Then the external stuff can retain that
pointer and pass it back to the routines of interest, which know
what the struct looks like. For an example, see hashlib at:

<http://cbfalconer.home.att.net/download/>

Please do something about your multiple postings. One copy
suffices.
 
R

rihad

Hi,

I have several translation units (modules) which contain static {local}
variables. These function have short global accessor functions.
I would like to change these functions into macros, but still preserve
the data hiding issue. How do I construct a macro to access static
{local} variables in a module? The macro will be in a header file
that is included in several modules.

Example: timer_isr.c
static unsigned int timer_ticks = 0;

void Timer_Isr(void)
{
++timer_ticks;
}

unsigned int Get_Timer_Ticks(void)
{
return timer_ticks;
}

void Reset_Timer_Ticks(void)
{
timer_ticks = 0;
}
How about "inline"? Then the function bodies will go into headers, of course.
Or if inlining is not available (i.e. you're not using GCC 3), maybe like this:

unsigned int Get_Timer_Ticks(struct timer_ticks *timer_ticks)
{
return timer_ticks->timer_ticks;
}

void Reset_Timer_Ticks(struct timer_ticks *timer_ticks)
{
timer_ticks->timer_ticks = 0;
}

/* cast to make it rvalue */
#define Get_Timer_Ticks(s) ((timer_ticks_t) ((s)->timer_ticks))
#define Reset_Timer_Ticks(s) ((void) ((s)->timer_ticks = 0))

Ugly? Sure. That's why you should use inlining :)
 
M

Mark Gordon

Hi,

I have several translation units (modules) which contain static
{local} variables. These function have short global accessor
functions. I would like to change these functions into macros, but
still preserve the data hiding issue. How do I construct a macro to
access static{local} variables in a module? The macro will be in a
header file that is included in several modules.

<snip>

You can't do this in C. If a variable is declared as static then it has
no external linkage so no other module can access it. No preprocessor
trickery can get around this.
 
T

Thomas Matthews

rihad said:
On Sun, 28 Sep 2003 14:35:41 GMT, Thomas Matthews


Ugly? Sure. That's why you should use inlining :)

Unfortunately, I work with a compiler that doesn't support
inlining in the GCC sense. I also have to code to a
style convention. I want something portable that will
compile on: MS Visual Studio, Borland Builder, GCC and
ARM.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,082
Messages
2,570,589
Members
47,211
Latest member
Shamestone

Latest Threads

Top