Binary data-structure serialization

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Jun 2 13:36:19 PDT 2010


On 06/02/2010 03:34 PM, bearophile wrote:
> Andrei Alexandrescu:
>> To ensure the destructor is called, I guess we need this:
>>
>> @system struct StackAllocated(T)
>> {
>>       private ubyte buffer[__traits(classInstanceSize, T)] = void;
>>       @property T payload() { return cast(T) buffer.ptr; }
>>       alias payload this;
>>       @disable this(); // meh
>>       @disable this(this);
>>       this(Args...)(auto ref Args args)
>>       {
>>           emplace!T(buffer.ptr, buffer.length);
>>       }
>>       ~this()
>>       {
>>           clear(payload);
>>       }
>> }
>
> Is this useful?
>
> static if (__traits(hasMember, T, "__dtor")) {
>       ~this() {
>           clear(payload);
>       }
> }
>
> Bye,
> bearophile

Absolutely!

Andrei


More information about the Digitalmars-d mailing list