我有一个virtual class A
,subclass of A, TheA
和一个class B
。
TheA
指针cam1
在类B
的{{1}}函数内部初始化init()
的{{1}}将这个cam1
指针传递给python。boost.python
是一个the_a
python对象,但无法使用其功能。问题是:
如何使用
the_a
的功能?
PyTheA
the_a
#ifndef _A_HPP_
#define _A_HPP_
#include <iostream>
class A{
public:
virtual void capImage() = 0;
virtual int getWidth() = 0;
};
#endif //_A_HPP_
#ifndef _THE_A_
#define _THE_A_
#include "a.hpp"
class TheA : public A{
public:
TheA();
~TheA();
void capImage();
int getWidth();
};
#endif
#ifndef _B_HPP_
#define _B_HPP_
#include <iostream>
#include "a.hpp"
class B{
public:
A *cam1;
A *cam2;
public:
void init();
int getWidth() ;
A* getA();
};
#endif
#include <iostream>
#include "a.hpp"
boost python包装文件
#include "the_a.hpp"
#include <iostream>
using namespace std;
TheA::TheA(){
;
}
TheA::~TheA(){
;
}
void TheA::capImage(){
cout<<"the a"<<endl;
}
int TheA::getWidth(){
return 1280;
}
测试cpp代码的主要功能
#include "b.hpp"
#include "a.hpp"
#include "the_a.hpp"
void B::init(){
A *a = new TheA();
this->cam1 = new TheA();
}
int B::getWidth() {
return 1000;
}
A* B::getA(){
return this->cam1;
//or
// return this->(*cam1);
}
#include <iostream>
#include <boost/python.hpp>
#include <boost/python/wrapper.hpp>
#include <boost/python/object.hpp>
#include <boost/python/instance_holder.hpp>
#include "a.hpp"
#include "b.hpp"
#include "the_a.hpp"
using namespace std;
using namespace boost;
using namespace boost::python;
struct AWrapper: A, wrapper<A>{
void capImage(){
this->get_override("capImage")();
}
int getWidth(){
return this->get_override("getWidth")();
}
};
BOOST_PYTHON_MODULE(inner_member_pointer){
class_<AWrapper, boost::noncopyable>("PyA")
.def("capImage", pure_virtual(&A::capImage))
.def("getWidth", pure_virtual(&A::getWidth))
;
class_<TheA, boost::shared_ptr<TheA> >("PyTheA")
.def("capImage", &TheA::capImage)
.def("getWidth", &TheA::getWidth)
;
class_<B>("PyB")
.def("getWidth", &B::getWidth)
.def("init", &B::init)
.def("getA", &B::getA, return_value_policy<manage_new_object>())
;
}
python测试脚本
/*************************************************************************
> File Name: main.cpp
> Author: wangzheqie At: https://github.com/wangzheqie
> Mail: wangzheqie@qq.com
> Created Time: Thu 21 Jun 2018 08:06:34 AM CST
************************************************************************/
#include <iostream>
#include "a.hpp"
#include "b.hpp"
#include "the_a.hpp"
using namespace std;
int main(int argc, char** argv){
B b;
b.init();
cout<<b.getWidth()<<endl;
cout<<b.getA()<<endl;
TheA *the_a = (TheA*)b.getA();
the_a->capImage();
cout<<the_a->getWidth()<<endl;
return 0;
}
python输出结果
cmake_minimum_required(VERSION 2.8)
find_package(Boost 1.5 REQUIRED)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-cpp")
include_directories(
${PROJECT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/include
${Boost_INCLUDE_DIRS}
/usr/include/python2.7
)
link_directories(
/usr/lib/x86_64-linux-gnu
/usr/lib
/usr/local/lib
)
add_library(libs SHARED
src/a.cpp
src/b.cpp
src/the_a.cpp
)
add_library(inner_member_pointer SHARED inner_member_pointer.cpp)
target_link_libraries(inner_member_pointer libs libboost_python.so libpython2.7.so)
set_target_properties(inner_member_pointer PROPERTIES PREFIX "")
add_executable(main main.cpp)
target_link_libraries(main libs)
代码托管在:
https://github.com/wangzheqie/cpp_python_convert
目录#/usr/bin/env python2.7
# -*- coding: utf-8 -*-
from inner_member_pointer import PyB
from inner_member_pointer import PyTheA
from inner_member_pointer import PyA
b = PyB()
b.init() # if not init(), b.getA() is None
print(b.getWidth())
# new a PyTheA in python
a = PyTheA()
print(a)
print(type(a))
a.capImage()
print(a.getWidth())
# return a PyThA from c++
the_a = b.getA()
print(the_a)
print(type(the_a))
the_a.capImage() # error
print(the_a.getWidth()) # error