Read/write structs in streams (Tango, and Strings)

Bill Baxter wbaxter at gmail.com
Wed Oct 22 08:21:44 PDT 2008


On Thu, Oct 23, 2008 at 12:11 AM, Robert Kosek
<robert.kosek at thewickedflea.com> wrote:
> Bill Baxter wrote:
>>
>> On Wed, Oct 22, 2008 at 11:24 PM, Steven Schveighoffer
>> <schveiguy at yahoo.com> wrote:
>> [...] And yes you need some kind of library if you want to stream
>> arbitrary
>> structs which may or may not contain pointers to disk.   And no,
>> neither Tango nor Phobos has such a library.
>>
>> --bb
>
>
> Okay, thanks.  To write a struct to a stream should really be like writing a
> buffer.  You point the writer at the memory and tell it how large it is, and
> it writes the block to the stream.  So it should to be able to write a
> pointer-less struct.

But if you care about portability you need to worry about things like
endianness and the variable size of D's real, creal, and ireal types.
Also difference of size in size_t from 32 bit to 64 bit platforms.  So
even without pointers there can be more to it than just dumping a
chunk of memory into a stream.

> That much is good, I can do string mangling/storage another way, probably.

Yep, if your needs are fairly simple then it's not that difficult to
come up with something.  There is something like an EndianStream in
Tango.  I think it does byteswapping on ints and floats and things for
you.

--bb


More information about the Digitalmars-d-learn mailing list