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


we interfacing externally controlled program defined headers containing enumerations, , structures. want able interface multiple versions of program little duplication of code possible. each version has same general enums , structures, slight modifications on time. in ideal setup conditionally include different versions of same header (i.e. if interfacing version 1 #include "version1\progdefs.h", else #include "version2\progdefs.h"), don't believe possible in c++.

below simple example illustrate issue , doing. thank 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 want write out packet structure network, being able pick @ runtime version use. have base class , 1 child class each version. 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 work, can see code exact same in both classes. difference in values , structure used because of different version of headers. in reality project has many enumerations hundreds of items, , many structures, none of have control over. can change @ time, our code doesn't need new elements not relevant program. changes effect values / size of elements used. there way design our code use these headers without duplicating code? thank you!

note: not have support c++11. thank suggestions. many suggestions coding terms i'm unfamiliar , have test see if supported compiler , try , respond each individually.

you wrap contents of each progdefs.h file struct, preferably directly, or if can't modify it, this:

struct version1 { #include "version1\progdefs.h" }; 

then can make basewriter 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 not available, replace using x = y in code above typedef y x. is, e.g

typedef typename version::packet packet; 

you can without these typedefs, simplify code bit.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -