C++ 在boostpython中使用自定义智能指针
我想使用Boost::python在python中公开封装在自定义智能指针中的对象 警告C++ 在boostpython中使用自定义智能指针,c++,python,boost,smart-pointers,boost-python,C++,Python,Boost,Smart Pointers,Boost Python,我想使用Boost::python在python中公开封装在自定义智能指针中的对象 警告 自定义智能指针的现有用法过于普遍,无法使用 经济升级到boost智能指针 我想使用自动解引用技术,如几个地方所述 问题是我似乎不能完全正确地理解它。下面是一个示例代码: LegacyCode::Ptr->传统智能指针代码 LegacyCode::Session->封装在旧版智能指针中的旧版对象 namespace boost { namespace python { template <c
- 自定义智能指针的现有用法过于普遍,无法使用 经济升级到boost智能指针
- 我想使用自动解引用技术,如几个地方所述
namespace boost { namespace python
{
template <class T> T* get_pointer(LegacyCode::Ptr<T> const& p)
{
return p.get();
}
template <typename T>
struct pointee<LegacyCode::Ptr<T> >
{
typedef T type;
};
}}*
BOOST_PYTHON_MODULE(pyro)
{
using namespace boost::python;
class_<LegacyCode::Session,LegacyCode::Ptr<LegacyCode::Session>>("Session")
.def("get_type",&LegacyCode::Session::getType);
}
namespace boost{namespace python
{
模板T*get_指针(LegacyCode::Ptr const&p)
{
返回p.get();
}
模板
结构指针
{
T型;
};
}}*
BOOST_PYTHON_模块(pyro)
{
使用名称空间boost::python;
课堂(课时)
.def(“get_type”、&LegacyCode::Session::getType);
}
以下是一个完全有效的示例。您几乎已经拥有了它-您必须从boost::python
命名空间中删除get\u pointer()
#include <boost/python.hpp>
// dummy smart ptr class
template <typename T> class Ptr {
public:
typedef T element_type;
Ptr(): px(0) {}
Ptr(T* p): px(p) {}
// base operators
T* operator->() { return px; }
const T* operator->() const { return px; }
T& operator*() { return *px; }
const T& operator*() const { return *px; }
// getters
T* get() { return px; }
const T* get() const { return px; }
private:
T* px;
};
// a dummy class that will be held by your custom smart pointer
class Session {
public:
Session(int value) : value_(value) {}
virtual ~Session() {}
// a few methods to play with the class
int value() const { return value_; };
void value(int value) { value_ = value; }
private:
int value_;
};
// this emulates methods actually using your smart pointers
void print_value_1(const Ptr<Session>& s) {
std::cout << "[by const reference] The value of this session is " << s->value() << std::endl;
}
void print_value_2(Ptr<Session> s) {
std::cout << "[by value] The value of this session is " << s->value() << std::endl;
}
// here comes the magic
template <typename T> T* get_pointer(Ptr<T> const& p) {
//notice the const_cast<> at this point
//for some unknown reason, bp likes to have it like that
return const_cast<T*>(p.get());
}
// some boost.python plumbing is required as you already know
namespace boost { namespace python {
template <typename T> struct pointee<Ptr<T> > {
typedef T type;
};
} }
// now the module
BOOST_PYTHON_MODULE(example) {
using namespace boost::python;
class_<Session, Ptr<Session>, boost::noncopyable>("Session", init<int>());
def("print_value_1", &print_value_1);
def("print_value_2", &print_value_2);
}
我们通过示例演示了
boost.python
将根据需要正确运行转换。要回答未知原因,需要const_cast
是由于Ptr
的定义,而不是因为boost.python<当Ptr
本身为const
时,code>Ptr将const
应用于其元素。它相当于常量指针和指向常量指针之间的差异。例如,shared_ptr
在类型本身中做了这样的区分:const shared_ptr
和shared_ptr
。我目前有以下问题:CGAL库有一个基本上包装指针的类(称为句柄)。我想公开它,就好像它是一个普通的句柄指向什么。但不幸的是,句柄没有element_类型的变量,因此我总是会遇到编译错误。有什么提示吗?
import example
s = example.Session(27)
example.print_value_1(s)
example.print_value_2(s)