• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

Polymorphic object serialization/creation?

Hello all.  I have a question about implementing object serialization in c++.  

The goal is that objects should be able to write out their type (typeid().name()) in addition to their data members.  When the file is read, an object of the appropriate type is allocated and then constructed from the ifstream.  

I've seen remarks about how MFC does this with CRuntimeClass and CObject, but I'd like to stay away from macros.  Assuming that all classes are derived from a base CSerial class, does anyone have any suggestions about how to do this?  Any information appreciated.  Thanks.
0
mweagle
Asked:
mweagle
  • 2
  • 2
1 Solution
 
KangaRooCommented:
To read back, you need a 'database' containing the typenames associated with methods to read, write and create objects. This database has to be filled, so you'll need some code to execute at startup, possibly by initializing some static. The use of macro's just simplifies that, but they are not neccesary.
You can avoid macro's for with the use of a template class with a static member.

template<typename T> class RegisterType
{
     static bool is_registered;
};

template<typename T>
bool RegisterType<T>::is_registered = TypeDB::Register(typeid(T).name());

// Somewhere in your implemamtation units you instantiate the template:

template RegisterType<MyClass>;


Another thing, you don't need inheritance. The methods associated with each type to read/write can be associated as functors or function pointers. This has the advantage that you can serialize builtin types and library types that you don't have control over.

I've done a serialization scheme once (with macro's though). I can paste some relevant pieces if you're interested.
0
 
mweagleAuthor Commented:
I was going to use inheritance in order to use overloaded Serialization operators.  Then, when I read in a type from the file I could allocate an object of that type, read in the type-specific data, and then return a pointer to the base class.  Does this sound reasonable?  I'm not worried about types that aren't derived from this base class.

However, this is pretty close to what I had in mind.  Is TypeDB::Register a class with a static Register function, a static database (std::map perhaps) of classnames to create functions?  

Any code snippets would be great, especially since I want to force clients of this scheme to implement the needed functions and I'm not clear where the static instantiation has to occur.  Thanks again for your help.
0
 
mbormannCommented:
Perhaps you can look at
http://www.codefarms.com/ppf/
http://www.swparts.com/products/radixcpp.htm
?

I am a Java guy ,so perhaps if you read these pages it will help you out ,pls feel free to ask me b4 15 March

http://java.sun.com/products/jdk/1.2/docs/guide/serialization/spec/serialTOC.doc.html

http://java.sun.com/docs/books/tutorial/essential/io/serialization.html

simple example
http://www.j-nine.com/pubs/Webtech-sep97/object_serialization.htm

If u have any problems refer to
http://www.cs.wisc.edu/~solomon/cs537/java-tutorial.html

Remember to implement a interface (pure virtual class in C++) means it forces the implementing class to have the functions defined in that interface.

I know its not precise but sorry
:(
0
 
mweagleAuthor Commented:
I managed to figure out how to do it with a template class and a base CSerial class that does not require (the dreaded ;) macros.  Thanks for the links though.
0
 
mbormannCommented:
:)
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now