Range interface for std.serialization
Dmitry Olshansky
dmitry.olsh at gmail.com
Mon Aug 26 02:23:28 PDT 2013
26-Aug-2013 11:07, Jacob Carlborg пишет:
> On 2013-08-25 22:50, Dmitry Olshansky wrote:
>
>> Adapting the code by Jacob (Alternative AO2)
>>
>> auto archiver = new XmlArchive!(char)(outputRange);
>> auto serializer = new Serializer(archiver);
>> serializer.put(new Object);
>> serializer.put([1, 2, 3, 4]); //mix and match stuff as you see fit
>>
>> And even
>> copy(iota(1, 10), serializer);
>>
>> Would all work just fine.
>
> I'm still worried about how to get out deserialized objects, especially
> if they are serialized as a range.
Array or any container should do for a range.
I'm not 100% sure what kind of interface to use, but Serializer and
Deserializer should not be "shipped in one package" as in one class.
The two a mirror each other but in essence are always used separately.
Ditto about archiver/unarchiver they simply provide different
functionality and it makes no sense to reuse the same object in 2 ways.
Hence alternative 1 (unwrapping that snippet backwards):
//BTW why go new & classes here(?)
auto unarchiver = new XmlUnarchiver(someCharRange);
auto deserialzier = new Deserializer(unarchiver);
auto obj = deserializer.unpack!Object;
//for sequence/array in underlying format it would use any container
List!int list = deserializer.unpack!(List!int);
int[] arr = deserializer.unpack!(int[]);
IMO looks quite nice. The problem of how exactly should a container be
filled is open though.
So another alternative being more generic (the above could be consider
convenience over this one):
Vector!int ints;
deserilaizer.unpackRange!(int)(x => ints.pushBack(x));
Basically unpack next sequence of data (as serialized) by feeding it to
an output range using element type as param. And a simple lambda
qualifies as an output range.
Also take a look at the new digest API. I have an understanding that
serialization would do well to take the same general strategy - concrete
archivers as structs + polymorphic interface and wrappers on top.
I'm still missing something about separation of archiver and serializer
but in my mind these are tightly coupled and may as well be one entity.
One tough little thing to take care of in std.serialization is how to
reduce amount of constant overhead (indirections, function calls,
branches etc.) per item. Polymorphism is easily achieved on top of fast
and tight core the other way around is impossible.
--
Dmitry Olshansky
More information about the Digitalmars-d
mailing list