Range interface for std.serialization

Dmitry Olshansky dmitry.olsh at gmail.com
Tue Sep 24 13:27:00 PDT 2013


24-Sep-2013 21:02, Jacob Carlborg пишет:
> On 2013-08-28 13:20, Dmitry Olshansky wrote:
>> Taking into account that you've settled on keeping Serializers as
>> classes just finalize all methods of a concrete serializer that is
>> templated on archiver (and make it a final class).
>>
>> Should be as simple as:
>>
>> class Serializer {
>>      void put(T)(T item){ ...}
>>      //other methods per specific type
>> }
>>
>> final class ConcreteSerializer(Archiver) : Serializer {
>> final:
>>      ...
>>      //use Archiver here to implement these hooks
>> }
>
> I'm having quite hard time to figure out how this should work. Or I'm
> misunderstanding what you're saying.
>
> If I understand you correctly I should do something like:
>
> class Serializer
> {
>      void put (T) (T item)
>      {
>          static if (is(T == int))
>              serializeInt(item);
>
>      ...
>      }
>
>      abstract void serializeInt (int item);
> }
>
> But if I'm doing it that way I will still have the problem with a lot of
> methods that need to be implemented in the archiver.
>

If I'm correct archiver would have the benefit of templates and common 
code would be merged (so all of these in a concrete serializer do 
forward to archiver.write!int, archive.write!uint etc.) On the plus side 
of having a bunch of methods in Serializer you need exactly one 
ConcreteSerializer!(Archive) that implement them. And user-defined 
archiver need not to even think of this, just define single templated 
write (or put or whatever).

> Hmm, I guess it would be possible to minimize the number of methods used
> for built in types. There's still a problem with user defined types though.

Indeed. But it must be provided as a template in generic serializer. The 
benefit is that said logic to serialize arbitrary UDTs is implemented 
there once and for all. Archiver is then partially relived of it. To 
achieve that an archiver may need to provided some fundamental "hooks" 
like  startStruct/endStruct (I didn't think through exact ones).

-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list