Solved

Polymorphic object serialization/creation?

Posted on 2000-03-08
5
250 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

707 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

18 Experts available now in Live!

Get 1:1 Help Now