May 09, 2006 Re: Writing Classes to File | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeremy | In article <e3o138$1ujn$1@digitaldaemon.com>, Jeremy says... > >In article <e3o0d4$1tdc$1@digitaldaemon.com>, Sean Kelly says... >> >>Jeremy wrote: >>> Is there a way I can just write a whole class to a file? >>> >>> For example, why can't I do: >>> >>> file.writeExact(someClass, someClass.sizeof); >>> >>> This seems to just write 4 bytes, which must be the pointer. >>> >>> The compiler complains when I do this: >>> >>> file.writeExact(*someClass, (*someClass).sizeof); >>> >>> Saying that '*' is only used infront of pointers -- but aren't class always represented with pointers? >> >>They are, but I think they don't have the same semantics as pointers insofar as the type is concerned. This is what inspired me to make the "isizeof" proposal a few weeks ago, as there's currently no way to get a class' size at compile-time. However, you can get it a run-time via: >> >>someClass.classinfo.init.length >> >> >>Sean > >Interesting... so could this be done: > >file.writeExact(someClass, someClass.classinfo.init.length); > >to write the entire class to a file? (I'd like to do some zlib compression on it >too :-D ) > > Just for everyone who reads this later -- the above works. I can write the entire class to a file for later reading/reconstruction with only that writeExact function call. Ahhh, my saving/loading troubles just got much easier :) |
May 09, 2006 Re: Writing Classes to File | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brad Roberts | In article <Pine.LNX.4.64.0605082013370.2422@bellevue.puremagic.com>, Brad Roberts says... > >On Mon, 8 May 2006, Jeremy wrote: > >> In article <e3o0d4$1tdc$1@digitaldaemon.com>, Sean Kelly says... >> > >> >Jeremy wrote: >> >> Is there a way I can just write a whole class to a file? >> >> >> >> For example, why can't I do: >> >> >> >> file.writeExact(someClass, someClass.sizeof); >> >> >> >> This seems to just write 4 bytes, which must be the pointer. >> >> >> >> The compiler complains when I do this: >> >> >> >> file.writeExact(*someClass, (*someClass).sizeof); >> >> >> >> Saying that '*' is only used infront of pointers -- but aren't class always represented with pointers? >> > >> >They are, but I think they don't have the same semantics as pointers insofar as the type is concerned. This is what inspired me to make the "isizeof" proposal a few weeks ago, as there's currently no way to get a class' size at compile-time. However, you can get it a run-time via: >> > >> >someClass.classinfo.init.length >> > >> > >> >Sean >> >> Interesting... so could this be done: >> >> file.writeExact(someClass, someClass.classinfo.init.length); >> >> to write the entire class to a file? (I'd like to do some zlib compression on it >> too :-D ) > >Even with this, direct writing of a class like this will only work in the simplest of cases. It won't dive into sub structures, for example, char[]'s. Object serialization for the general case is a lot more complicated. > >Later, >Brad Hrm... So after I write the class, I should also write the char[]'s seperately with another write function. This shouldn't be too much of a problem... my class consists mostly of floats/ints/doubles |
May 09, 2006 Re: Writing Classes to File | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeremy | In article <e3p2me$oee$1@digitaldaemon.com>, Jeremy says... > >In article <Pine.LNX.4.64.0605082013370.2422@bellevue.puremagic.com>, Brad Roberts says... >> >>On Mon, 8 May 2006, Jeremy wrote: >> >>> In article <e3o0d4$1tdc$1@digitaldaemon.com>, Sean Kelly says... >>> > >>> >Jeremy wrote: >>> >> Is there a way I can just write a whole class to a file? >>> >> >>> >> For example, why can't I do: >>> >> >>> >> file.writeExact(someClass, someClass.sizeof); >>> >> >>> >> This seems to just write 4 bytes, which must be the pointer. >>> >> >>> >> The compiler complains when I do this: >>> >> >>> >> file.writeExact(*someClass, (*someClass).sizeof); >>> >> >>> >> Saying that '*' is only used infront of pointers -- but aren't class always represented with pointers? >>> > >>> >They are, but I think they don't have the same semantics as pointers insofar as the type is concerned. This is what inspired me to make the "isizeof" proposal a few weeks ago, as there's currently no way to get a class' size at compile-time. However, you can get it a run-time via: >>> > >>> >someClass.classinfo.init.length >>> > >>> > >>> >Sean >>> >>> Interesting... so could this be done: >>> >>> file.writeExact(someClass, someClass.classinfo.init.length); >>> >>> to write the entire class to a file? (I'd like to do some zlib compression on it >>> too :-D ) >> >>Even with this, direct writing of a class like this will only work in the simplest of cases. It won't dive into sub structures, for example, char[]'s. Object serialization for the general case is a lot more complicated. >> >>Later, >>Brad > >Hrm... So after I write the class, I should also write the char[]'s seperately with another write function. This shouldn't be too much of a problem... my class consists mostly of floats/ints/doubles > > offset contents 0 pointer to vtable 4 monitor 8... non-static members from the D abi def. so you also write and read pointer to vtbl, is this safe enough? especially when exchanging between different program? |
May 09, 2006 Re: Writing Classes to File | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeremy | Jeremy schrieb:
> Hrm... So after I write the class
a class is called "object" after instantiation ...
|
May 09, 2006 Re: Writing Classes to File (zlib) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeremy | >>
>>Interesting... so could this be done:
>>
>>file.writeExact(someClass, someClass.classinfo.init.length);
>>
>>to write the entire class to a file? (I'd like to do some zlib compression on it
>>too :-D )
>>
>>
OK, so I am able to write the class to file. However, I am writing many objects of the same class to a file and I want to pass it through some compression. What is the best way to do this?
1) Should I allocate memory the size of all my objects, writeExact all my objects to that memory space, and then use zlib compression on that memory space?
2) or write everything to the file, then ZIP up the file?
3) or zlib compress each object's data before writing to the file? (e.g. if I
was saving 10 objects, I'd do 10 individual zlib compressions and writes) --
would this result in the same space usage as option (1)?
|
May 09, 2006 Re: Writing Classes to File | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chad J | Chad J wrote: [...] > > Also, beware the object reference... > > class Mobile > { > struct fields > { > int x, y; > int health; > > char[] name; // <--- There be monsters here! > Container backpack; // <--- > } > fields myVars; [...] > For handling the references, BCS, did you come up with something different by any chance? I mentioned it a few posts back but to summarize, reference items were dumper to the file first (after recording the current file location) and then file pointers were used in the parent object. This has the odd effect that children end up in the file first but oh well. To help find things I put a header at the start of the file (to begin with it had dummy values) and then when I knew where things ended up, I overwrote the header. I have a linked list example I can post if anyone is interested but it will have to wait till Monday (I'm books until then). |
May 10, 2006 Re: Writing Classes to File | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | In article <e3qg96$2vio$1@digitaldaemon.com>, BCS says... [...] >I have a linked list example I can post if anyone is interested but it will have to wait till Monday (I'm books until then). Um, I'm not a busy as I thoght. Here it is. works on linux (dmd ~v.153 I think) may need some tweeking for win make shure ./test is not an important file as it get trashed. -------------------------- import std.stdio; import std.stream; int main(char[][] argv) { LLItem List1, List2; // construct a linked list List1 = new LLItem(argv, 0); // open a file Stream str = new File("./test", FileMode.OutNew); long pos, first; // store the "header" pos = str.position; str.writeExact(&first, long.sizeof); // dump list first = List1.DumpToFile(str); // over write header str.position = pos; str.writeExact(&first, long.sizeof); // re open file str.close; str = new File("./test",FileMode.In); // read header str.readExact(&first, long.sizeof); // read list str.position = first; List2 = LLItem.ReadFromFile(str); // print list while(null !is List2) { writef("%d:%s\n", List2.integer, List2.string); List2 = List2.next; } return 0; } class LLItem { char[] string; int integer; LLItem next; private this(){} // turn list of args into a linked list this(char[][] args, int i) { string = args[0]; integer = i; if(1 < args.length) next = new LLItem(args[1..$], i+1); } static struct LLItemDump { int strLength; long string; int integer; long next; } long DumpToFile(Stream outs) { LLItemDump dump; // deal with int dump.integer = integer; // deal with next if(null is next) dump.next = 0; else dump.next = next.DumpToFile(outs); // deal with string dump.strLength = string.length; dump.string = outs.position; outs.writeExact(string.ptr, string.length); // dump long pos = outs.position; outs.writeExact(&dump, LLItemDump.sizeof); return pos; } static LLItem ReadFromFile(Stream ins) { //get item LLItemDump dump; ins.readExact(&dump, LLItemDump.sizeof); LLItem ret = new LLItem; //deal with int ret.integer = dump.integer; // deal with next if(0 == dump.next) ret.next = null; else { ins.position = dump.next; ret.next = LLItem.ReadFromFile(ins); } //deal with string ret.string.length = dump.strLength; ins.position = dump.string; ins.readExact(ret.string.ptr, dump.strLength); return ret; } } |
Copyright © 1999-2021 by the D Language Foundation