0

I'm converting part of my code from JS to C++ thus I need to refactor direct instance variables assignment into setter methods:

a.xx=3; to a->setXx(3);

and also getter methods:

...a.xx... to ...a->getXx()...

Currently I use shelljs (node.js) for the conversions. Sample command for renaming a function from oldMethod() to newMethod():

sed('-i', /\boldMethod[ ]*\(/g, 'newMethod(', filename);

Preferably I would use the same technique fro the setters/getter. However I'm open to use other acceptable approach too.

My problems:

  1. I can not get the tagged substitutions working
  2. I'll need to capitalize the first letter
  3. I'm not sure how to set proper conditions for the getter methods. There is no clear expression like X=Y; I can't simply turn all alphanum.alphanum into getters because it would also replace incorrect expressions e.g. a("foo.doo"); My suggestions:

    a. do it anyway and manually update the incorrect replacements

    b. supply a set of valid variable names, e.g. "tag|possition|rotation|..."

PerfectGamesOnline.com
  • 1,774
  • 1
  • 18
  • 31

2 Answers2

3

In C++ we are less precious about using "getters" and "setters" as really they do little more than circumvent whilst appearing to respect encapsulation.

There's nothing wrong with using a->xx=3;, having made the xx member public. That will simplify your conversions.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

My solution is to enumerate all known methods. It's less flexible but it works.

var inf = process.argv[2];

sed('-i', /\b([a-zA-Z0-9_]+)\.tag[ ]*=[ ]*(.+);/g, '$1->setTag($2);', inf);
sed('-i', /\b([a-zA-Z0-9_]+)\.tag\b/g, '$1->getTag()', inf);

sed('-i', /\b([a-zA-Z0-9_]+)\.z[ ]*=[ ]*(.+);/g, '$1->setZ($2);', inf);
sed('-i', /\b([a-zA-Z0-9_]+)\.z\b/g, '$1->getZ()', inf);

sed('-i', /\b([a-zA-Z0-9_]+)\.pos[ ]*=[ ]*(.+);/g, '$1->setPos($2);', inf);
sed('-i', /\b([a-zA-Z0-9_]+)\.pos\b/g, '$1->getPos()', inf);
PerfectGamesOnline.com
  • 1,774
  • 1
  • 18
  • 31