473,395 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Encapsulation & Macro Functions

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

Nov 13 '05 #1
4 4940
On Sun, 28 Sep 2003 14:34:19 GMT, Thomas Matthews
<Th****************************@sbcglobal.net> wrote in comp.lang.c:
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?


I don't much like this sort of thing, but I have done it when
necessary. Try this:

timer_isr.c:

static volatile unsigned int timer_ticks;
const volatile unsigned int * const timer_ptr = &timer_ticks;

Then put timer_ptr in a header with extern, and include it in
functions that need to access the current value.

unsigned int my_ticks = *timer_ptr;

Not the use of two const qualifiers, external code can only use the
pointer to read the current value, can't modify the pointer or the
pointer-to value.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #2
Thomas Matthews wrote:
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.


Did you write the library? If there is no way of accessing the variable
outside the source file, there is no way macros are going to help, they
aren't considered by the compiler, but by the preprocessor, so whatever the
resulting code is after translating them, it still has to be valid c.

On another node, but not on topic, your compiler might change those to
inline functions if you specify the proper arguments to it. Depending on
your compiler, you may also be able to use the inline keyword to force the
compiler to do so. Finally, you could make a function that returns a
pointer to this static data. But I think that is circumventing the idea of
having it static locally (which is the same reason why a macro wouldn't
work).

Good luck,

--
Martijn
http://www.sereneconcepts.nl
Nov 13 '05 #3
Martijn wrote:
On another node


LOL been working with linked lists too much - I meant "note" of course ;)

--
Martijn
http://www.sereneconcepts.nl
Nov 13 '05 #4
Martijn wrote:
Thomas Matthews wrote:
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.

Did you write the library? If there is no way of accessing the variable
outside the source file, there is no way macros are going to help, they
aren't considered by the compiler, but by the preprocessor, so whatever the
resulting code is after translating them, it still has to be valid c.


Unfortunately, I didn't write the exact code (which wasn't posted due to
Intellectual Property concerns). I just noticed on my compiler that it
was not inlining the function (at least in no-optimization mode, which
allows best debugging). I _could_ change the optimization settings, but
I'd rather do it using code.

This is code for an embedded system.

On another node, but not on topic, your compiler might change those to
inline functions if you specify the proper arguments to it. Depending on
your compiler, you may also be able to use the inline keyword to force the
compiler to do so. Finally, you could make a function that returns a
pointer to this static data. But I think that is circumventing the idea of
having it static locally (which is the same reason why a macro wouldn't
work).


On other nodes, I searched the FAQ, and the newsgroup and couldn't find
anything on this. Perhaps this might be a convincing argument to
support switching to C++ (No flames please).
--
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

Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

71
by: ROSY | last post by:
1. How would you use the functions memcpy(), memset(), memmove()?
5
by: Thomas Matthews | last post by:
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,...
13
by: uma676 | last post by:
Hi all, I want to know the differebces between function and macro in c language. if anybody can tell me atleast 4 diff's. for which i will be regrated. Rgds, Balaji
9
by: Let_Me_Be | last post by:
Hi all, I'm developing a small defensive programming toolkit for my own projects. So, here are my questions. 1) Is it possible to move from something like this: SAFECALL(foo();) to __safecall...
32
by: bluejack | last post by:
Ahoy: For as long as I've been using C, I've vacillated on the optimal degree of encapsulation in my designs. At a minimum, I aggregate data and code that operate on that data into classlike...
2
by: subramanian100in | last post by:
Is my following understanding correct ? Data abstraction means providing the interface - that is, the set of functions that can be called by the user of a class. Information hiding means...
6
by: jason | last post by:
Hi, I learned my lesson about passing pointers, but now I have a question about macros. Why does the function work and the MACRO which is doing the same thing on the surface, does not work in...
19
by: Taras_96 | last post by:
Hi all, A poster at http://bytes.com/forum/thread60652.html implies that using strtoupper in transform doesn't work because ctype.h may define strtoupper as a macro: "The problem is that most...
45
by: mdh | last post by:
Hi All, The section is titled Variable-length Argument lists. May I ask a question about the use of "macros". To quote K&R. "The standard header <stdarg.hcontains a set of macro definitions...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.