Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在boostpython中使用自定义智能指针_C++_Python_Boost_Smart Pointers_Boost Python - Fatal编程技术网

C++ 在boostpython中使用自定义智能指针

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::python在python中公开封装在自定义智能指针中的对象

警告

  • 自定义智能指针的现有用法过于普遍,无法使用 经济升级到boost智能指针
  • 我想使用自动解引用技术,如几个地方所述
问题是我似乎不能完全正确地理解它。下面是一个示例代码:

LegacyCode::Ptr->传统智能指针代码

LegacyCode::Session->封装在旧版智能指针中的旧版对象

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)