By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,436 Members | 2,958 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,436 IT Pros & Developers. It's quick & easy.

to identify the caller of a function

P: n/a


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!!!
Jan 9 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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

Jan 9 '08 #2

P: n/a
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
Jan 9 '08 #3

P: n/a
On Jan 9, 6:48 pm, Grizlyk <grizl...@yandex.ruwrote:
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 )...
Jan 9 '08 #4

P: n/a
Rahul wrote:
On Jan 9, 6:48 pm, Grizlyk <grizl...@yandex.ruwrote:
>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
Jan 9 '08 #5

P: n/a
On Jan 9, 5:54*pm, Rahul <sam_...@yahoo.co.inwrote:
>
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 hereUtilityUseHandle;

// 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
<retvaluse_utility_fn(
const UtilityUseHandle& valid_registered_handle,
<other parameters>
) /* throw (InvalidUtilityUseHandle) */ ;

// finished, thank you ...
void unregister(
const UtilityUseHandle& valid_registered_handle
) /* throw (InvalidUtilityUseHandle) */ ;

Jan 9 '08 #6

P: n/a
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
Jan 9 '08 #7

P: n/a
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
Jan 9 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.