What is foo_var_num supposed to be in your second example?
Also, the ID_STATIC flag was close to what I wanted. When using it, the inner class is invisible to index() when applied to an object but not when doing something like index(Foo.Bar).
I would like to get the following:
indices(Foo.Bar);
({ methods ... })
indices(Foo.Bar());
({ Gazonk methods ... })
Ie, the important thing is that when Gazonk is instantiated, there is a surrounding instance which has initialized some things.
/ Marcus Agehall (PacketFront)
Previous text:
2004-08-13 10:54: Subject: CMOD questions
Q1: How should I define my classes to get the following function:
[...]
Ie, I want a class within Foo that can only be instantiated using the Foo.get_bar() method.
I'm not quite sure what you mean by "can only".
The following might do what you want:
Foo.pmod:
static class bar {}; bar get_bar() { return bar(); }
Equvivalent cmod:
PIKECLASS bar flags ID_STATIC; { }
PIKEFUN object(bar) get_bar() { pop_n_elems(args); push_object(clone_object(bar_program, 0)); }
Q2: Is there a way that a Foo.bar() object (as described above) can access the variables defined in Foo? (The variables which are accessed thru the THIS-pointer that is.)
You mean via the parent-pointer.
Yes (of course, since everything that can be done at Pike-level, by definition can be done at C-level).
Something like the following ought to work:
PIKEVAR mixed foo;
PIKECLASS bar { PIKEFUN get_foo() { struct external_variable_context loc;
pop_n_elems(args);
loc.o = Pike_fp->current_object; loc.parent_identifier = Pike_fp->fun; if (loc.o->prog) loc.inherit = INHERIT_FROM_INT(loc.o->prog, loc.parent_identifier); find_external_context(&loc, 1);
low_object_index_no_free(Pike_sp, loc.o, foo_var_num + loc.inherit->identifier_level); Pike_sp++; } }
Getting the value for foo_var_num is left as an exercise. :-)
References to where things like this is already done within the Pike-source is appreciated.
interpret_functions.h contains lots of interesting code...
/ Henrik Grubbström (Lysator)