What I am looking for is a function to take all the variables of an object and save them to a file. Vice-Versa there would be a function to load all those variables back into a cloned object.
This could be done very easily, if you have a limited type of object you want to save.
void save_object(object o,string file) { mapping v=mkmapping(indices(o),values(o)); v->__program=object_to_program_to_name(o); Stdio.write_file(file,encode_value(v)); }
object load_object(string file) { String s=Stdio.read_byets(file); mapping v=decode_value(s); object o=program_from_name(o)();
foreach (v;string varname;mixed vardata) catch { o[varname]=vardata; };
return o; }
string object_to_program_to_name(object o); program name_tO_program(string name);
Now, you may not use some variable flags with a construct like this, since those are invisible to the outside. It's probably better to have encoding/decoding methods inside the object, inherited from some base class.
Also, the program-string functions needs to be implemented. They can easily today, if the base class is a program file (".pike") and not some internal class (class Foo):
string object_to_program_to_name(object o) { program p=object_program(o); mapping m=master()->programs; // this mapping is better created by the master or cached somehow mapping n=mkmapping(values(m),indices(m)); // or use search return n[o] || error("Cannot encode that object\n"); }
program name_to_program(string name) { return (program)name; }
It's not best or fast, but maybe it can give you ideas. I don't think Pike's encode_value is what you really want to encode objects, while it can in some sort encode programs, in my experience, you rather often want to change the program for the objects rather then to keep them forever, but keep the data.
/ Mirar
Previous text:
2003-02-28 00:55: Subject: Save_Object
Coming from a LPC compiler background, is there anything within PIKE that would lean towards the save_object 'kfun' they have implemented in several compilers?
What I am looking for is a function to take all the variables of an object and save them to a file. Vice-Versa there would be a function to load all those variables back into a cloned object.
Anything or am I blowing smoke?
/ Brevbäraren