Jump to page: 1 2
Thread overview
Writing Classes to File
May 08, 2006
Jeremy
May 08, 2006
BCS
May 08, 2006
Jeremy
May 09, 2006
BCS
May 09, 2006
Chad J
May 09, 2006
BCS
May 10, 2006
BCS
May 08, 2006
Sean Kelly
May 08, 2006
Jeremy
May 09, 2006
Brad Roberts
May 09, 2006
Jeremy
May 09, 2006
icee
May 09, 2006
dennis luehring
May 09, 2006
Jeremy
Re: Writing Classes to File (zlib)
May 09, 2006
Jeremy
May 08, 2006
Brad Anderson
May 08, 2006
Kyle Furlong
May 08, 2006
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?

Anyway, I want to save a class to file, so I can load it back. I was hoping there was a way to do this without having to write/read each element of the class seperately.


May 08, 2006
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?
> 
> Anyway, I want to save a class to file, so I can load it back. I was hoping
> there was a way to do this without having to write/read each element of the
> class seperately.
> 
> 
My solution is to declare a struct that has related data members in it. Then I give the class a DumpToFile method (generally named something else) that copies the data to the struct and does the writeExact thing. A readExact and revers translation do the other end. This has the advantage of providing a nice place to do some translations like endean swaps and recursive decent of trees (dump each child, recording where it is, place a FILE pointer in the struct and then dump the struct).

The biggest catch is keeping the struct<->class translators up to date with new rev of the class.

<script:coffee author.GetOn(SoapBox);)>

But that beings up my old dead horse: the "witheach" statement
http://www.digitalmars.com/d/archives/digitalmars/D/32232.html

<script:coffee author.GetOff(SoapBox);)>
May 08, 2006
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
May 08, 2006
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 )


May 08, 2006
In article <e3ntd5$1nnb$1@digitaldaemon.com>, BCS 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?
>> 
>> Anyway, I want to save a class to file, so I can load it back. I was hoping there was a way to do this without having to write/read each element of the class seperately.
>> 
>> 
>My solution is to declare a struct that has related data members in it. Then I give the class a DumpToFile method (generally named something else) that copies the data to the struct and does the writeExact thing. A readExact and revers translation do the other end. This has the advantage of providing a nice place to do some translations like endean swaps and recursive decent of trees (dump each child, recording where it is, place a FILE pointer in the struct and then dump the struct).
>
>The biggest catch is keeping the struct<->class translators up to date with new rev of the class.
>
><script:coffee author.GetOn(SoapBox);)>
>
>But that beings up my old dead horse: the "witheach" statement http://www.digitalmars.com/d/archives/digitalmars/D/32232.html
>
><script:coffee author.GetOff(SoapBox);)>

So, could you just include the struct inside your class?

e.g.:

struct ClassVars {
int x, y;
int health;
};

class someClass {
private:
ClassVars myVars;
..
public:
void saveTo(BufferedFile someFile) {
someFile.writeExact(&myVars, myVars.sizeof);
}
};

And in processing that class (e.g. did this character gain some health) -- you could check myVars.health etc.


May 08, 2006
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?
> 
> Anyway, I want to save a class to file, so I can load it back. I was hoping there was a way to do this without having to write/read each element of the class seperately.
> 
> 

Would Mango's pickling mechanism work here?

http://www.dsource.org/projects/mango

http://mango.dsource.org


BA
May 08, 2006
Brad Anderson wrote:
> 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?
>>
>> Anyway, I want to save a class to file, so I can load it back. I was hoping
>> there was a way to do this without having to write/read each element of the
>> class seperately.
>>
>>
> 
> Would Mango's pickling mechanism work here?
> 
> http://www.dsource.org/projects/mango
> 
> http://mango.dsource.org
> 
> 
> BA

Mango's solution is invasive, but for that technique, very well developed.

-- 
Kyle Furlong // Physics Undergrad, UCSB

"D is going wherever the D community wants it to go." - Walter Bright
May 09, 2006
In article <e3o17r$1un7$1@digitaldaemon.com>, Jeremy says...
>
>In article <e3ntd5$1nnb$1@digitaldaemon.com>, BCS says...
>>
>>My solution is to declare a struct that has related data members in it. Then I give the class a DumpToFile method (generally named something else) that copies the data to the struct and does the writeExact thing.
[...]
>>
>>The biggest catch is keeping the struct<->class translators up to date with new rev of the class.
>>
>><script:coffee author.GetOn(SoapBox);)>
>>
>>But that beings up my old dead horse: the "witheach" statement http://www.digitalmars.com/d/archives/digitalmars/D/32232.html
>>
>><script:coffee author.GetOff(SoapBox);)>
>
>So, could you just include the struct inside your class?

that would work and would reduce the different revs catch.
>
>e.g.:
>
>struct ClassVars {
>int x, y;
>int health;
>};

I'd put the struct in the class scope as well

class
{
static sreuct args
{
}
args a;

}
>
>class someClass {
>private:
>ClassVars myVars;
>..
>public:
>void saveTo(BufferedFile someFile) {
>someFile.writeExact(&myVars, myVars.sizeof);
>}
>};
>
>And in processing that class (e.g. did this character gain some health) -- you could check myVars.health etc.
>

But Ohh dose that get ungly. OTOH might not be to bad if you want to spilt out some sets of args.


May 09, 2006
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
May 09, 2006
I think you might want to check out http://www.runuo.com/
It emulates a server for the massively multiplayer game Ultima Online. I mention it only because it has a system for doing just this sort of thing - they call it serializing (saving) and deserializing (loading). Their setup may not be any better than the struct thing, but it is an alternative, and it's worked for this very large project.  I say it may not be any better because I have had my share of frustration with serializing and deserializing.  If you want, I'll write an example, but it would be quite lengthy (and I have differential equations homework to do) so maybe later if you say you want it.

That struct thing is interesting, and I'd like to see what kind of ideas there are for handling this task.

Also, beware the object reference...

class Mobile
{
   struct fields
   {
       int x, y;
       int health;

       char[] name;           // <---   There be monsters here!
       Container backpack;    // <---
   }
   fields myVars;

   ...

   void saveTo(BufferedFile someFile) {
       someFile.writeExact(&myVars, myVars.sizeof);

       // better add something like:
       someFile.writeExact(myVars.name.ptr, myVars.name.length);
       backpack.saveTo(someFile);
   }
   // careful when loading...
}

class Container : Item
{
    struct fields
    {
        Item[] contents;
        ...
    }
    fields myVars;
    ...
}

For handling the references, BCS, did you come up with something different by any chance?
« First   ‹ Prev
1 2