当前位置: 代码网 > it编程>前端脚本>Python > python调用C++库实现数据类型转换的完整指南

python调用C++库实现数据类型转换的完整指南

2026年03月17日 Python 我要评论
枚举类型的封装假设有一个简单的枚举类型 color,可以直接将其暴露给 python。示例代码#include <pybind11/pybind11.h>namespace py = py

枚举类型的封装

假设有一个简单的枚举类型 color,可以直接将其暴露给 python。

示例代码 

#include <pybind11/pybind11.h>
namespace py = pybind11;
// 定义一个枚举类型
enum class color { red, green, blue };
pybind11_module(example, m) {
    // 绑定枚举类型到 python
    py::enum_<color>(m, "color")
        .value("red", color::red)
        .value("green", color::green)
        .value("blue", color::blue)
        .export_values();  // 使枚举值可以直接通过模块访问
}

在 python 中使用:

import example
print(example.color.red)  # 输出: color.red
print(int(example.color.green))  # 输出: 1

结构体的封装

接下来,看一个稍微复杂的例子,包括如何封装结构体和带有构造函数的结构体。

示例代码 

#include <pybind11/pybind11.h>
namespace py = pybind11;
// 定义一个简单的结构体
struct point {
    float x, y;
};
// 定义一个带有构造函数的结构体
struct coloredpoint : point {
    color color;  // 使用上面定义的枚举类型作为成员变量
    coloredpoint(float x, float y, color color): point{x, y}, color(color) {}
};
pybind11_module(example, m) {
    // 绑定枚举类型
    py::enum_<color>(m, "color")
        .value("red", color::red)
        .value("green", color::green)
        .value("blue", color::blue)
        .export_values();
    // 绑定简单结构体
    py::class_<point>(m, "point")
        .def(py::init<>())  // 默认构造函数
        .def_readwrite("x", &point::x)
        .def_readwrite("y", &point::y);
    // 绑定带有构造函数的结构体
    py::class_<coloredpoint, point /* 基类 */>(m, "coloredpoint")
        .def(py::init<float, float, color>())  // 自定义构造函数
        .def_readwrite("color", &coloredpoint::color);
}

在这个例子中:

  • 首先绑定了枚举类型 color
  • 然后定义了一个简单的结构体 point,并将其属性 x 和 y 暴露给 python。
  • 接着定义了一个继承自 point 的结构体 coloredpoint,它包含一个额外的 color 成员,并提供了一个自定义的构造函数。

在 python 中使用:

import example

# 创建一个 point 实例
p = example.point()
p.x = 1.0
p.y = 2.0
print(p.x, p.y)  # 输出: 1.0 2.0

# 创建一个 coloredpoint 实例
cp = example.coloredpoint(3.0, 4.0, example.color.blue)
print(cp.x, cp.y, cp.color)  # 输出: 3.0 4.0 color.blue

封装 c++ 容器类型

示例代码

#include <pybind11/pybind11.h>
#include <pybind11/stl.h> // 包含对 stl 容器的支持
namespace py = pybind11;
// 返回一个 vector<int>
std::vector<int> get_vector() {
    return {1, 2, 3, 4, 5};
}
// 接受一个 vector<int> 参数并返回它
std::vector<int> echo_vector(const std::vector<int>& vec) {
    return vec;
}
// 返回一个 map<string, int>
std::map<std::string, int> get_map() {
    return {{"one", 1}, {"two", 2}, {"three", 3}};
}
// 接受一个 map<string, int> 参数并返回它
std::map<std::string, int> echo_map(const std::map<std::string, int>& m) {
    return m;
}
pybind11_module(example, m) {
    m.def("get_vector", &get_vector, "get a vector of integers");
    m.def("echo_vector", &echo_vector, "echo a vector of integers");
    m.def("get_map", &get_map, "get a map of string to integer");
    m.def("echo_map", &echo_map, "echo a map of string to integer");
}

这里定义了四个函数:

  • get_vector: 返回一个 std::vector<int>。
  • echo_vector: 接收一个 std::vector<int> 参数,并简单地返回它。
  • get_map: 返回一个 std::map<std::string, int>。
  • echo_map: 接收一个 std::map<std::string, int> 参数,并简单地返回它。

【注意】:#include <pybind11/stl.h> 这行代码非常重要,它包含了对 c++ 标准模板库容器的支持。

在 python 中调用

