Insuring a function is only called from a macro

Discussion in 'C Programming' started by holysmoke, Aug 19, 2008.

  1. holysmoke

    holysmoke Guest

    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!
     
    holysmoke, Aug 19, 2008
    #1
    1. Advertising

  2. In article <>,
    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.
     
    Richard Tobin, Aug 19, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dead RAM
    Replies:
    20
    Views:
    1,171
    John Harrison
    Jul 14, 2004
  2. D Senthil Kumar

    macro name from macro?

    D Senthil Kumar, Sep 20, 2003, in forum: C Programming
    Replies:
    1
    Views:
    603
    Jack Klein
    Sep 21, 2003
  3. sounak

    to get macro name from macro value

    sounak, Nov 22, 2005, in forum: C Programming
    Replies:
    17
    Views:
    523
    Mark McIntyre
    Nov 22, 2005
  4. Replies:
    24
    Views:
    617
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
    Dec 1, 2006
  5. Patrick Kowalzick
    Replies:
    5
    Views:
    501
    Patrick Kowalzick
    Mar 14, 2006
Loading...

Share This Page