std.concurrency wrapper over MPI?
dsimcha
dsimcha at yahoo.com
Thu Aug 11 04:07:26 PDT 2011
On 8/11/2011 4:14 AM, Jacob Carlborg wrote:
> On 2011-08-07 21:28, dsimcha wrote:
>> In addition to the bug reports I filed, why is it necessary to write any
>> serialization code to serialize through the base class? What's wrong
>> with just doing something like:
>>
>> class Base {}
>> class Derived : Base {}
>>
>> void main() {
>> auto serializer = new Serializer(new XMLArchive!());
>>
>> // Introspect Derived and figure out all the details automatically.
>> serializer.register!(Derived);
>> }
>>
>
> I've been thinking about this and currently I don't see how this would
> be possible. When serializing through a base class reference the static
> type would be of the base class. But what I need is the static type of
> the subclass, to be able to loop through the tuple returned by tupleof.
> The only information I can get about the subclass is basically the fully
> qualified name.
>
> What I would need is some kind of associative array that maps strings to
> types, but as far as I know that's not possible, specially since the
> strings would be runtime values.
>
You have classinfo as a key, as you point out. You also already have a
template that's capable of serializing a class given that its static
type is exactly its dynamic type.
I was thinking something like:
class Serializer {
string delegate(Object)[TypeInfo_Class] registered;
void register(T)() {
registered[T.classinfo] = &downcastSerialize!(T);
}
void serialize(T)(T value) if(is(T : Object)) {
if(value.classinfo is T.classinfo) {
// Then the static type is exactly the runtime type.
// Serialize it the same way you do now.
} else {
enforce(value.classinfo in registered,
"Cannot serialize a " ~ value.classinfo.name ~
" because it has not been registered.");
return registered[value.classinfo](value);
}
}
string downcastSerialize(T)(Object value) if(is(T : Object)) {
auto casted = cast(T) value;
assert(casted);
assert(value.classinfo is T.classinfo);
return serialize(casted);
}
}
More information about the Digitalmars-d
mailing list