一旦模块编译完成,就可以像导入任何其他 python 模块一样导入并使用它:

import example

# 获取并打印一个整数向量
vec = example.get_vector()
print(vec)  # 输出: [1, 2, 3, 4, 5]

# 回显向量
echoed_vec = example.echo_vector([6, 7, 8])
print(echoed_vec)  # 输出: [6, 7, 8]

# 获取并打印一个字符串到整数的映射
mapping = example.get_map()
print(mapping)  # 输出: {'one': 1, 'two': 2, 'three': 3}

# 回显映射
echoed_map = example.echo_map({"four": 4, "five": 5})
print(echoed_map)  # 输出: {'four': 4, 'five': 5}

cv::mat类型

pybind11进行  cv mat 与 numpy.ndarray 之间转换,示例代码

///typercaster.h 进行c++ opencv cv::mat与python numpy.ndarray 之间转换头文件
#include<opencv2/core/core.hpp>
#include<pybind11/pybind11.h>
#include<pybind11/numpy.h>
namespace pybind11 { 
namespace detail{
template<>
struct type_caster<cv::mat>{
public:   
    pybind11_type_caster(cv::mat, _("numpy.ndarray")); 
    //! 1. cast numpy.ndarray to cv::mat    
    bool load(handle obj, bool){        
        array b = reinterpret_borrow<array>(obj);        
        buffer_info info = b.request();    
        int nh = 1;        
        int nw = 1;        
        int nc = 1;        
        int ndims = info.ndim;        
        if(ndims == 2){           
           nh = info.shape[0];           
           nw = info.shape[1];       
        } 
        else if(ndims == 3){            
            nh = info.shape[0];           
            nw = info.shape[1];           
            nc = info.shape[2];        
        }else{            
            throw std::logic_error("only support 2d, 2d matrix");            
            return false;       
        }       
        int dtype;        
        if(info.format == format_descriptor<unsigned char>::format()){            
            dtype = cv_8uc(nc);        
        }else if (info.format == format_descriptor<int>::format()){            
            dtype = cv_32sc(nc);       
        }else if (info.format == format_descriptor<float>::format()){           
            dtype = cv_32fc(nc);        
        }else{            
            throw std::logic_error("unsupported type, only support uchar, int32, float"); 
            return false;
        }   
        value = cv::mat(nh, nw, dtype, info.ptr);
        return true;    
    }    
    //! 2. cast cv::mat to numpy.ndarray    
    static handle cast(const cv::mat& mat, return_value_policy, handle defval){        
        std::string format = format_descriptor<unsigned char>::format();
        size_t elemsize = sizeof(unsigned char);
        int nw = mat.cols;
        int nh = mat.rows;
        int nc = mat.channels();
        int depth = mat.depth();
        int type = mat.type();
        int dim = (depth == type)? 2 : 3;
        if(depth == cv_8u){
            format = format_descriptor<unsigned char>::format();
            elemsize = sizeof(unsigned char);
        }else if(depth == cv_32s){
            format = format_descriptor<int>::format();
            elemsize = sizeof(int);
        }else if(depth == cv_32f){
            format = format_descriptor<float>::format();
            elemsize = sizeof(float);
        }else{            
            throw std::logic_error("unsupport type, only support uchar, int32, float");
        }        
        std::vector<size_t> bufferdim;
        std::vector<size_t> strides;
        if (dim == 2) {
            bufferdim = {(size_t) nh, (size_t) nw};
            strides = {elemsize * (size_t) nw, elemsize};
        } else if (dim == 3) {
            bufferdim = {(size_t) nh, (size_t) nw, (size_t) nc};
            strides = {(size_t) elemsize * nw * nc, (size_t) elemsize * nc, (size_t) elemsize};
        }
        return array(buffer_info( mat.data,  elemsize,  format, dim, bufferdim, strides )).release();    
}};
}}//! end namespace pybind11::detail

需要使用cv::mat类型数据时,在使用pybind11封装的导出c++库头文件中,包含此typecaster.h头文件即可。

智能指针数据类型

需要定义结构体以及相关的操作函数:

