Narek
Narek

Reputation: 39871

Generate set/get methods for a c++ class

Is there any tool that generates set and get methods for a class automatically.

Just I create classes very frequently and would like to have a tool which for each class-member wil generate the following functions automatically:

Member_Type getMemberName() const; //in header file
Member_Type getMemberName() const //in source file 
{
    return member;
}

void setMemberName(const Member_Type & val); //in header
void setMemberName(const Member_Type & val) //in source file 
{
    member = val;
}

I have met a macro like this but did not like the idea:

#define GETSETVAR(type, name) \
private: \
    type name; \
public: \
    const type& get##name##() const { return name; } \
    void set##name##(const type& newval) { name = newval; }

May be someone knows how to do that with MS Visual Studio, or eny other tool?

Upvotes: 3

Views: 24624

Answers (5)

user18853
user18853

Reputation: 2837

I could not find a tool so I wrote a python script in 10 minutes. Its not perfect but its helpful:

generate_getters.py :

#!/usr/bin/python
import sys

if len(sys.argv) < 1:
    print 'usage: > python script.py <cpp_file_name>'

fileName = sys.argv[1]
className = fileName.split('.')[-2].split("/")[-1]

print 'classname:' +  className

def printGetterSettersForLine(line):
    syntax = line.strip().split(';')[0]
    words = syntax.split()
    if len(words) != 2:
        return
    getter = words[0] + ' ' + className + '::get' + words[1].title() + '() { \n'
    getter = getter + '     return ' + words[1] + ';\n'
    getter = getter + '}';
    print getter

with open(fileName) as f:
        lines = f.readlines()
        for line in lines:
            printGetterSettersForLine(line)

Usage: > python generate_getters.py ClassName.h Generating setter is left as exercise for reader. ;)

Upvotes: 0

Jason Williams
Jason Williams

Reputation: 57902

If you're using Visual Studio, my Atomineer Pro Documentation add-in will do this - it will instantly add getter/setter methods for a member field, using a naming style of your choice.

e.g. if you have this:

public:

   ...

private:
    int m_Speed;

Then you can execute the command to convert it to this

public:
    // Access the Speed
    int GetSpeed(void) const    { return(m_Speed);  };
    void SetSpeed(int speed)    { m_Speed = speed;  };

    ...

private:
    int m_Speed;

(Note: you don't have to use "m_" or "Get..." - this is just an example to show how it handles prefixed or suffixed naming schemes. You can configure the member naming style used (speed, _speed, mSpeed, m_speed, etc) and the naming style for the getter/setter methods (GetSpeed(), get_speed(), etc))


It does similar things when applied to a C# member:

protected int m_Speed;

then you can execute the command to convert it to an auto property:

protected int Speed { get; set; }

...and execute the command a second time to produce a property with a backing field:

protected int Speed
{
    get { return(m_Speed); }
    set { m_Speed = value; }
}
private int m_Speed;

Upvotes: 3

peterchen
peterchen

Reputation: 41096

Agree with Kotti - Visual Assist (and other addins) provide this functionality.

The actual source code should have the getters / setters, because you probably want to add validation and change notification to the setters as needed.

Using a macro for that is just... facepunchable. (willing to elaborate on request).

Upvotes: 2

Mark B
Mark B

Reputation: 96241

Why do you want to be generating these methods?

Generally a better approach is to make an actual use interface that defines operations on the object, not just sets of individual attributes. By creating a class interface, you avoid the need to write a bunch of getters and setters.

If you really need to generate public getters and setters for all (or even most) of your attributes, probably better is to just make it a struct, then no such generation is needed.

Upvotes: 4

M. Williams
M. Williams

Reputation: 4985

Not the tool actually, but you could use Encapsulate Method in Visual Assist X, for example, which makes getter / setter methods for some private class member.

Sure, many of tools that work similiar as VAX do have the same methods.

Also, if you have to do this action for a huge amount of classes, you could implement your own command-line tool and lauch it for every source file that you have.

Upvotes: 10

Related Questions