Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ident two functions

I would like to create something to use in Visual Studio (C++),maybe an extension, that will be used with two known functions inside. Let me try and explain. Let's say I have two functions start() and stop(). I usually use these functions like:

start();
 do something...
stop();

Some times it might become kind of confusing to know what is starting and what is stopping:

start();
 do something...
 start();
 do something...
 start();
 do something...
 stop();
 stop();
stop();

For me to understand what is starting and what is stopping I need to ident all the lines manually. I would like to create something like:

start(){
 do something....
 start(){
 do something ...
 }
}

Every time I close the start brackets the stop function will be close and the code will be much more organized.

Do you guys have any idea on how to accomplish this or something similar?

like image 346
Ricardo Mota Avatar asked Oct 23 '15 20:10

Ricardo Mota


1 Answers

In C++11 you can wright such code, but you need to understand high order functions and c++ lambdas.

#include <iostream>

class Executor
{
public:
  void perform(void (*routine)())
  {
    start();
    routine();
    stop();
  }

  void start() { std::cout << "start" << std::endl; }
  void stop() { std::cout << "stop" << std::endl; }
};

int main()
{
  Executor executor;
  executor.perform([](){
      std::cout << "perform 0" << std::endl;
    });
  executor.perform([](){
      std::cout << "perform 1" << std::endl;
      Executor executor;
      executor.perform([](){
          std::cout << "perform 10" << std::endl;
        });
    });
}
like image 124
Yury Avatar answered Oct 08 '22 05:10

Yury