During the test, after large blocks I check whether the STOP button was pressed. There is a section with if

if (m_stopTest) { manualStopTest(); // Выключаем все //... return; // Выходим из теста } 

Is it possible to somehow properly formulate this section, for example, in a function in order to change what is invested in one place? For example, such thoughts

 if (checkStop(m_stopTest)) return; 

Where checkStop includes all those checks and calls. But then it is not logical that checkStop checks, and something else there turns it on, off.

  • Mouthly state somehow, despite the presence of the accepted answer. I rather thought that you need to transfer the function inside, and not the flag. - αλεχολυτ
  • @ αλεχολυτ I assumed that it was possible to do so, as in the accepted answer, and so far I did. But I thought maybe there are more well-established solutions. It is important for me not to repeat the same if block multiple times, but somehow wrap it up into something. But at the same time there must be a return from the parent function. - Dmitry Suvorov

2 answers 2

Do you want something like that?

 bool checkStop(bool m_stopTest) { if (m_stopTest) { manualStopTest(); return true; } return false; } 
  • Yes, but it just seems not logical to hide under the name checkStop other functions that are not related to the word check .. - Dmitry Suvorov
  • @DmitrySuvorov, call doStopIfNeeded() . - Fat-Zer
  • one
    @ DmitrySuvorov The choice of the name is yours :) The compiler will even accept fa6sd7tfgsd67sdagds76() ... - Harry

To solve your problem of "throwing" return to a higher function, I see at least three options:

  1. Use the predicate function and pass the appropriate parameter to it: a flag (as demonstrated by Harry in his answer) or even additionally a function to be performed, such as manualStopTest :

     using F = void (*)(); bool checkStop(bool stop, F f) { if (stop) { f(); return true; } return false; } 

    Using:

     if (checkStop(m_stopTest, &manualStopTest)) return; 

In this case, the return is passed along the chain: return true; inside checkStop results in a return from a top-level function.

  1. Use macro substitution:

     #define COND_RET(cond) \ if ((cond)) \ { \ manualStopTest(); \ return; \ } 

    Example:

     // какие-то действия ... COND_RET(m_stopTest) 

In this case, the return only one, then it is rather a C-style than C ++. It is better to avoid macros if there are suitable substitutions in the language. In this case, the inline implementation of the function from item 1 will look better.

  1. Throw an exception in the nested function and catch it somewhere higher:

     void checkStop(bool stop, F f) { if (stop) { f(); throw stopRequested(); } } 

    Using:

     // Код высшего уровня try { // Вызов кода верхнего уровня, т.е. функции, содержащей вызов checkStop } catch(...) { // Нужная обработка, может быть просто игнорирование } 

    I nevertheless would not advise such an option, since use exceptions for branching logic is not accepted. Both because of the superfluous overhead, and on the very ideology of the exceptions, which are designed to resolve truly exceptional situations.