Skip to content

Cpp64¤

Communicates with the cpp_lib library via the Cpp32 class that is running on a server.

Cpp64 ¤

Cpp64()

Bases: Client64

Communicates with a 32-bit C++ library.

This class demonstrates how to communicate with a 32-bit C++ library if an instance of this class is created within a 64-bit Python interpreter.

Source code in src/msl/examples/loadlib/cpp64.py
19
20
21
22
23
24
25
26
27
def __init__(self) -> None:
    """Communicates with a 32-bit C++ library via the server running [Cpp32][].

    This class demonstrates how to communicate with a 32-bit C++ library if an
    instance of this class is created within a 64-bit Python interpreter.
    """
    # specify the name of the corresponding 32-bit server module, cpp32,
    # which hosts the 32-bit C++ library -- cpp_lib32.
    super().__init__("cpp32", append_sys_path=Path(__file__).parent)

add ¤

add(a, b)

Add two integers.

See the corresponding Cpp32.add method.

Parameters:

Name Type Description Default
a int

First integer.

required
b int

Second integer.

required

Returns:

Type Description
int

The sum, a + b.

Source code in src/msl/examples/loadlib/cpp64.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def add(self, a: int, b: int) -> int:
    """Add two integers.

    See the corresponding [Cpp32.add][msl.examples.loadlib.cpp32.Cpp32.add] method.

    Args:
        a: First integer.
        b: Second integer.

    Returns:
        The sum, `a + b`.
    """
    reply: int = self.request32("add", a, b)
    return reply

add_or_subtract ¤

add_or_subtract(a, b, *, do_addition)

Add or subtract two floating-point numbers ('double' refers to the C++ data type).

See the corresponding Cpp32.add_or_subtract method.

Parameters:

Name Type Description Default
a float

First double-precision number.

required
b float

Second double-precision number.

required
do_addition bool

Whether to add or subtract the numbers.

required

Returns:

Type Description
float

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

Source code in src/msl/examples/loadlib/cpp64.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def add_or_subtract(self, a: float, b: float, *, do_addition: bool) -> float:
    """Add or subtract two floating-point numbers *('double' refers to the C++ data type)*.

    See the corresponding [Cpp32.add_or_subtract][msl.examples.loadlib.cpp32.Cpp32.add_or_subtract] method.

    Args:
        a: First double-precision number.
        b: Second double-precision number.
        do_addition: Whether to add or subtract the numbers.

    Returns:
        `a + b` if `do_addition` is `True` else `a - b`.
    """
    reply: float = self.request32("add_or_subtract", a, b, do_addition=do_addition)
    return reply

circumference ¤

circumference(radius, n)

Estimates the circumference of a circle.

This method calls the distance_n_points function in cpp_lib.

See the corresponding Cpp32.circumference method.

Parameters:

Name Type Description Default
radius float

The radius of the circle.

required
n int

The number of points to use to estimate the circumference.

required

Returns:

Type Description
float

The estimated circumference of the circle.

Source code in src/msl/examples/loadlib/cpp64.py
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
def circumference(self, radius: float, n: int) -> float:
    """Estimates the circumference of a circle.

    This method calls the `distance_n_points` function in [cpp_lib][cpp-lib].

    See the corresponding [Cpp32.circumference][msl.examples.loadlib.cpp32.Cpp32.circumference] method.

    Args:
        radius: The radius of the circle.
        n: The number of points to use to estimate the circumference.

    Returns:
        The estimated circumference of the circle.
    """
    reply: float = self.request32("circumference", radius, n)
    return reply

distance_4_points ¤

distance_4_points(points)

Calculates the total distance connecting 4 Points.

See the corresponding Cpp32.distance_4_points method.

Parameters:

Name Type Description Default
points FourPoints

The points to use to calculate the total distance. Since points is a struct that is a fixed size we can pass the ctypes.Structure object directly from 64-bit Python to the 32-bit Python. The ctypes module on the 32-bit server can load the pickled ctypes.Structure.

required

Returns:

Type Description
float

The total distance connecting the 4 points.

