c++ - Define directive inside code to separate code versions -


i modifying code written else.

the existing code contains algorithms, valid 2 situations. differences minimal - data types , few lines in processing.

the original code sets

#define abc  #ifdef abc typedef struct {     unsigned char a, b, c; } mystruct; #else   typedef unsigned char mystruct; #endif   #if defined(abc) #define size1 3 #else   #define size1 2 #endif   typedef struct {     mystructtable[size1]; } mystructa;  void mymethod(){    statement1;    #if defined(abc)    staement2;    #endif } 

it works... if choice made inside source code, trying give user option use either 1 version or another, , having hardest time separating code.

i create complete set of files each version. problem have:

since there complex algorithms, make sense keep algorithms in single place... replication code may lead errors, if change made in 1 version , not other.

i have tried of type

if(option) {   #define abc }  else {   #undef abc } 

but debugger skips on , not see abc defined when going on "yes" branch.

is there way untangle code, or have create clear separate paths - duplicating entire code ?

i've done before. had library had no source code, did have header. there tons of code used library, too, way more change myself.

well had code library same thing other one, different device, driver etc. picture. @ runtime wanted substitute library theirs, still have ability use old (had backwards-compatible).

so did this: came few handy macros transform text other definitions. there 2 header files, , 1 source file. example, want substitute foolibrary barlibrary selectable @ runtime. in 1 header file called foolibrary.h:

// don't ask me why, 2 levels of indirection necessary // pull off concatenation // got question on #define cat(x, y) x ## y #define cat(x, y) cat(x, y)  #ifdef foo_hijack_namespace  // library init functions bool shouldhijack(); void sethijack(bool yn);  // handy-dandy function-changing macros #define foo_fn_name(x) cat(x, foo_hijack_namespace) #define foo_fn_name_ns(x) foo_hijack_namespace :: foo_fn_name(x)  #define foo_decl(x) foo_api foo_fn_name(x) #define foo_impl(x) foo_api foo_fn_name_ns(x) #define foo_call(x) (shouldhijack() ? foo_fn_name_ns(x) : x)  namespace foo_hijack_namespace { #else  #define foo_decl(x) x #define foo_impl(x) x #define foo_call(x) x  #endif // foo_hijack_namespace 

then, inside namespace wrapper re-define function calls so: there's function bool barfun(fooargs...) in other library, might redefine as

bool foo_decl(barfun)(fooargs...); // more foo_decl's... 

in foolibrary.h. foo_api usual cdecl-type junk, me doing #define foo_api winapi example. add similar declarations til end, , don't forget close namespace:

#ifdef foo_hijack_namespace } #endif // foo_hijack_namespace 

then, in foolibrary.cpp, provide implementation so:

#include "foolibrary.h"  // hijack static bool bshouldhijack = false; bool shouldhijack() { return bshouldhijack; } void sethijack(bool yn) { bshouldhijack = yn; }  bool foo_impl(barfun)(fooargs...) {     // implementation here... } // more foo_impl's... 

and on. finally, make 1 more header file call "foolibraryhijacked.h":

#include "foolibrary.h" #define barfun foo_call(barfun) // more #define's... 

provide 1 re-declaration, 1 re-implementation, , 1 #define each function. had 20 of these functions used regex generate them me.

finally, use all, in target code file, #include "foolibraryhijacked.h" last #include, or @ least after #include "barlibrary.h". #define macro foo_hijack_namespace something, "foo", , barfun becomes foo::barfunfoo after preprocessor gets hold of it.

now how separate type definitions interesting. perhaps use opaque pointers if application code passing variables 1 function another.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -