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)

Bases: Server32

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.

Parameters:
  • host (str) – The IP address (or hostname) to use for the server.

  • port (int) – The port to open for the server.

  • kwargs (str) – Optional keyword arguments. The keys and values are of type str.

add(a, b)

Add two integers.

The corresponding C++ code is

int add(int a, int b) {
    return a + b;
}

See the corresponding 64-bit add() method.

Parameters:
  • a (int) – First integer.

  • b (int) – Second integer.

Returns:

The sum of a and b.

Return type:

int

subtract(a, b)

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.

Parameters:
  • a (float) – First floating-point number.

  • b (float) – Second floating-point number.

Returns:

The difference between a and b.

Return type:

float

add_or_subtract(a, b, do_addition)

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.

Parameters:
  • a (float) – First double-precision number.

  • b (float) – Second double-precision number.

  • do_addition (bool) – Whether to add or subtract the numbers.

Returns:

a+b if do_addition is True else a-b.

Return type:

float

scalar_multiply(a, xin)

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.

Parameters:
Returns:

A new array with each element in xin multiplied by a.

Return type:

list[float]

reverse_string_v1(original)

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.

Parameters:

original (str) – The original string.

Returns:

The string reversed.

Return type:

str

reverse_string_v2(original)

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.

Parameters:

original (str) – The original string.

Returns:

The string reversed.

Return type:

str

distance_4_points(four_points)

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:

The total distance connecting the 4 points.

Return type:

float

circumference(radius, n)

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;
}
Parameters:
  • radius (float) – The radius of the circle.

  • n (int) – The number of points to use to estimate the circumference.

Returns:

The estimated circumference of the circle.

Return type:

float

class msl.examples.loadlib.cpp32.Point

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)

Bases: Structure

C++ struct that is a fixed size in memory.

This object can be pickle’d.

struct FourPoints {
    Point points[4];
};
Parameters:
  • point1 (Point) – The first point.

  • point2 (Point) – The second point.

  • point3 (Point) – The third point.

  • point4 (Point) – The fourth point.

points

Structure/Union member

class msl.examples.loadlib.cpp32.NPoints

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