Level at which an OO object was ENTERED
From
pcc@21:1/5 to
All on Sun Feb 19 22:26:32 2023
Hi all,
Is there a simple (and fast) way to identify the level at which an OO object was ENTERED ?
To explain my problem :
I have a class (C) with a some methods that may be called individually, or recursively
Inside my class object (o), I need to execute some procedures in the context of the caller of the object.
For this I need to know the level from which the object was called.
This is not an issue if all methods were at the same level.
[info level] allows me to know the level of the caller of the METHOD.
But inside the object, I may call a method from inside another method.
and I have no way to know the recursion depth, as the call tree depends on the arguments of the first call of the object
(and this needs to be as fast as possible)
Thus, I need to know the level of the caller of the OBJECT.
Here is a short exemple of such a class :
oo::class create C {
constructor { args } {
puts "level = [info level] : constructor of [self] "
}
method + { lvl args } {
puts "level = [info level] : lvl=$lvl"
if { $args ne {} } { my {*}$args }
}
method color { color args } {
puts "level = [info level] : color=$color"
if { $args ne {} } { my {*}$args }
}
method out { msg args } {
puts "level = [info level] : out $msg"
if { $args ne {} } { my {*}$args }
}
method unknown { args} {
puts "level = [info level] : unknown $args"
}
export + >
}
# and here is a test procedure using the class example above :
proc test { args } {
puts "---- calling from level [ info level] -----"
catch {o destroy }
C create o
# call method individually
o color orange
o out "single message"
# call object with multiple methods
o + 1 color red out 123456 + 11 color blue
puts "--------------------------------"
}
test
I get this result : (level increase with call depth)
---- calling from level 1 -----
level = 2 : constructor of ::o
level = 2 : color=orange
level = 2 : out single message
level = 2 : lvl=1
level = 3 : color=red
level = 4 : out 123456
level = 5 : lvl=11
level = 6 : color=blue
--------------------------------
But I would need
---- calling from level 1 -----
level = 2 : constructor of ::o
level = 2 : color=orange
level = 2 : out single message
level = 2 : lvl=1
level = 2 : color=red
level = 2 : out 123456
level = 2 : lvl=11
level = 2 : color=blue
--------------------------------
Is there a way to achieve this ?
(for example with a command such as "info object level")
Best regards
Pascal
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)