CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Saving RTTI in a Database

    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.

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    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.

  3. #3
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686
    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:
    Code:
    // 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.
    Code:
    // 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.

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    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.

  5. #5
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured