?
Solved

Polymorphic object serialization/creation?

Posted on 2000-03-08
5
Medium Priority
?
256 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 300 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

771 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