msl.examples.loadlib.cpp32 module
A wrapper around a 32-bit C++ library, cpp_lib32.
Example of a server that loads a 32-bit shared library, cpp_lib,
in a 32-bit Python interpreter to host the library. The corresponding cpp64
module
can be executed by a 64-bit Python interpreter and the Cpp64
class can send
a request to the Cpp32
class which calls the 32-bit library to execute the
request and then return the response from the library.
- class msl.examples.loadlib.cpp32.Cpp32(host, port, **kwargs)[source]
Bases:
Server32
A wrapper around the 32-bit C++ library, cpp_lib32.
This class demonstrates how to send/receive various data types to/from a 32-bit C++ library via
ctypes
.Note
Any class that is a subclass of
Server32
MUST provide two arguments in its constructor: host and port (in that order) and **kwargs. Otherwise theserver32
executable, seestart_server32
, cannot create an instance of theServer32
subclass.- add(a, b)[source]
Add two integers.
The corresponding C++ code is
int add(int a, int b) { return a + b; }
See the corresponding 64-bit
add()
method.
- subtract(a, b)[source]
Subtract two floating-point numbers (‘float’ refers to the C++ data type).
The corresponding C++ code is
float subtract(float a, float b) { return a - b; }
See the corresponding 64-bit
subtract()
method.
- add_or_subtract(a, b, do_addition)[source]
Add or subtract two double-precision numbers (‘double’ refers to the C++ data type).
The corresponding C++ code is
double add_or_subtract(double a, double b, bool do_addition) { if (do_addition) { return a + b; } else { return a - b; } }
See the corresponding 64-bit
add_or_subtract()
method.
- scalar_multiply(a, xin)[source]
Multiply each element in an array by a number.
The corresponding C++ code is
void scalar_multiply(double a, double* xin, int n, double* xout) { for (int i = 0; i < n; i++) { xout[i] = a * xin[i]; } }
See the corresponding 64-bit
scalar_multiply()
method.
- reverse_string_v1(original)[source]
Reverse a string (version 1).
In this method Python allocates the memory for the reversed string and passes the string to C++.
The corresponding C++ code is
void reverse_string_v1(const char* original, int n, char* reversed) { for (int i = 0; i < n; i++) { reversed[i] = original[n-i-1]; } }
See the corresponding 64-bit
reverse_string_v1()
method.
- reverse_string_v2(original)[source]
Reverse a string (version 2).
In this method C++ allocates the memory for the reversed string and passes the string to Python.
The corresponding C++ code is
char* reverse_string_v2(char* original, int n) { char* reversed = new char[n]; for (int i = 0; i < n; i++) { reversed[i] = original[n - i - 1]; } return reversed; }
See the corresponding 64-bit
reverse_string_v2()
method.
- distance_4_points(four_points)[source]
Calculates the total distance connecting 4
Point
’s.The corresponding C++ code is
double distance_4_points(FourPoints p) { double d = distance(p.points[0], p.points[3]); for (int i = 1; i < 4; i++) { d += distance(p.points[i], p.points[i-1]); } return d; }
See the corresponding 64-bit
distance_4_points()
method.- Parameters:
four_points (
FourPoints
) – The points to use to calculate the total distance.- Returns:
- circumference(radius, n)[source]
Estimates the circumference of a circle.
This method calls the
distance_n_points
function in cpp_lib32.See the corresponding 64-bit
circumference()
method.The corresponding C++ code uses the
NPoints
struct as the input parameter to sum the distance between adjacent points on the circle.double distance_n_points(NPoints p) { if (p.n < 2) { return 0.0; } double d = distance(p.points[0], p.points[p.n-1]); for (int i = 1; i < p.n; i++) { d += distance(p.points[i], p.points[i-1]); } return d; }
- class msl.examples.loadlib.cpp32.Point[source]
Bases:
Structure
C++ struct that is a fixed size in memory.
This object can be
pickle
’d.struct Point { double x; double y; };
- x
Structure/Union member
- y
Structure/Union member
- class msl.examples.loadlib.cpp32.FourPoints(point1, point2, point3, point4)[source]
Bases:
Structure
C++ struct that is a fixed size in memory.
This object can be
pickle
’d.struct FourPoints { Point points[4]; };
- Parameters:
- points
Structure/Union member
- class msl.examples.loadlib.cpp32.NPoints[source]
Bases:
Structure
C++ struct that is not a fixed size in memory.
This object cannot be
pickle
’d because it contains a pointer. A 32-bit process and a 64-bit process cannot share a pointer.struct NPoints { int n; Point *points; };
- n
Structure/Union member
- points
Structure/Union member