This project demonstrates the integration of C++ code with Python, combining Python's simplicity with the high performance of C++. The study evaluates multiple tools and techniques to achieve seamless interoperability between the two languages.
Four tools were analyzed for their ability to expose C++ code to Python:
- SWIG
- pybind11
- cppyy
- Boost.Python
The project tests each tool’s capability to handle advanced C++ features:
- Inheritance
- Smart Pointers
- Raw Pointers
All tools successfully integrated C++ with Python while supporting the tested advanced C++ features. The project leverages CMake
to build Python libraries for each tool, ensuring a modular and maintainable structure.
Note: This project has been tested on Linux. All commands provided in the documentation are specific to Linux environments.
The repository is organized as follows:
-
shared/
: Contains the C++ code. This directory is further divided into:include/
: Header files for the C++ code.src/
: Source files for the C++ code.
-
libs/
: Directory whereCMake
outputs the built Python libraries for each tool. -
tests/
: Contains Python scripts executed viapytest
to validate the functionality of the libraries. The Python tests interact with the C++ code integrated via the chosen tools. Tests accept a--tool-name
argument to specify the integration tool being tested.
Before building any tool, ensure it is installed on your system. Installation instructions are not provided here as they vary by operating system. Refer to the official documentation for guidance:
The C++ code is broken down into two main components:
-
Inheritance and Smart Pointers:
- Defines classes for geometric shapes (
Circle
andRectangle
) and their management through aShapeManager
. Here's how it's organized:- Base Class
Shape
: Acts as an abstract base class with a pure virtualarea()
method, demonstrating inheritance. - Derived Classes:
Circle
: Inherits fromShape
, implementsarea()
to compute the area of a circle using the formula πr².Rectangle
: Also inherits fromShape
, implementingarea()
for rectangle area calculation (length × width).
ShapeManager
:- Utilizes smart pointers (
std::shared_ptr<Shape>
) for managing a collection of shapes. This ensures automatic memory management, preventing memory leaks by leveraging the shared ownership model ofstd::shared_ptr
. - The class provides methods to add shapes, calculate the total area of all managed shapes, and other utility functions for shape manipulation.
- Utilizes smart pointers (
- Base Class
- Defines classes for geometric shapes (
-
Raw Pointers:
DataStreamExample
:- This class illustrates the use of raw pointers for memory management. It initializes a
uint32_t*
buffer with values based on their index, provides methods for getting the buffer pointer, getting the buffer size and printing the buffer to the console.
- This class illustrates the use of raw pointers for memory management. It initializes a
We've included comprehensive test cases to ensure both the C++ functionality and its integration with Python are functioning correctly:
-
test_shape_manager
:- Checks that
ShapeManager
can manage bothCircle
andRectangle
instances correctly, including accurate area calculations for individual shapes and the sum of all shapes.
- Checks that
-
test_data_stream_init
:- Ensures that the
DataStreamExample
class initializes its buffer correctly, where each element should beindex * 10
.
- Ensures that the
-
test_data_stream_modification
:- Confirms that changes made to the buffer (
uint32_t*
) withinDataStreamExample
are correctly reflected when the buffer is subsequently accessed, validating the integrity of memory operations.
- Confirms that changes made to the buffer (
These tests are crucial for verifying the seamless interaction between C++ and Python.
To build the Python library for a specific tool:
cd <tool_folder>
mkdir build
cd build
cmake ..
make
The generated library will be placed in the libs/
directory.
Automated tests ensure the correctness of the integrations. Tests are executed using pytest
and can target a specific integration tool. For example:
python3 -m pytest -v --tb=short tests/main.py --tool-name=boost_integration
This example command runs all tests using the library built with Boost.Python (from /libs/boost_integration
).