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?
On Thu, Feb 27, 2003 at 06:54:52PM -0500, gmvalic@cox.net wrote:
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.
sTeam (www.open-steam.org) has implemented something like that.
sTeam is essentially an lpmud written in pike. (it is a lot more, but that is a different topic)
greetings, martin.
No, that is a topic that surfaces occasionally. The encode_value function in Pike can encode objects to strings that can be stored on disk, stored in databases, sent over the net etc. It however needs to have access to the program the object came from and it needs the object to have an encode method that the Codec can call. The encode method should transform all the internal states of the object to a "plain" data structure (int/float/string/array/mapping/multiset). Since most objects have no other state and dependencies than the values in their variables, it would make sense to have this behaviour built into Pike in case there is no encoding method in the object.
/ Martin Nilsson (har bott i google)
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
On Fri, Feb 28, 2003 at 01:05:04AM +0100, Martin Nilsson (har bott i google) @ Pike (-) developers forum wrote:
No, that is a topic that surfaces occasionally. The encode_value function in Pike can encode objects to strings that can be stored on disk, stored in databases, sent over the net etc. It however needs to have access to the program the object came from
ok, sTeam needs that as well.
interresting about sTeam is however that an object can be stored and later reloaded without other objects loosing reference to it.
greetings, martin.
I don't know if that was a question, but if it was I once again point to persistent.pike. Possibly available in your nearest Roxen dist, and perhaps not. :)
/ Peter Bortas
Previous text:
2003-02-28 01:12: Subject: Re: Save_Object
On Fri, Feb 28, 2003 at 01:05:04AM +0100, Martin Nilsson (har bott i google) @ Pike (-) developers forum wrote:
No, that is a topic that surfaces occasionally. The encode_value function in Pike can encode objects to strings that can be stored on disk, stored in databases, sent over the net etc. It however needs to have access to the program the object came from
ok, sTeam needs that as well.
interresting about sTeam is however that an object can be stored and later reloaded without other objects loosing reference to it.
greetings, martin.
/ Brevbäraren
Since most objects have no other state and dependencies than the values in their variables, it would make sense to have this behaviour built into Pike in case there is no encoding method in the object.
Agreed, doing this by default would make a whole lot of sense as long as it was documented as such. I should add that my method only save _public_ variables which of course is not what you want if you use private variables for state (I designed that particular where they are used in this way to avoid having to deal with every single variable).
/ David Hedbor
Previous text:
2003-02-28 01:02: Subject: Save_Object
No, that is a topic that surfaces occasionally. The encode_value function in Pike can encode objects to strings that can be stored on disk, stored in databases, sent over the net etc. It however needs to have access to the program the object came from and it needs the object to have an encode method that the Codec can call. The encode method should transform all the internal states of the object to a "plain" data structure (int/float/string/array/mapping/multiset). Since most objects have no other state and dependencies than the values in their variables, it would make sense to have this behaviour built into Pike in case there is no encoding method in the object.
/ Martin Nilsson (har bott i google)
This will work: string enc = encode_value(obj, master->Codec());
You need to implement the _encode/_decode methods for the object, for example using a generic implementation:
string _encode() { mapping enc = mkmapping(indices(this), values(this)); foreach(enc; string key; mixed value) { if(functionp(value)) { m_delete(enc, key); } } return encode_value( enc, master()->Codec() ); }
void _decode(string data) { mapping decoded = decode_value(data, master()->Codec()); foreach(decoded; string key; mixed value) { catch { this[key] = value; }; } }
There is no way to make it automatically save public variables to the best of my knowledge (unless you make your own Codec()), but I think that the above solution is easy enough.
That said, if it by default did do what the above does, that wouldn't be bad thing.
/ David Hedbor
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
That wouldn't take care of the reference to the program in all cases, and the codec in the master would only handle it if the program is a pike module or a nested class in a pike module.
If one added additional constraints on the program, like that it must not contain any methods (except perhaps an implicit create function), then it would be possible to encode and decode such objects without the original program - the decoder would simply create an identical one instead. That would allow struct-like classes to be encoded and decoded as simply as mappings.
/ Martin Stjernholm, Roxen IS
Previous text:
2003-02-28 01:05: Subject: Save_Object
This will work: string enc = encode_value(obj, master->Codec());
You need to implement the _encode/_decode methods for the object, for example using a generic implementation:
string _encode() { mapping enc = mkmapping(indices(this), values(this)); foreach(enc; string key; mixed value) { if(functionp(value)) { m_delete(enc, key); } } return encode_value( enc, master()->Codec() ); }
void _decode(string data) { mapping decoded = decode_value(data, master()->Codec()); foreach(decoded; string key; mixed value) { catch { this[key] = value; }; } }
There is no way to make it automatically save public variables to the best of my knowledge (unless you make your own Codec()), but I think that the above solution is easy enough.
That said, if it by default did do what the above does, that wouldn't be bad thing.
/ David Hedbor
True. However in actuality, I think that kind of objects is and always will be rather rare in Pike.
/ David Hedbor
Previous text:
2003-02-28 01:33: Subject: Save_Object
That wouldn't take care of the reference to the program in all cases, and the codec in the master would only handle it if the program is a pike module or a nested class in a pike module.
If one added additional constraints on the program, like that it must not contain any methods (except perhaps an implicit create function), then it would be possible to encode and decode such objects without the original program - the decoder would simply create an identical one instead. That would allow struct-like classes to be encoded and decoded as simply as mappings.
/ Martin Stjernholm, Roxen IS
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
pike-devel@lists.lysator.liu.se