refactoring - C++ : Factorize code without passing a million arguments -


i doing c/c++ programming (mostly c++) , find myself in need factorize code twice same, except every occurence of "left" replaced "right". once code finishes, need know if "left" or "right" version being executed, that's all, both return number out of can make sense (once combined left or right information).

in setup, every change needs done twice, , that's annoying.

so factorize replacing left/right "other" , call factorized function twice, knowing each time if calling "left" or "right".

now reach part of code, there million variables (cursors, ids, arrays being filled, etc ...). if wanted factorize left/right code, i'd need function have gazillion arguments, , that'd quite ugly.

i don't want overload c++ class attributes used in case.

any suggestions factorize smoothly here ?

    int arrayright[many], arrayleft[many], cursor;      while(1)     {          rightthing = arrayright[cursor];          // process rightthing assigned          // ...          // ...          // ...          leftthing = arrayleft[cursor]          // process rightthing assigned          // ...          // ...          // ...          cursor++;    } 

try this? (it works on c++14 since uses auto lambda)

auto func = [&](auto& thething){     // blah, blah code };  func(arrayright[cursor]); func(arrayleft [cursor]); 

[&] here means import variables in same scope lambda function.

for older c++ version, use following code ugly way (in school's project of c99).

int* pdata[2] = {arrayright, arrayleft}; (int i=0; i<2; i++) {     int* thething = pdata[i];     // blah, blah } 

some of friend told me macro can hold va_args here macro way. should work on gcc. msvc 2003, doesn't work though. (__typeof need replaced boost::typeof, , anonymous struct definition not supported in older version of msvc)

#define in(...) __va_args__ #define pp_arg_n( \           _1,  _2,  _3,  _4,  _5,  _6,  _7,  _8,  _9, _10, \          _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \          _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, \          _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, \          _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, \          _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, \          _61, _62, _63, n, ...) n #define pp_rseq_n()                                        \          63, 62, 61, 60,                                   \          59, 58, 57, 56, 55, 54, 53, 52, 51, 50,           \          49, 48, 47, 46, 45, 44, 43, 42, 41, 40,           \          39, 38, 37, 36, 35, 34, 33, 32, 31, 30,           \          29, 28, 27, 26, 25, 24, 23, 22, 21, 20,           \          19, 18, 17, 16, 15, 14, 13, 12, 11, 10,           \           9,  8,  7,  6,  5,  4,  3,  2,  1,  0 #define pp_narg_(...)    pp_arg_n(__va_args__) #define pp_narg(...)     pp_narg_(__va_args__, pp_rseq_n())  #define withinternal(datatype, desiredtype, x, datacnt, data...) for(struct {size_t __i; datatype __t[datacnt];} __s = {0, data}; x = __s.__t[__s.__i], __s.__i < datacnt; __s.__i++) #define with(x, ...) withinternal(__typeof(__va_args__), __typeof(__va_args__), x, pp_narg(__va_args__), __va_args__) #define withconst(x, ...) withinternal(__typeof(__va_args__), __typeof((__va_args__) + 0), x, pp_narg(__va_args__), __va_args__) #define withtype(tn, x, ...) withinternal(tn, tn, x, pp_narg(__va_args__), __va_args__) 

and when use it:

int main() {     int x;     int s1=2, s2=3;     with(x, in(s1, s2))         cout<<x<<endl;     withconst(x, in(45, 55))         cout<<x<<endl;     withtype(int, x, in(45, s1, 55, s2))         cout<<x<<endl;     return 0; } 

i believe more clear.

  • with works variables (basically) because when passing consts with, gcc __typeof generate automated const type x not assigned.
  • withconst uses x+0 trick remove const type of variables + operator doesn't work on every datatype has limitations.
  • withtype specifies data type, suitable mixed situation.

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -