Deserializing const fields

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Thu Sep 6 12:54:46 PDT 2012


On 9/6/12 8:40 PM, Jacob Carlborg wrote:
> On 2012-09-05 09:18, Andrei Alexandrescu wrote:
>
>> Deserialization is a low-level operation, and it shouldn't invoke a
>> constructor because the object has been constructed at serialization
>> time.
>>
>> I suggest you allocate a chunk of raw memory, deserialize into it, then
>> cast it to the object type.
>
> I gave this a try but I have two questions:
>
> 1. Is that safe to do with immutable fields?

Except for pointers, which of course need to be handled carefully 
whether immutable or not, it's fine to deserialize into immutable fields 
as long as you clarify to the compiler you're dealing with ubyte[] and 
cast to the object type after.

> 2. If I'm allocating a chunk of raw memory then I would need to recreate
> the vtable, classinfo and other stuff. Would it be safe to just create a
> new instance with the "_d_newclass"? This is the function used by the
> runtime when "new" is used. This will allocate the necessary memory,
> handle all the extra stuff but it won't call the constructor.

Yes, that's the ticket.


Andrei


More information about the Digitalmars-d mailing list