Automatically replace the assignment operator with the "set" function

I am porting my C ++ Windows project created using Embarcadero RAD Studio for Linux with Qt. Therefore, in my code there are many statements:

Menu->Enabled = true;

For Qt, it should be converted as follows:

Menu->setEnabled(true);

So now I am wasting a lot of time commenting on the code. Is there any way to make this replacement automatically?

+4
source share
4 answers

If this is a little porting information that interferes with the more important issues associated with this porting project, you can also consider deferring this refactoring to the end using an approach that looks something like this:

class Menu {

public:

      class punt_enabled {
          Menu *me;
      public:
          punt_enabled(Menu *meArg);
          Menu &operator=(bool flag);
      };

      punt_enabled Enabled;

 // ...

 };

 // ...

 Menu::Menu(...) : Enabled(this) //...

 Menu::punt_enabled::punt_enabled(Menu *meArg) : me(meArg) {}

 Menu &Menu::punt_enabled(bool flag) { me->setEnabled(flag); return *me; }

 // ...

" ", , grep + sed perl, .

P.S. punt_enabled , , .

+3

Linux MinGW Cygwin Windows Unix, , :

grep -rl "Menu->Enabled = true;" ./ | xargs sed -i "s/Menu->Enabled = true;/Menu->setEnabled(true);/g"

: ,

: , "- > Enabled = true" :

grep -rl -e "->Enabled = true;" ./ | xargs sed -i "s/->Enabled = true;/->setEnabled(true);/g"
+2

, OCD, . , :

, . , ( , , ).

-, , . , / . ​​, , , . , , . ? - , ? - setXXX, , . , .

-, git . , .

, , Source Insight . .

, , , Microsoft, .

- ( ). , : , =. - :

class C {
public:
   // int value;  -- OLD
   magic_int value;  // NEW
};

C magic , ++ " ", Java.

C::C (whatever-it-did-before)
: existing-stuff,
   value(this,&C::setter)  // for each newly magic field

, . , - :

int magic_int::operator= (int x)
{
(owner->*setfunc)(x);
}

, magic_int setfunc, . CRTP , :

template<typename OwnerT, typename ValueT>
class magic {
   friend class OwnerT;
   ValueT value;  // the real backing storage, if any.
   OwnerT* owner;
   void (OwnerT::setfunc(const ValueT&));
};

, - C, setter, , , , . , , .

, , , setfunc . ( ), = .

.

0

You can use the Qt Creator search and replace tools to automatically replace a specific template throughout the project. Press Ctrl+Shift+For select Edit > Find/Replace > Advanced Find > Open Advanced Find. Select a search area and press the button Search & Replace. Then you can specify the text to replace.

Another option is to click Ctrl+Shift+Ron the selected phrase and enter a replacement string.

0
source

All Articles