Article View: de.comp.lang.c++
Article #249Re: Aufrufer einer Methode feststellen?
From: Armin M. Warda
Date: Fri, 11 Aug 1995 12:19
Date: Fri, 11 Aug 1995 12:19
81 lines
3086 bytes
3086 bytes
Stuart Maclean <S.D.Maclean@ecs.soton.ac.uk> schrieb mir
zur gleichen Anfrage in comp.lang.c++:
: In article <40cm1r$oej@fbi-news.informatik.uni-dortmund.de>,
: warda@veronica.informatik.uni-dortmund.de (Armin M. Warda) wrote:
: > Hello,
: >
: >is there a possibility to find out within a method who
: >called the method?
: >
: >I could explicitly give the 'this'-pointer as parameter
: >when calling a method:
: Isn't it neater when you dont have to explicitly pass this around 8-)
: Unfortunately, if YOU don't supply the info, the runtime system will have
: to do more work to get the caller object. What with looking at the stack
: and stuff, you're going to need to know your architecture/compiler quite
: well.
: Here's what I do to identify the calling object on the SPARC, compiler: GCC
: .text
: .align 4
: .global _caller
: _caller:
: mov %g1, %o1 ! %o1 is scratch, so OK to save %g1 there
:
: restore ! back to caller's register window (cached stack frame)
:
: mov %i0, %g1 ! GCC passes the 'this' pointer in %o0, which becomes
: ! %i0 in the new window. Grab it into %g1
:
: save ! back to the original window
:
: mov %g1, %o0 ! return value (ie the calling object) returned in %o0
:
: retl ! return to caller. Note this function is a LEAF !!!
:
: mov %o1, %g1 ! restore clobbered %g1.
: ! This is the delay slot instruction.
:
: ! end of _caller
: Note that this ASSUMES that there was indeed an object address in %i0 of
: the previous stack frame. In other words, we assume that the calling
: function was indeed a non-static member function, not an ordinary function
: or static member function.
: Then use as such
: class Foo {
: public:
: void Bar();
: };
:
: void Foo::Bar()
: {
: if ( caller() == this ) {
: this is an intra-object call....
: }
: NOTE : As per usual when comparing object equality by way of address
: equality, beware of multiple inheritance. For a discussion, see Meyers,
: item 17, page 57.
: But who ever uses MI anyway ;-)
: Hope this helps. Any comments welcomed.
: Later,
: Stuart
A.
--
-------------------------------------------------------------------------------
Armin M.Warda <http://ls4-www.informatik.uni-dortmund.de/QM/MA/warda/pi.html>
Universitaet Dortmund, Fachbereich Informatik, Lehrstuhl IV, D-44221 Dortmund
Technical Details
Message-ID:
<40fhs0$1ql@fbi-news.informatik.uni-dortmund.de>Path:
arc1-ca.pugleaf.net!Informatik.Uni-Dortmund.DE!veronica!wardaReferences:
<40clg8$oej@fbi-news.informatik.uni-dortmund.de>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!Informatik.Uni-Dortmund.DE!veronica!warda From: warda@veronica.informatik.uni-dortmund.de (Armin M. Warda) Newsgroups: de.comp.lang.c++ Subject: Re: Aufrufer einer Methode feststellen? Date: 11 Aug 1995 12:19:12 GMT Organization: CS Department, Dortmund University, Germany Sender: warda@informatik.uni-dortmund.de (Armin M. Warda) Message-ID: <40fhs0$1ql@fbi-news.informatik.uni-dortmund.de> References: <40clg8$oej@fbi-news.informatik.uni-dortmund.de> NNTP-Posting-Host: veronica.informatik.uni-dortmund.de Keywords: caller, this