473,378 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

to identify the caller of a function



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
7 4269
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mark | last post by:
The situtation is that I'm trying to ensure that certain functions are only called by functions that I want them to be called from. I have a popup window which has a function which calls a function...
9
by: jaden10001 | last post by:
I have read that the function.caller method is now depricated. I see alot of browsers still support it but Opera 7 does not. Is there a way to get the name of the caller of a function in Opera 7?
4
by: Thomas Mlynarczyk | last post by:
Hi, I stumbled over a strange behaviour of Mozilla. When I want to access the caller property of a function that was not called from within another function, Mozilla seems to abort the script....
8
by: Rakehs | last post by:
Hi, I have a method Show() which is invoked by several methods at different intervals. I want to identify the calling method within Show() whenever it is invoked. How do i do this? Thanks in...
14
by: Genival | last post by:
Hello all... First sorry my bad English. Look next code Dim oDS as Dataset ' <= Caller oDs = MyFunc 'other caller type Dim i as integer = MyFunc
9
by: Csaba Gabor | last post by:
Inside a function, I'd like to know the call stack. By this I mean that I'd like to know the function that called this one, that one's caller and so on. So I thought to do: <script...
8
by: all.junks | last post by:
Hi, Let's say I'm in function foo. I am trying to find the function(or return address) which called foo. Initially, I thought I could use stack base pointer(ebp+4) to find the return address....
8
by: contactmayankjain | last post by:
Hi, Can you tell me is there some procedure to calculate the number of calls of a function during run time? How to insert a counter and how to increment it? and possibly which function have...
7
by: =?UTF-8?B?QW50w7NuaW8gTWFycXVlcw==?= | last post by:
Hi, Sorry if this's been discussed before, I couldn't find it. As well you know, the ECMAScript standard doesn't include any way to access a function's caller. This has been available on Mozilla...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.