#include <pybind11/pybind11.h>
#include <memory> // 包含智能指针
namespace py = pybind11;
// 定义一个简单的结构体
struct person {
    std::string name;
    int age;
};
// 返回一个包含person对象的shared_ptr
std::shared_ptr<person> create_person(const std::string& name, int age) {
    return std::make_shared<person>(person{name, age});
}
pybind11_module(example, m) {
    // 绑定person结构体到python,并指定使用std::shared_ptr进行管理
    py::class_<person, std::shared_ptr<person>> cls(m, "person");
    cls.def(py::init<const std::string&, int>()) // 绑定构造函数
       .def_readwrite("name", &person::name)
       .def_readwrite("age", &person::age);
    // 绑定create_person函数,它返回一个person对象的shared_ptr
    m.def("create_person", &create_person, "create a new person instance");
}

在这个例子中:

  • 定义了一个简单的结构体 person,它有两个成员变量:name 和 age。
  • create_person 函数创建并返回一个 person 对象的 std::shared_ptr。
  • 在绑定 person 结构体时,指定了使用 std::shared_ptr<person> 进行管理,这样 pybind11 就知道如何自动管理对象的生命周期。

在 python 中使用

一旦模块编译完成,就可以像导入任何其他 python 模块一样导入并使用它:

import example

# 创建一个新的 person 实例
person = example.create_person("alice", 30)
print(f"name: {person.name}, age: {person.age}")  # 输出: name: alice, age: 30

# 修改属性值
person.age = 31
print(f"updated age: {person.age}")  # 输出: updated age: 31

这个例子展示了如何将一个由 std::shared_ptr 管理的结构体从 c++ 暴露给 python。通过这种方式,可以利用 c++ 的资源管理和内存安全特性,同时在 python 中方便地使用这些数据结构。

注意,尽管这里使用的是 std::shared_ptr,pybind11 同样支持 std::unique_ptr,但通常不建议直接将 std::unique_ptr 返回给 python,因为它的所有权语义可能会导致复杂性增加。如果确实需要使用 std::unique_ptr,考虑采用工厂模式或其他方法来管理对象的生命周期。

智能指针+结构体+基类

#include <pybind11/pybind11.h>
#include <memory> // 包含智能指针
namespace py = pybind11;
// 定义基类
class base {
public:
    virtual ~base() = default; // 虚析构函数确保正确清理派生类对象
    virtual std::string say_hello() const { return "hello from base"; }
};
// 定义派生类
struct derived : public base {
    std::string name;
    int age;
    derived(const std::string& name, int age) : name(name), age(age) {}
    std::string say_hello() const override {
        return "hello from derived, my name is " + name + " and i'm " + std::to_string(age) + " years old.";
    }
};
// 返回一个包含derived对象的shared_ptr
std::shared_ptr<base> create_derived(const std::string& name, int age) {
    return std::make_shared<derived>(name, age);
}
pybind11_module(example, m) {
    // 绑定基类到python
    py::class_<base, std::shared_ptr<base>> base(m, "base");
    base.def(py::init<>())
         .def("say_hello", &base::say_hello);
    // 绑定派生类到python,同时指定它基于哪个基类
    py::class_<derived, std::shared_ptr<derived>, base> derived(m, "derived");
    derived.def(py::init<const std::string&, int>())
           .def_readwrite("name", &derived::name)
           .def_readwrite("age", &derived::age)
           .def("say_hello", &derived::say_hello);
    // 绑定创建derived实例的函数
    m.def("create_derived", &create_derived, "create a new derived instance");
}

在这个例子中:

  • 定义了一个基类 base,它有一个虚函数 say_hello()。
  • 定义了一个从 base 继承的结构体 derived,它有两个成员变量 name 和 age,并重写了 say_hello() 方法。
  • create_derived 函数创建并返回一个 derived 对象的 std::shared_ptr<base>,这允许在 c++ 中使用多态性,同时提供给 python 使用。
  • 在绑定类时,为 derived 类指定了它的基类 base,以及使用的智能指针类型 std::shared_ptr。

在 python 中使用

一旦模块编译完成,就可以像导入任何其他 python 模块一样导入并使用它:

import example

# 创建一个新的 derived 实例
obj = example.create_derived("alice", 30)
print(obj.say_hello())  # 输出: hello from derived, my name is alice and i'm 30 years old.

# 修改属性值
obj.age = 31
print(f"updated age: {obj.age}")  # 输出: updated age: 31

# 调用基类的方法
base_obj = example.base()
print(base_obj.say_hello())  # 输出: hello from base

到此这篇关于python调用c++库实现数据类型转换的完整指南的文章就介绍到这了,更多相关python调用c++实现数据类型转换内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com