to identify the caller of a function

Discussion in 'C++' started by Rahul, Jan 9, 2008.

  1. Rahul

    Rahul Guest

    Hi Everyone,

    I have a function utility() which is a part of a library and i would
    like to know as to who is
    invoking the function. I could request that information as a parameter
    enumeration, but i would like to know if there is any other smart way
    of doing the same?

    Thanks in advance!!!
    Rahul, Jan 9, 2008
    #1
    1. Advertising

  2. Rahul

    dizzy Guest

    Rahul wrote:

    >
    >
    > Hi Everyone,
    >
    > I have a function utility() which is a part of a library and i would
    > like to know as to who is
    > invoking the function. I could request that information as a parameter
    > enumeration, but i would like to know if there is any other smart way
    > of doing the same?


    My way of doing that involves using macros and a non standard predefined
    macro (but that exists on many current compilers and even if it didn't you
    can easily detect that and provide some dummy version) so it might not be
    that smart. But it works nicely, like this:

    // this is your real function (that will receive as "caller" the callee)
    void func_real(type1 arg1, type2 arg2, char const* caller);
    #define func(arg1, arg2) func_real((arg1), (arg2), __FUNCTION__)

    Notice the usage of __FUNCTION__. The idea is when you "call" func you just
    expand the macro, expanding to "func_real((arg1), (arg2), __FUNCTION__)"
    and __FUNCTION__ (the non standard predefined macro I mentioned) will be
    filled by the preprocessor with the current function name so you get that
    to your actual function.

    If someone has better options that still make it possible to use normal
    function call "func(arg1, arg2)" syntax without macros please advise...

    --
    Dizzy
    dizzy, Jan 9, 2008
    #2
    1. Advertising

  3. Rahul

    Grizlyk Guest

    Rahul wrote:
    >
    > I have a function utility() which is a part of a library
    > and i would like to know as to who is invoking the function.
    > I could request that information as a parameter enumeration,
    > but i would like to know if there is any other smart way
    > of doing the same?


    What do you want to do with the information about caller?
    If(caller is ok)...?

    If you want to change behaviour of called function depended from
    caller, then there is other legal ways to do it, for example, to make
    for caller a class with a member, which will do correct things for the
    caller.

    Maksim A. Polyanin
    old page about some C++ improvements:
    http://grizlyk1.narod.ru/cpp_new
    Grizlyk, Jan 9, 2008
    #3
  4. Rahul

    Rahul Guest

    On Jan 9, 6:48 pm, Grizlyk <> wrote:
    > Rahul wrote:
    >
    > > I have a function utility() which is a part of a library
    > > and i would like to know as to who is invoking the function.
    > > I could request that information as a parameter enumeration,
    > > but i would like to know if there is any other smart way
    > > of doing the same?

    >
    > What do you want to do with the information about caller?
    > If(caller is ok)...?
    >
    > If you want to change behaviour of called function depended from
    > caller, then there is other legal ways to do it, for example, to make
    > for caller a class with a member, which will do correct things for the
    > caller.
    >
    > Maksim A. Polyanin
    > old page about some C++ improvements:http://grizlyk1.narod.ru/cpp_new


    well, the logic of utility depends on the caller, and moreover utility
    wouldn't like to service unknown callers ( as in callers should
    register before hand )...
    Rahul, Jan 9, 2008
    #4
  5. Rahul wrote:
    > On Jan 9, 6:48 pm, Grizlyk <> wrote:
    >> Rahul wrote:
    >>
    >>> I have a function utility() which is a part of a library
    >>> and i would like to know as to who is invoking the function.
    >>> I could request that information as a parameter enumeration,
    >>> but i would like to know if there is any other smart way
    >>> of doing the same?

    >>
    >> What do you want to do with the information about caller?
    >> If(caller is ok)...?
    >>
    >> If you want to change behaviour of called function depended from
    >> caller, then there is other legal ways to do it, for example, to make
    >> for caller a class with a member, which will do correct things for
    >> the caller.
    >>
    >> Maksim A. Polyanin
    >> old page about some C++ improvements:http://grizlyk1.narod.ru/cpp_new

    >
    > well, the logic of utility depends on the caller, and moreover utility
    > wouldn't like to service unknown callers ( as in callers should
    > register before hand )...


    If the callers register, they should be given unique IDs, so you just
    make 'utility()' request the ID from the caller (as one of its args).

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jan 9, 2008
    #5
  6. On Jan 9, 5:54 pm, Rahul <> wrote:
    >
    > well, the logic of utility depends on the caller, and moreover utility
    > wouldn't like to service unknown callers ( as in callers should
    > register before hand )...- Hide quoted text -
    >


    The words "design error" are flashing in big red letters in
    my brain ... it doesn't make sense to disqualify callers
    who have linked to your library, that's the sort of security
    you'd impose on a distributed service, not a function call.

    If you still want to impose something like this, then just
    have:

    typedef <something here> UtilityUseHandle;

    // callers must get a handle first ...
    // (function concocts a verifyable handle and returns it)
    UtilityUseHandle register(<some credentials>);

    // utility function - must pass valid handle else
    // it'll throw an InvalidUtilityUseHandle exception
    <retval> use_utility_fn(
    const UtilityUseHandle& valid_registered_handle,
    <other parameters>
    ) /* throw (InvalidUtilityUseHandle) */ ;

    // finished, thank you ...
    void unregister(
    const UtilityUseHandle& valid_registered_handle
    ) /* throw (InvalidUtilityUseHandle) */ ;
    tragomaskhalos, Jan 9, 2008
    #6
  7. Rahul

    Grizlyk Guest

    dizzy wrote:
    >
    > void func_real(type1 arg1, type2 arg2, char const* caller);
    > #define func(arg1, arg2) func_real((arg1), (arg2), __FUNCTION__)
    >
    > __FUNCTION__ will be filled by the preprocessor with the
    > current function name so you get that to your actual function.
    >
    > If someone has better options that still make it possible
    > to use normal function call "func(arg1, arg2)" syntax
    > without macros please advise...


    And what will be?

    ***
    typedef int type1;
    typedef int type2;

    //
    typedef
    void
    (*t_func)
    (type1 arg1, type2 arg2);

    typedef
    void
    (*t_func_real)
    (type1 arg1, type2 arg2, t_func func);

    //
    template<t_func_real Fn>
    class Tcaller
    {
    public:

    static
    void
    call(type1 arg1, type2 arg2)
    {
    Fn(arg1, arg2, call);
    }
    };

    //usage
    void func_real(type1 arg1, type2 arg2, t_func func);

    void
    foo(type1 arg1, type2 arg2)
    {
    Tcaller<func_real>::call(arg1,arg2);
    }

    ***

    Is it better? I am not sure.

    Maksim A. Polyanin
    old page about some C++ improvements:
    http://grizlyk1.narod.ru/cpp_new
    Grizlyk, Jan 9, 2008
    #7
  8. Rahul

    Grizlyk Guest

    Rahul wrote:
    >
    > well, the logic of utility depends on the caller, and
    > moreover utility wouldn't like to service unknown callers
    > ( as in callers should register before hand )...


    What the purpose of the registration and what means "unknown"? What
    kind of information of caller are expected by service?

    Do you want to trace usage of the service (called)? There is
    development stuffs as profiler.

    If you need portable real-time tracing as normal execution of service,
    that access to the service can be done, for example, as interface like
    open-write-close.

    Maksim A. Polyanin
    old page about some C++ improvements:
    http://grizlyk1.narod.ru/cpp_new
    Grizlyk, Jan 9, 2008
    #8
    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. M. Moennigmann

    allocate 2d array in function and return it to caller

    M. Moennigmann, May 28, 2004, in forum: C Programming
    Replies:
    8
    Views:
    7,287
    Dave Thompson
    Jun 10, 2004
  2. popov
    Replies:
    2
    Views:
    338
    popov
    Dec 10, 2003
  3. Replies:
    8
    Views:
    664
    James Kanze
    May 10, 2007
  4. Mark
    Replies:
    2
    Views:
    385
  5. Thomas Mlynarczyk

    Function.caller if not called from within a function

    Thomas Mlynarczyk, Feb 26, 2005, in forum: Javascript
    Replies:
    4
    Views:
    179
    Thomas Mlynarczyk
    Feb 26, 2005
Loading...

Share This Page