to identify the caller of a function

R

Rahul

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!!!
 
D

dizzy

Rahul said:
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...
 
G

Grizlyk

Rahul said:
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
 
R

Rahul

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 )...
 
V

Victor Bazarov

Rahul said:
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
 
T

tragomaskhalos

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) */ ;
 
G

Grizlyk

dizzy said:
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
 
G

Grizlyk

Rahul said:
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
 

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

Forum statistics

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

Latest Threads

Top