开发者

Create getter and setter members for a C++ class in Vim

I'm creating a class with many double (or something else, the type doesn't matter) members.

class CalcParameters {
public:
  CalcParameters() {}
  ~CalcParameters() {}

  // getters and setters here

private:
  double _param1;
  double _param2;
  ...
};

Is it possible, in Vim, to write a script to automatically generate public getters and setters for every parameter?

like

double param1() const { return _param1; }
void setParam1(double param1) { _param1 = param1; }
...

Note:

This code is not a snippet from real code but o开发者_C百科nly something I wrote to illustrate the question. On my real working code the members are named and setters have validation logic besides the attribution. But when writing a skeleton class with all members it would be nice to let Vim create the basic getters and setters for me. Is this possible?


For simple data classes, another nice option sometimes is to use a boost::tuple (or std::tuple in C++0x):

typedef tuple<int, char> MyDataElement;
MyDataElement data = make_tuple(1,'c');
int whatever = get<0>(data); // 1

Also, thinking it twice, you also may consider using Boost Property Map.

Finally, as for the VIM script, it certainly can be done. I've been investigating a little bit, and you can do something like this (assuming you identify member variables with a prefix, say m_):

:let bl = getbufline("" ,1, "$")

this will load all the lines of the buffer, then, filter them by those that contain something "m_":

:let xx = filter(bl, 'v:val =~ "m_.*"')

then you can investigate how to extract actual variable names from the array xx, and so on.


I wrote a ftplugin for this purpose a long time ago.

The idea is to type ;AA and to answer the questions Vim asks you. It has a primitive form of const-correctness understanding (i.e. an int would be copied while a string would be exchanged by const-ref)

I should rewrite it some day now that I know how to fetch the type of an already written data member, plus several functions aimed at converting identifiers (member <-> getter <-> setter <-> parameter <-> local variable <-> constant / underscore separated words <-> lower/upper camel cases). Alas I've been introduced to Demeter and I tend to minimize my use of getters and setters and hence my need to modernize the ftplugin.

EDIT: Since then, I've written two new refactoring methods for my lh-refactor plugin: Extract Setter and Extract Getter. As explained above, the extraction methods know how to build the names of the setter and the getter from the name of the member variable.


If this is really the way your class looks, store them in order in a vector<double> and provide a single getter by size_t index into the vector, along with a separate paramCount() method to measure the vector length using size().


Move the relevant data outside of the private section and into a public section. Nobody said all member data must be private: if it's part of the interface then it should be public, and the hell with those verbose, redundant, silly getters and setters.

In programming, if something can be done by a script then it shouldn't be done.


Yes. It is possible to use Vim in this way.

VimCasts has a relevant video which describes one of the simpler ways of automating processes like these, with nothing more than a macro, marks, and a little yanking and pasting.

But this is not how I would approach your problem. Instead, I would type :q! and start over. Almost invariably, the use of getters and setters in C++ is poor style, and inadvisable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