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