Insuring a function is only called from a macro

H

holysmoke

Hi all,
I have a function

void actualfunction(unsigned int t, unsigned int lineno)
{
....
}

I want that it is called only from a macro I wrote for it (I don't
want to use a wrapper function) like,

#define FUNCTION(a) actualfunction((a), __LINE__)

How to do I insure that actualfunction is only called as FUNCTION?
One thing I could do is, call another function that sets a global
variable and then test that variable in 'actualfunction'.

#define FUNCTION(a) setTest(); \
actualfunction((a), __LINE__); \
void setTest(void)
{
static unsigned char test;
test = 0xFF;
}

void actualfunction(unsigned int t, unsigned int lineno)
{
assert(test == 0xFF);
test = 0x00;

....
}
BTW I'm ok to ignore the case where some calls setTest and then
actualfunction directly but this is a run time check, is a compile
time check posssible?




Would appreciate any pointers. Thanks!
 
R

Richard Tobin

holysmoke said:
I want that it is called only from a macro I wrote for it (I don't
want to use a wrapper function) like,

You can't. Anything the macro expands to, the user could just
write themselves.

The simplest solution is to give the function an obscure name that
makes it clear it shouldn't be used, like xxx_function_internal, or
function_calling_me_directly_invalidates_warranty. And document
the fact that it shouldn't be used.

-- Richard
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top