Boost.python3 C++ overloads 函数和类成员函数重载

参考官方文档

https://www.boost.org/doc/libs/1_72_0/libs/python/doc/html/tutorial/tutorial/functions.html#tutorial.functions.overloading

 

1. test.cpp

#include <boost/python.hpp>
#include <set>
#include <map>
#include <memory>
#include <iostream>
#include <chrono>
#include <stdlib.h>
#include <math.h>
#include <cstdio>
#include <boost/python/numpy.hpp>
#include <boost/python/overloads.hpp>
#include <opencv2/opencv.hpp>
#include <list>
#include <pthread.h>
 
 
namespace np = boost::python::numpy;
namespace py = boost::python;
using namespace boost::python;
using namespace std;
using namespace cv;
 
py::tuple test()
{
    return py::make_tuple(123,"hello");
}

py::tuple test(int num)
{
    return py::make_tuple(num,"hello");
}

class Test2
{
    public:
        int init();
        int init(int num);
};

int Test2::init()
{
    return -1;
}

int Test2::init(int num)
{
    return num;
}
 
BOOST_PYTHON_MODULE(test_overloads)
{
    py::tuple    (*o1)()        = test;
    py::tuple    (*o2)(int)     = test;

    int (Test2::*init1)() = &Test2::init;
    int (Test2::*init2)(int) = &Test2::init;

    def("test", o1);
    def("test", o2);

    class_<Test2>("Test2",init<>())
            .def("init", init1)
            .def("init", init2);
 
}

2. CMakeLists.txt

cmake_minimum_required(VERSION 3.6.0 FATAL_ERROR)
 
project(rtmp_streaming VERSION 1.0.0 DESCRIPTION "boost.python overlads test")
 
 
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
 
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -std=c++14 -Wall")
 
set ( CMAKE_EXE_LINKER_FLAGS_DEBUG  "${CMAKE_EXE_LINKER_FLAGS_DEBUG}  ")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}  -std=c++14 -Wall -O0 -fno-omit-frame-pointer")
 
find_package(PythonInterp 3.6 REQUIRED)
find_package(PythonLibs 3.6 REQUIRED)
 
set(Python_ROOT_DIR "")
 
 
set(BOOST_PYTHONLIB python3)
 
find_package(Boost COMPONENTS ${BOOST_PYTHONLIB})
find_package(OpenCV  REQUIRED)
 
 
include(CMakeToolsHelpers OPTIONAL)
 
 
include_directories(${PYTHON_INCLUDE_DIRS})
include_directories(${OpenCV_INCLUDE_DIRS})
 
link_directories(${Boost_LIBRARY_DIR})
 
 
 
 
#SET_TARGET_PROPERTIES(test_overloads PROPERTIES PREFIX "")
add_library(test_overloads SHARED test.cpp)
 
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR})
 
target_link_libraries(test_overloads  ${OpenCV_LIBS} ${Boost_LIBRARIES} boost_numpy3 /usr/lib/x86_64-linux-gnu/libpython3.6m.so)

3. test_overloads.py

from test_overloads import test,Test2


print(test())
print(test(444))

t = Test2()
print(t.init())
print(t.init(666))

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值