Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Insuring a function is only called from a macro

Reply
Thread Tools

Insuring a function is only called from a macro

 
 
holysmoke
Guest
Posts: n/a
 
      08-19-2008
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!
 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      08-19-2008
In article <07da609e-11bf-4901-8741->,
holysmoke <> wrote:

>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
--
Please remember to mention me / in tapes you leave behind.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why would a function be executed twice if only called onload()? DrKen Javascript 8 06-24-2011 10:27 PM
allowing a function to be called only from a specific function junky_fellow@yahoo.co.in C Programming 24 12-01-2006 12:43 PM
object-like macro used like function-like macro Patrick Kowalzick C++ 5 03-14-2006 03:30 PM
three times copy ctor called, one ctor called, why? Apricot C++ 4 04-16-2004 07:55 AM
insuring your camera Bhaskar Karambelkar Digital Photography 7 08-17-2003 01:51 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57