From: Grzegorz J. <ja...@he...> - 2004-06-01 01:10:54
|
Hi, On Mon, 31 May 2004 ch...@da... wrote: > Hello all, > > I=B4m a newbie that have just discovered this great tool and have a > question: it is possible to identify an object of a class? > > Let me explain: I want to implement a "log-able" class which would be mor= e > or less like the BeforeClass in the examples but, additionaly to identify > the methods that are called, I want to identify the parameters passed an= d > the object that have made the invocation in order to be able to > reconstruct the computation. > > Imagine that have a class A and two objects belonging to this class, let > say a1 and a2, how can I differenciate the invocation of the same method > in each object? Can someone recommend a reference or give an idea? (1) Do you want to add logging code at caller site or at callee site? (2) Do you want to log identity of caller or callee? (In the first paragraph you write about caller object, in the second about the callee object). (1)-caller, (2)-caller: use 'this' at call site. (1)-caller, (2)-callee: add code to take callee address just before the call is made, e.g. instrument p[14].fun->Foo(); like this: __logging_utility_log(p[14].fun, "Foo", "a", 5)->Foo("a", 5); and add template <class T> T* __logging_utility(T* p, /* other stuff */) { /* do actual logging, 'p' has identity of callee */ return p; } (1)-callee, (2)-caller: here you want calee to know at runtime who is the caller; as far as I know this cannot be done portably in C++ itself, so you either need to add some code on the caller's side or you need to use non-portable code to inspect the stack and find out the address of calling function (from which you can later figure out if it is a method of an object; if so, then it is likely to have object address as one of its arguments; this is all highly non-portable in terms of compiler and hardware, also optimization in some cases may affect the stack layout) (1)-callee, (2)-callee: use 'this' at callee site. One additional thing to watch for is that in class hierarchies with multiple inheritance or with virtual inheritance one object may have many valid addresses, so you have to normalize address obtained from 'this' by casting to one of the least derived base classes. (Finding such class is of course doable with OpenC++.) I hope one of above variants is what you meant. The thing you are trying to do (computation reconstruction) seems very interesting. Could you elaborate a little bit on the context where you want to apply it? Please let me know if you need more info. BR Grzegorz ################################################################## # Grzegorz Jakacki Huada Electronic Design # # Senior Engineer, CAD Dept. 1 Gaojiayuan, Chaoyang # # tel. +86-10-64365577 x2074 Beijing 100015, China # # Copyright (C) 2004 Grzegorz Jakacki, HED. All Rights Reserved. # ################################################################## |