Python 3 - Generating get and set for C++

Creating get and set can be a tough task.

Indeed if you have like dozen of new properties to add to new classes, it could take hours.

So what about generating them in few minutes with a script?

Let’s see that in this Python scripting tutorial for the C++ language.

First of all

Fell free to adapt the code for your own language such as Java, PHP or C# for example.

We’ll need to import the sys module in order to get access to the list of command line arguments passed to our Python script (argv).

Then we’ll construct the output from this following list of arguments:

  • Class name
  • Type name
  • Variable name

It means that if you have a class, for example BadprogSky, in wich you want to add get and set definitions of a variable, let’s say "plane" of type "BadprogPlane *", you’ll have to use:

  • Class name: "BadprogSky"
  • Type name: "BadprogPlane *"
  • Variable name: "plane"

Let's code a bit.

Code

Let’s add the following code into a file named "getAndSet.py":

getAndSet.py

import sys

# BadproG.com

###############################################
# Checks how many arguments from command line #
###############################################
if len(sys.argv) != 4 :
print("Usage: python getAndSet.py \"ClassName\" \"Type\" \"VariableName\"")
print("Example: python getAndSet.py \"MyClass\" \"Type1<Type2> *\" \"name\"")
sys.exit() # exits from program

#################################
# Makes arguments easier to use #
#################################

av1 = sys.argv[2] # type name
av2 = sys.argv[3] # variable name
av3 = sys.argv[1] # class name

############################
# Cleans the variable name #
############################

if "_" == av2[0] :
av2 = av2[1:] # removing "_" from variable if exists

##############
# FOR HEADER #
##############

# creating get

headerGetPart1 = av1 + " "
headerGetPart2 = "get"
headerGetPart3 = av2[0].capitalize() # first letter only with capitalize
headerGetPart3 += av2[1:] # second character until the end of string
headerGetPart4 = "() const"
headerGetPartAll = "\n//\n" + headerGetPart1 + headerGetPart2 + headerGetPart3 + headerGetPart4 + ";"

print(headerGetPartAll)

# creating set

headerSetPart1 = "void "
headerSetPart2 = "set"
headerSetPart3 = av2[0].capitalize() # first letter only with capitalize
headerSetPart3 += av2[1:] # second character until the end of string
headerSetPart4 = "(" + av1 + " " + av2 + ")"
headerSetPartAll = headerSetPart1 + headerSetPart2 + headerSetPart3 + headerSetPart4 + ";\n"

print(headerSetPartAll)

###########
# FOR CPP #
###########

# creating get
codeGetPart1 = "// ============================================================================="
codeGetPart1 += "\n// " + headerGetPart2 + headerGetPart3
codeGetPart1 += "\n// =============================================================================\n"
codeGetPart1 += headerGetPart1 + av3 + "::" + headerGetPart2 + headerGetPart3 + headerGetPart4 + " {"
codeGetPart2 = "\n\t"
codeGetPart2 += "return " + "_" + av2 + ";"
codeGetPart2 += "\n}\n"
codeGetPartAll = codeGetPart1 + codeGetPart2

print(codeGetPartAll)

# creating set
codeSetPart1 = "// ============================================================================="
codeSetPart1 += "\n// " + headerSetPart2 + headerSetPart3
codeSetPart1 += "\n// =============================================================================\n"
codeSetPart1 += "void " + av3 + "::" + headerSetPart2 + headerSetPart3 + headerSetPart4 + " {"
codeSetPart2 = "\n\t"
codeSetPart2 += "_" + av2 + " = " + av2 + ";"
codeSetPart2 += "\n}\n"
codeSetPartAll = codeSetPart1 + codeSetPart2

print(codeSetPartAll)

print("//\n" + av1 + "\t_" + av2 + ";")

 

Executing

Use either python or py as launcher:

$ py getAndSet.py "BadprogSky" "BadprogPlane *" "plane"

Result

// declaration for the header as public methods
BadprogPlane * getPlane() const;
void setPlane(BadprogPlane * plane);

// definitions for the .cpp file
// =============================================================================
// getPlane
// =============================================================================
BadprogPlane * BadprogSky::getPlane() const {
        return _plane;
}

// =============================================================================
// setPlane
// =============================================================================
void BadprogSky::setPlane(BadprogPlane * plane) {
        _plane = plane;
}

// declaration in the header as private member
BadprogPlane *  _plane;

Conclusion

Add the 2 declarations in your header as public methods and the _plane variable in your header as private member.

Then put the 2 definitions in your .cpp file and you are done.

Some hours saved with this Python script.

Good job, you did it.wink

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.