Click to See Complete Forum and Search --> : Saving RTTI in a Database


rliq
December 8th, 2002, 08:04 PM
I want to Serialise my polymorphic objects to a database. I am creating an XML string, which is passed as a parameter to a Stored Procedure. This all works fine.

Unfortunately, one of the fields in the XML is <TYPE>. Each polymorphic object has on const m_nType member with a unique value.

I retrieve the data through a RecordSet. As I traverse the RecordSet I check the <TYPE> field and switch(nType) to create the correct class. This means that if I add new types I have to change the switch(nType) statment, which is bad...

Has anyone managed to save the RTTI to a database?
If so how?
What was the type of the column?
Is the RTTI information valid for another instance of the program (ie I write, somebody else reads)?

Is there a better way to do it?

Also, this would help, as from the documentation, I just cannot work out how to... Get the RTTI of an object and create another object of the same class. If anyone could provide a short snippet of code it would help, I just cannot find an example.

Thank you
Rob.

TheCPUWizard
December 8th, 2002, 09:15 PM
Use of an Object Factory could allievate the switch issue.

Implementing a CreateNew() virtual function in the base class would allow you to create a new instance of a polymorphic type of the same type.

rliq
December 8th, 2002, 11:43 PM
I understand what you are saying but...I do not know the class type.

I understand how to make copies of polymorphic objects using a virtual CreateNew()/Clone(). Infact that is how I am doing it.

But I still need to pass the TYPE to the CreateNew(), because I do not have a copy of the object at hand, I am creating one from scratch.

Eg: if I already have a pPolyLocation, I can:

// CLocation is Base class
CLocation* pLocation = pPolyLocation->CreateNew();

and pLocation will point to a newly created object the same type as pPolyLocation points to. The problem is, I don't have pPolyLocation. I am loading the <TYPE> from a database or disk file.

// Snippet of my code currently
CLocation* pLocation = CLocation::CreateNew( nType );

I am responsible for saving the CLocations to database/disk though. So if there is a way I can save to database/disk so that I don't have to switch(TYPE) when I load them back it would be better (this is the bit I am after).
Something like the VC++ Serialize() method, but without using MFC.

Thanks
Rob.

TheCPUWizard
December 9th, 2002, 06:06 AM
Here are some more details....

If each type has a uniqueID, create a map collection with one instance of each possible type. Then when you are reading in the data, you do a find in the map and call the clone method. Completely portable!

See Scott Meyer's "Effective C++"/"More Effective C++" for full samples.

rliq
December 9th, 2002, 05:55 PM
Thank You. That is a great idea. It has also given me some new thoughts on other parts of the application I am writing.

I'll certainly check out those books. I do have one by Scott, but not those mentioned.

Once again. Thank you very much for your help, it is very much appreciated. I am a lonesome C++ programmer, but not so alone anymore.
Rob.