C++ design for multiple versions of same interface (enumerations / structures in header files)

前提是你 提交于 2021-01-27 07:43:42

问题


We are interfacing with an externally controlled program with defined headers containing enumerations, and structures. We want to be able to interface with multiple versions of this program with as little duplication of code as possible. Each version has the same general enums and structures, but with slight modifications over time. In the ideal setup we could just conditionally include the different versions of the same header (i.e. if interfacing with version 1 #include "version1\progDefs.h", else #include "version2\progDefs.h"), but don't believe that is possible in C++.

Below is a simple example to illustrate the issue and what we are currently doing. Thank you for any assistance.

version1\progDefs.h contains

enum items
{  
  BOOK_TYPE = 0,
  PAGE_TYPE = 1,
  WORD_TYPE = 2
};
struct packet
{
  int type;
};

version2\progDefs.h contains

enum items
{  
  VOLUME_TYPE = 0,
  BOOK_TYPE = 1,
  PAGE_TYPE = 2,
  WORD_TYPE = 3
};
struct packet
{
  int type;
  char detail[80];
};

We now want to write out the packet structure to the network, being able to pick at runtime which version to use. Currently we have a base class and then one child class for each version. They look something like this

baseWriter.h

class baseWriter
{
    virtual void sendBookPacket() = 0;
};

version1Writer.cpp

#include "baseWriter.h"
#include "version1\progDefs.h"

class version1Writer : public baseWriter {};
void version1Writer::sendBookPacket()
{
  struct packet pkt;
  pkt.type = BOOK_TYPE;
  sendNetworkPacket((void*)&pkt, sizeof(pkt));
}

version2Writer.cpp

#include "baseWriter.h"
#include "version2\progDefs.h"

class version2Writer : public baseWriter {};
void version2Writer::sendBookPacket()
{
  struct packet pkt;
  pkt.type = BOOK_TYPE;
  sendNetworkPacket((void*)&pkt, sizeof(pkt));
}

This will work, but as you can see the code is the exact same in both classes. The only difference is in the values and structure used because of the different version of headers. In reality the project has many enumerations with hundreds of items, and many structures, none of which we have control over. They can change at any time, but our code doesn't need to as the new elements are not relevant to the program. However the changes do effect the values / size of the elements used. Is there any way to design our code to use these headers without duplicating code? Thank you!

Note: We do not have support for C++11. Thank you for the suggestions. Many are suggestions coding terms I'm unfamiliar with and will have to test to see if supported by compiler and will try and respond to each individually.


回答1:


You could wrap the contents of each progDefs.h file into a struct, preferably directly, or if you can't modify it, like this:

struct Version1 {
#include "version1\progDefs.h"
};

Then you can make baseWriter a template class:

template <typename Version>
class baseWriter
{
    using packet = typename Version::packet;
    using items = typename Version::items;

    void sendBookPacket() {
        packet pkt;
        pkt.type = items::BOOK_TYPE;
        sendNetworkPacket((void*)&pkt, sizeof(pkt));
    }
};

And then

using version1Writer = baseWriter<Version1>;
using version2Writer = baseWriter<Version2>;

Update:
If C++11 is not available, replace using X = Y in the code above to typedef Y X. That is, e.g

typedef typename Version::packet packet;

You can also do everything without these typedefs, they just simplify the code a bit.




回答2:


This seems like a good candidate for Visitor Pattern. The visitor pattern will allow you to choose which object to instantiate at runtime instead of compile time.




回答3:


I think you'll need to duplicate your code, like you did, if you want to be sure it works. You don't have to duplicate it by hand, you can generate the code automatically to save you the work and many potential bugs.



来源:https://stackoverflow.com/questions/30631469/c-design-for-multiple-versions-of-same-interface-enumerations-structures-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!