Functions and arguments

This example shows how to work with function arguments

Let’s consider the following c++ file:

#include <iostream>
using namespace std;

namespace ns{
    int myFunction(int a, const std::string& x1) {
        return a + 1;
    }
}

The following code can be used to find the different arguments of a function and do some basic operations on them:

from pygccxml import utils
from pygccxml import declarations
from pygccxml import parser

# Find out the c++ parser
generator_path, generator_name = utils.find_xml_generator()

# Configure the xml generator
xml_generator_config = parser.xml_generator_configuration_t(
    xml_generator_path=generator_path,
    xml_generator=generator_name)

# The c++ file we want to parse
filename = "example.hpp"

decls = parser.parse([filename], xml_generator_config)
global_namespace = declarations.get_global_namespace(decls)
ns = global_namespace.namespace("ns")

# Use the free_functions method to find our function
func = ns.free_function(name="myFunction")

# There are two arguments:
print(len(func.arguments))

# We can loop over them and print some information:
for arg in func.arguments:
    print(
        arg.name,
        str(arg.decl_type),
        declarations.is_std_string(arg.decl_type),
        declarations.is_reference(arg.decl_type))