Source code in src/msl/examples/loadlib/cpp64.py
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
def distance_4_points(self, points: FourPoints) -> float:
    """Calculates the total distance connecting 4 [Point][msl.examples.loadlib.cpp32.Point]s.

    See the corresponding [Cpp32.distance_4_points][msl.examples.loadlib.cpp32.Cpp32.distance_4_points] method.

    Args:
        points: The points to use to calculate the total distance.
            Since `points` is a struct that is a fixed size we can pass the
            [ctypes.Structure][]{:target="_blank"} object directly from 64-bit Python to
            the 32-bit Python. The [ctypes][]{:target="_blank"} module on the 32-bit server
            can load the [pickle][]{:target="_blank"}d [ctypes.Structure][]{:target="_blank"}.

    Returns:
        The total distance connecting the 4 points.
    """
    reply: float = self.request32("distance_4_points", points)
    return reply

reverse_string_v1 ¤

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++.

See the corresponding Cpp32.reverse_string_v1 method.

Parameters:

Name Type Description Default
original str

The original string.

required

Returns:

Type Description
str

The string reversed.

Source code in src/msl/examples/loadlib/cpp64.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
def reverse_string_v1(self, original: str) -> str:
    """Reverse a string (version 1).

    In this method Python allocates the memory for the reversed string
    and passes the string to C++.

    See the corresponding [Cpp32.reverse_string_v1][msl.examples.loadlib.cpp32.Cpp32.reverse_string_v1] method.

    Args:
        original: The original string.

    Returns:
        The string reversed.
    """
    reply: str = self.request32("reverse_string_v1", original)
    return reply

reverse_string_v2 ¤

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.

See the corresponding Cpp32.reverse_string_v2 method.

Parameters:

Name Type Description Default
original str

The original string.

required

Returns:

Type Description
str

The string reversed.

Source code in src/msl/examples/loadlib/cpp64.py
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
def reverse_string_v2(self, original: str) -> str:
    """Reverse a string (version 2).

    In this method C++ allocates the memory for the reversed string and passes
    the string to Python.

    See the corresponding [Cpp32.reverse_string_v2][msl.examples.loadlib.cpp32.Cpp32.reverse_string_v2] method.

    Args:
        original: The original string.

    Returns:
        The string reversed.
    """
    reply: str = self.request32("reverse_string_v2", original)
    return reply

scalar_multiply ¤

scalar_multiply(a, xin)

Multiply each element in an array by a number.

See the corresponding Cpp32.scalar_multiply method.

Parameters:

Name Type Description Default
a float

Scalar value.

required
xin Sequence[float]

Array to modify.

required

Returns:

Type Description
list[float]

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

Source code in src/msl/examples/loadlib/cpp64.py
75
76
77
78
79
80
81
82
83
84
85
86
87
88
def scalar_multiply(self, a: float, xin: Sequence[float]) -> list[float]:
    """Multiply each element in an array by a number.

    See the corresponding [Cpp32.scalar_multiply][msl.examples.loadlib.cpp32.Cpp32.scalar_multiply] method.

    Args:
        a: Scalar value.
        xin: Array to modify.

    Returns:
        A new array with each element in `xin` multiplied by `a`.
    """
    reply: list[float] = self.request32("scalar_multiply", a, xin)
    return reply

subtract ¤

subtract(a, b)

Subtract two floating-point numbers ('float' refers to the C++ data type).

See the corresponding Cpp32.subtract method.

Parameters:

Name Type Description Default
a float

First floating-point number.

required
b float

Second floating-point number.

required

Returns:

Type Description
float

The difference, a - b.

Source code in src/msl/examples/loadlib/cpp64.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def subtract(self, a: float, b: float) -> float:
    """Subtract two floating-point numbers *('float' refers to the C++ data type)*.

    See the corresponding [Cpp32.subtract][msl.examples.loadlib.cpp32.Cpp32.subtract] method.

    Args:
        a: First floating-point number.
        b: Second floating-point number.

    Returns:
        The difference, `a - b`.
    """
    reply: float = self.request32("subtract", a, b)
    return reply