Solved

Polymorphic object serialization/creation?

Posted on 2000-03-08
5
251 Views
Last Modified: 2010-04-02
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
Comment
Question by:mweagle
  • 2
  • 2
5 Comments
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2599549
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
 

Author Comment

by:mweagle
ID: 2601977
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
 
LVL 5

Accepted Solution

by:
mbormann earned 150 total points
ID: 2611927
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
 

Author Comment

by:mweagle
ID: 2613396
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
 
LVL 5

Expert Comment

by:mbormann
ID: 2615232
:)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now