Python bindings - int32 variable

The following example concerns a python object which represents an int32 variable (not a value!), which can for example be passed by pointer or reference to C++ functions whether for an in, out or in-out parameter.

In C/C++ the unary indirection operator (*) dereferences a pointer, converting a pointer value p to an l-value *p.

The equivalent in python is the expression p.val where p refers to a python object representing an int32 variable.

The following C++ defines reflected global functions which take an int32 by value, pointer, reference, const pointer and const reference:


// C++ code
namespace ns
{
	$function+ ceda::int32 Square1(ceda::int32 x) { return x*x; }
	$function+ ceda::int32 Square2(const ceda::int32& x) { return x*x; }
	$function+ ceda::int32 Square3(const ceda::int32* x) { return (*x)*(*x); }
	
	$function+ void Inc1(ceda::int32& x) { ++x; }
	$function+ void Inc2(ceda::int32* x) { ++(*x); }
}

The following Python code has a variable in the source code named x which references a python object which represents an int32 variable.


# python code

# This binds x to an int32 variable
x = ceda.int32(5)  
print 'x = ' + `x`  # prints the address of the int32 variable

# Read the value of the int32 variable
print 'x.val = ' + `x.val`
                
# Write the value of the int32 variable
x.val = 10

# Read the value of the int32 variable
print 'x.val = ' + `x.val`

# The int32 variable can be passed to functions that take an int32 variable by pointer or reference
ns.Inc1(x)
ns.Inc2(x)
print 'x.val = ' + `x.val`
                
# The int32 variable can be passed to functions that take an int32 by value, const pointer or const reference
# (i.e. without needing to use the x.val syntax because there is no ambiguity)
print 'Square1(x) = ' + `ns.Square1(x)`
print 'Square2(x) = ' + `ns.Square2(x)`
print 'Square3(x) = ' + `ns.Square3(x)`

which produces the following output:


x = 0x000002D406D40E90
x.val = 5
x.val = 10
x.val = 12
Square1(x) = 144
Square2(x) = 144
Square3(x) = 144