c++ - 在 boost python 中使用自定义智能指针

标签 c++ python boost smart-pointers boost-python

我想使用 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);
}

最佳答案

这是一个完整的示例。您几乎成功了 - 您必须从 boost::python 命名空间中删除 get_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);
}

您可以使用以下 python 代码对此进行测试:

import example
s = example.Session(27)
example.print_value_1(s)
example.print_value_2(s)

我们通过示例演示 boost.python 将根据需要正确运行转换。

关于c++ - 在 boost python 中使用自定义智能指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14355441/

相关文章:

c++ - QSettings(Qt 5.4) : setValue doesn't work properly

python - 将 Pandas 数据框转换为字典字典

c++ - 通过索引快速搜索并保持 C++ 中的插入顺序

python - 在 Python 中实现类似列表的索引访问

python - 如何在 Django 中记录所有外发电子邮件?

c++ - 指向动态分配的 boost multi_array 中的类的指针,而不是编译

c++ - Boost 序列化 Boost 进程间字符串

c++ - 从体素中提取尖锐的等值面

C++ 查找算法 : How do I find the last occurance of an element?

c++ - 在 C++ 中使用抽象库