hi,
in order to facilitate creating classes that are derived from basic types, i have started to implement a class Mapping that is supposed to behave exactly like a real mapping.
using http://pike.ida.liu.se/generated/manual/modref/ex/lfun_3A_3A.html as a guideline, i have implemented all functions that i believe a mapping needs to support.
please tell me if there is anything missing or wrong. if i don't get any complaints i am looking for a good place to check this into pike cvs. would Mapping.Mapping be a good place for this?
greetings, martin.
o Your class don't support the weak flags. Otoh it's not possible to do transparently since set_weak_flag currently doesn't try lfuns.
o cast: The casts to int, float, string and multiset are meaningless since they won't work.
o No `+=? Ok, it's optional but allows for good optimization.
o Most C level functions that take mappings don't accept objects, so explicit casting will be necessary there.
On Sat, Jan 29, 2005 at 01:00:01AM +0100, Martin Stjernholm, Roxen IS @ Pike developers forum wrote:
o Your class don't support the weak flags. Otoh it's not possible to do transparently since set_weak_flag currently doesn't try lfuns.
what will be the effect of this when using the class instead of a real mapping?
o cast: The casts to int, float, string and multiset are meaningless since they won't work.
i know, i left them in to have it display the error pike gives when trying that, because i am not happy with the default errormessage which should not use %O on this, but display the name of the class. (i'll try to fix that though. can i get the name of this_program() as a string?)
o No `+=? Ok, it's optional but allows for good optimization.
`+= was documented as being used for a destructive append. since i want to emulate mappings, += should not be destructive.
o Most C level functions that take mappings don't accept objects, so explicit casting will be necessary there.
well, most of the other code won't either, so the user will have to be doing castings anyways. though it would be nice if pike would generally allow objects with _is_type("mapping") being true, where the allowed type is mapping.
greetings, martin.
what will be the effect of this when using the class instead of a real mapping?
You can't set weak flags on it using set_weak_flag.
can i get the name of this_program() as a string?)
function_name(this_program), but it's not always it gives the name you want to show. master()->describe_program(this_program) should do a better job.
`+= was documented as being used for a destructive append. since i want to emulate mappings, += should not be destructive.
It's only used when it's possible to be destructive without observable side-effects. Real mappings can do the same optimization.
On Sat, Jan 29, 2005 at 06:45:00PM +0100, Martin Stjernholm, Roxen IS @ Pike developers forum wrote:
what will be the effect of this when using the class instead of a real mapping?
You can't set weak flags on it using set_weak_flag.
i gathered that, i meant to ask what is the effect of not being able to do that?
also, could i add: this_program set_weak_flag(); { set_weak_flag(data, 1); return this; }
`+= was documented as being used for a destructive append. since i want to emulate mappings, += should not be destructive.
It's only used when it's possible to be destructive without observable side-effects. Real mappings can do the same optimization.
oh, i was not aware of that,
but does that mean that when i have the statement: foo += bar; where foo and bar are of some arbitrary class that has `+= i have no control over wether `+= is even used? ie, pike could be turning this into foo=foo+bar; if it thinks there may be sideeffects?
greetings, martin.
i gathered that, i meant to ask what is the effect of not being able to do that?
Your simulated class can't use the feature of weak references to indices and/or values that real mappings have.
also, could i add: this_program set_weak_flag(); { set_weak_flag(data, 1); return this; }
Something like that is probably the best you can do, yes. But I think your function should take a bitfield argument like the real set_weak_flag so that users can set indices and values weak separately, and remove the weak bits again. There's also a get_weak_flag to query the current settings.
but does that mean that when i have the statement: foo += bar; where foo and bar are of some arbitrary class that has `+= i have no control over wether `+= is even used? ie, pike could be turning this into foo=foo+bar; if it thinks there may be sideeffects?
Yes, exactly. The identifier `+= is a bit misleading since it has no real connection with the += operator.
On Sat, Jan 29, 2005 at 06:45:00PM +0100, Martin Stjernholm, Roxen IS @ Pike developers forum wrote:
can i get the name of this_program() as a string?)
function_name(this_program), but it's not always it gives the name you want to show.
right, i get 0
master()->describe_program(this_program) should do a better job.
here i get the whole filepath which is usable i guess.
another question in that light: since _sprintf() returns the mapping for %O i get:
Cannot cast /local/home/mbaehr/cvs/cvs.sourceforge.net/pleac/pleac/pleac/include/pike/ch05/Mapping to string. Mapping.pike:24: ([ /* 1 element */ 1: 1 ])->cast("string") mapping:8: ...
which is not really readable if the mapping gets larger. it seems to come out of backtrace()[-1]->fun, where i can't seem to assign another value. maybe i need to create a whole new BacktraceFrame
greetings, martin.
pike-devel@lists.lysator.liu.se