Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > to identify the caller of a function

Reply
Thread Tools

to identify the caller of a function

 
 
Rahul
Guest
Posts: n/a
 
      01-09-2008


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!!!
 
Reply With Quote
 
 
 
 
dizzy
Guest
Posts: n/a
 
      01-09-2008
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

 
Reply With Quote
 
 
 
 
Grizlyk
Guest
Posts: n/a
 
      01-09-2008
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
 
Reply With Quote
 
Rahul
Guest
Posts: n/a
 
      01-09-2008
On Jan 9, 6:48 pm, Grizlyk <(E-Mail Removed)> 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 )...
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      01-09-2008
Rahul wrote:
> On Jan 9, 6:48 pm, Grizlyk <(E-Mail Removed)> 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


 
Reply With Quote
 
tragomaskhalos
Guest
Posts: n/a
 
      01-09-2008
On Jan 9, 5:54*pm, Rahul <(E-Mail Removed)> 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) */ ;



 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      01-09-2008
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
 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      01-09-2008
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
 
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
How to identify, when caller press keys waruna Case Modding 1 09-09-2009 02:06 PM
How to identify whether a function is module scoped function orstatic method of a class by using its fully qualified name srinivasan srinivas Python 0 07-02-2008 10:32 AM
Is it possible to identify caller function from a callee? all.junks@gmail.com C++ 8 05-10-2007 09:48 AM
Function.caller if not called from within a function Thomas Mlynarczyk Javascript 4 02-26-2005 08:14 PM
Can't get function caller if the caller is from a function within a popup window Mark Javascript 2 04-03-2004 07:57 AM



Advertisments