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.

Parameters:
  • host (str) – The IP address of the server.

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

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 the server32 executable, see start_server32, cannot create an instance of the Server32 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.

Parameters:
  • a (int) – The first integer.

  • b (int) – The second integer.

Returns:

int – The sum of a and b.

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.

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

  • b (float) – The second floating-point number.

Returns:

float – The difference between a and b.

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.

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

  • b (float) – The second double-precision number.

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

Returns:

float – Either a + b if do_addition is True else a - b.

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.

Parameters:
  • a (float) – The scalar value.

  • xin (list of float) – The array to modify.

Returns:

list of float – A new array with each element in xin multiplied by a.

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.

Parameters:

original (str) – The original string.

Returns:

str – The string reversed.

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.

Parameters:

original (str) – The original string.

Returns:

str – The string reversed.

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:

float – The total distance connecting the 4 Point’s.

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

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

Returns:

float – The estimated circumference of the circle.

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:
  • point1 (tuple of int) – The first point as an (x, y) ordered pair.

  • point2 (tuple of int) – The second point as an (x, y) ordered pair.

  • point3 (tuple of int) – The third point as an (x, y) ordered pair.

  • point4 (tuple of int) – The fourth point as an (x, y) ordered pair.

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