Macro pretreatment

This is a strange problem, so I have to provide a little background. I have a C ++ project I'm working on and want to clean up a bit. The main problem I'm dealing with makes me want to prohibit the massive abuse of the preprocessor macros that are used in the main component of the project. There is a file with a bunch #definethat is commented / uncommented before compiling and using the program to switch the use of different algorithms. I would prefer to use command line arguments rather than recompiling every time we want to try a different algorithm. The problem is that there is so much in the whole code #ifdefthat it seems impossible to simply reorganize the code for each algorithm.

I was told that the reason for this is that it must be a real-time system that will deal with millisecond time units, and the code in this component is called so many times that ifchecking will negatively affect our performance. If you want to try a different algorithm, you must set the appropriate flags and recompile to optimize performance.

So my question for all of you is this:
<i>
Is there a way to get rid of these macros and instead use command line arguments without a serious hit on performance and without altering the logic and code?

One of the options that I considered was an attempt to compile versions of this component for each of the possible combinations of algorithms, and then select the version that will be determined by the combination of the provided command line arguments. But, according to my friend, the number of combinations is too large to be feasible. I myself have not worked out these numbers, but I will take his word for it, given how much work he has contributed to this code.

+3
source share
5 answers

it is assumed that this will be a real-time system that will deal with milliseconds of units of time

This is a real problem.

[...], if .

.

( ), . - , , ifs #defines ( ifs, , - , ).

- , , , , ( " - ": D)

?

.

( , if:

  • ( )

  • , #define s.

  • .

  • .

    :

    • ; ; , .
    • ( char* , ). - , , - . if switch.
    • . (const) .
  • , .

  • , 0, .

/:

  • ( - a jmp - ). , , .

  • ( , , ..)

  • ( , )

  • .

  • ( ).

  • ( ), / , .

+2

? , if , , .

+1

, , , CPP.

, , , . ++. - , , - , , , ,

DifferentialEquationIntegrator <:
     Runge-Kutta Integrator
     Eulers Method Integrator

.. ( <: "-" - A <: B , "A , B ".

, , , , "" , , , .

, . , . .

- , . , . , gcc -E . , ( wc -l ), , , (1) - .

, .

- , . PhD -.

+1

: .

(), ONCE, . , , Factory .

, .

, , - , -, . , - , , , . , - (, - ).

+1

, , - -. , ( ). ( ) "" (, ).

:

// type of my funcs:
typedef void (*SolverFunc)( const SolverParams &sp );

// implementation for the algorithms:
void EulerSolver( const SolverParams &sp ) { ... }

void RhungeSolver( const SolverParams &sp ) { ... }

// my array of solvers:
static SolverFunc s_solvers [] = { EulerSolver, RhungeSolver };

// parsing command line params:
int main( int argc, char** argv )
{
    int solverIndex = ParseIndex(argv);
    s_solvers[solverIndex] ( .. params .. );
    return 0;
}

, c-style, ++-, . , , =)

+1

Source: https://habr.com/ru/post/1754527/


All Articles