Thread: Design for Multi Version of a File.

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    42

    Design for Multi Version of a File.

    I'd like some advice on how to lay out a design in C++ that can handle multiple versions of a file.

    The file will contain enough information to identify version 5 from version 6 in a header. Most of the structures will be identical. However there may be the occasional oddity and certainly many new structures. In the newer versions.

    This is an existing program written in C. I'm considering porting this to C++ while I learn C++.
    The input for the files are complicated by marshaling. So its not a simple matter of reading a struct.

    I was originally just considering implementing separate libraries for this one for version5 and the next for version10.. But I'd like input on handling this internally using C++.

    Thanks,
    Ken

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If we assume that identifying the version is relatively simple, you could actually do something like this:
    Code:
    class baseversion
    {
    private:
        // Class members that are common to all versions for currently oldest supported version.    
    public:
        virtual int readData() = 0;
        virtual int writeData() const = 0;
    };
    
    class version5: public baseversion
    {
    private:
        // Add version5 onwards data members. 
    public:
       virtual int readData();
       virtual int writeData();
    };
    
    class version6: public version5
    {
    private:
        // Add version6 onwards data members. 
    public:
       virtual int readData();
       virtual int writeData();
    };
    Then you have a factory function that does something like this:
    Code:
    baseversion *factory()
    {
        int version = figureoutversion();
        baseversion *ptr;
        switch(version)
        {
           case 5:
              ptr = new version5;
              break;
           case 6:
              ptr = new version6;
              break;
           default:
              // Handle "unknown version" error. 
              break;
         }
    }
    This is of course just the principle. You'd need to write a fair bit more code, and you probably need some symbolic names and such for constants where I've used 5, 6 etc.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    42
    Thanks Mats,

    Thats exactly what I was looking too see..

    Each structure/Function has a corresponding on disk representation.

    The current code is in "C" so my original thought was to create an array of pointers. Then simply initialize based upon the versions:
    Code:
    {
    if (version == 5) {
       base[01] = func01_v5; 
       base[02] = func02_v5;
    } else if (version == 6) {
       base[01] = func01_v6; 
       base[02] = func02_v6;
    }
    ... 
    }
    However I like the C++ techniques since I can keep the same names and just add where needed. And not have to create duplicate header structures for each type.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM