FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Inheritance -- array of base classes

 
Post new topic   Reply to topic     Forum Index -> Doost
View previous topic :: View next topic  
Author Message
baxissimo



Joined: 23 Oct 2006
Posts: 241
Location: Tokyo, Japan

PostPosted: Sun Jun 29, 2008 11:14 pm    Post subject: Inheritance -- array of base classes Reply with quote

Consider the following:
Code:

class Shape {
}
class Square : Shape {
   [...]
}
class Circle : Shape {
   [...]
}

class MyClass {
    Shape[] shapes;
    [...]
}


Now we want to write the serialize/unserialize routines for MyClass. This doesn't look like it will work at all with the current design. A proper solution requires saving some information about the actual most derived class stored and also determining that most derived type at runtime. It doesn't seem like the code currently does this anywhere.

Thoughts on how to fix this?
Back to top
View user's profile Send private message
aarti_pl



Joined: 25 Jul 2006
Posts: 28

PostPosted: Mon Jun 30, 2008 2:38 pm    Post subject: Reply with quote

This case is a bit more tricky. I don't have solution, but there seems to be two ways of solving this problem:
1. Registering classes like in
http://www.dsource.org/projects/serialization/browser/trunk/serialization/serializer.d

I didn't investigated how does it work, but it seems that it work...

2. Using virtual functions to serialize classes. I don't know exact solution now, but I could make work below example:

Code:
unittest { testCase.execute("load/dump - derived classes", {
    Base input;
    string output;

    input = new TransparentClass(-21, 2.11, "text1", 128, -127);
    output = input.dump;
    trace(output);

    input = new Base(10, -10);
    output = input.dump;
    trace(output);
});}


by adding following method to Base & TransparentClass (quick hack):

Code:
string dump() {
        auto arch = serializer.archive;

        if (!arch.traverse(this)) throw new ParsingException("Wrong format");
        return arch.storage;
}


So it should be doable somehow...
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Doost All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group