Boost.Python:如何公开std :: unique_ptr

时间:2013-12-14 09:34:10

标签: python c++ c++11 unique-ptr boost-python

我对boost.python很新,并尝试将函数的返回值公开给python。

函数签名如下所示:

 std::unique_ptr<Message> someFunc(const std::string &str) const;

在python中调用函数时,我收到以下错误:

TypeError: No to_python (by-value) converter found for C++ type: std::unique_ptr<Message, std::default_delete<Message> >

我在python中的函数调用如下所示:

a = mymodule.MyClass()
a.someFunc("some string here") # error here

我试图暴露std :: unique_ptr,但只是无法让它工作.. 有人知道如何正确公开指针类吗? 谢谢!

修改 我尝试了以下方法:

class_<std::unique_ptr<Message, std::default_delete<Message>>, bost::noncopyable ("Message", init<>())

;

这个例子编译,但我仍然得到上面提到的错误。 另外,我试图公开课程Message本身

class_<Message>("Message", init<unsigned>())

        .def(init<unsigned, unsigned>()) 
        .def("f", &Message::f)
;

4 个答案:

答案 0 :(得分:15)

简而言之,Boost.Python不支持move-semantics,因此不支持std::unique_ptr。 Boost.Python的news/change log没有迹象表明它已经针对C ++ 11移动语义进行了更新。此外,unique_ptr std::auto_ptr支持feature request已超过一年没有被提及。

然而,Boost.Python支持通过unique_ptr将对象的独占所有权转移到Python或从Python转移。由于auto_ptr本质上是unique_ptr的更安全版本,因此使用auto_ptr将API调整为使用unique_ptr的API应该相当直接:

  • 当C ++将所有权转移到Python时,C ++函数必须:
  • 当Python将所有权转移到C ++时,C ++函数必须:
    • 通过manage_new_object接受该实例。 FAQ提到使用std::auto_ptr政策从C ++返回的指针将通过auto_ptr进行管理。
    • 通过unique_ptr
    • release()发布/// @brief Mockup Spam class. struct Spam; /// @brief Mockup factory for Spam. struct SpamFactory { /// @brief Create Spam instances. std::unique_ptr<Spam> make(const std::string&); /// @brief Delete Spam instances. void consume(std::unique_ptr<Spam>); }; 释放控制权

鉴于无法更改的API /库:

SpamFactory::make()

SpamFactory::consume()/// @brief Adapter a member function that returns a unique_ptr to /// a python function object that returns a raw pointer but /// explicitly passes ownership to Python. template <typename T, typename C, typename ...Args> boost::python::object adapt_unique(std::unique_ptr<T> (C::*fn)(Args...)) { return boost::python::make_function( [fn](C& self, Args... args) { return (self.*fn)(args...).release(); }, boost::python::return_value_policy<boost::python::manage_new_object>(), boost::mpl::vector<T*, C&, Args...>() ); } 需要通过辅助功能进行包装。

将所有权从C ++转移到Python的功能一般可以通过一个创建Python函数对象的函数来包装:

releases()

lambda委托原始函数,并将实例的所有权mpl::vector委托给Python,并且调用策略指示Python将取得lambda返回的值的所有权。 adapt_unique描述了Boost.Python的调用签名,允许它正确管理语言之间的函数调度。

SpamFactory.make()的结果公开为boost::python::class_<SpamFactory>(...) .def("make", adapt_unique(&SpamFactory::make)) // ... ;

SpamFactory::consume()

通常适应/// @brief Wrapper function for SpamFactory::consume_spam(). This /// is required because Boost.Python will pass a handle to the /// Spam instance as an auto_ptr that needs to be converted to /// convert to a unique_ptr. void SpamFactory_consume( SpamFactory& self, std::auto_ptr<Spam> ptr) // Note auto_ptr provided by Boost.Python. { return self.consume(std::unique_ptr<Spam>{ptr.release()}); } 更难,但写一个简单的辅助功能很容易:

auto_ptr

辅助函数委托给原始函数,并将Boost.Python提供的unique_ptr转换为API所需的SpamFactory_consumeSpamFactory.consume()辅助功能公开为boost::python::class_<SpamFactory>(...) // ... .def("consume", &SpamFactory_consume) ;

#include <iostream>
#include <memory>
#include <boost/python.hpp>

/// @brief Mockup Spam class.
struct Spam
{
  Spam(std::size_t x) : x(x) { std::cout << "Spam()" << std::endl; }
  ~Spam() { std::cout << "~Spam()" << std::endl; }
  Spam(const Spam&) = delete;
  Spam& operator=(const Spam&) = delete;
  std::size_t x;
};

/// @brief Mockup factor for Spam.
struct SpamFactory
{
  /// @brief Create Spam instances.
  std::unique_ptr<Spam> make(const std::string& str)
  {
    return std::unique_ptr<Spam>{new Spam{str.size()}};
  }

  /// @brief Delete Spam instances.
  void consume(std::unique_ptr<Spam>) {}
};

/// @brief Adapter a non-member function that returns a unique_ptr to
///        a python function object that returns a raw pointer but
///        explicitly passes ownership to Python.
template <typename T,
          typename ...Args>
boost::python::object adapt_unique(std::unique_ptr<T> (*fn)(Args...))
{
  return boost::python::make_function(
      [fn](Args... args) { return fn(args...).release(); },
      boost::python::return_value_policy<boost::python::manage_new_object>(),
      boost::mpl::vector<T*, Args...>()
    );
}

/// @brief Adapter a member function that returns a unique_ptr to
///        a python function object that returns a raw pointer but
///        explicitly passes ownership to Python.
template <typename T,
          typename C,
          typename ...Args>
boost::python::object adapt_unique(std::unique_ptr<T> (C::*fn)(Args...))
{
  return boost::python::make_function(
      [fn](C& self, Args... args) { return (self.*fn)(args...).release(); },
      boost::python::return_value_policy<boost::python::manage_new_object>(),
      boost::mpl::vector<T*, C&, Args...>()
    );
}

/// @brief Wrapper function for SpamFactory::consume().  This
///        is required because Boost.Python will pass a handle to the
///        Spam instance as an auto_ptr that needs to be converted to
///        convert to a unique_ptr.
void SpamFactory_consume(
  SpamFactory& self,
  std::auto_ptr<Spam> ptr) // Note auto_ptr provided by Boost.Python.
{
  return self.consume(std::unique_ptr<Spam>{ptr.release()});
}

BOOST_PYTHON_MODULE(example)
{
  namespace python = boost::python;
  python::class_<Spam, boost::noncopyable>(
      "Spam", python::init<std::size_t>())
    .def_readwrite("x", &Spam::x)
    ;

  python::class_<SpamFactory>("SpamFactory", python::init<>())
    .def("make", adapt_unique(&SpamFactory::make))
    .def("consume", &SpamFactory_consume)
    ;
}

这是一个完整的代码示例:

>>> import example
>>> factory = example.SpamFactory()
>>> spam = factory.make("a" * 21)
Spam()
>>> spam.x
21
>>> spam.x *= 2
>>> spam.x
42
>>> factory.consume(spam)
~Spam()
>>> spam.x = 100
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Boost.Python.ArgumentError: Python argument types in
    None.None(Spam, int)
did not match C++ signature:
    None(Spam {lvalue}, unsigned int)

Interactive Python:

{{1}}

答案 1 :(得分:3)

我的建议是从std::unique_ptr容器中获取get()的原始指针。您将不得不小心地将unique_ptr保留在您希望使用原始指针值的整个时间范围内,否则该对象将被删除,并且您将拥有指向无效内存区域的指针。 / p>

答案 2 :(得分:1)

Boost支持movable semanticsunique_ptr since v.1.55。 但在我的项目中,我使用了以前的版本并制作了这样简单的包装器:

class_<unique_ptr<HierarchyT>, noncopyable>(typpedName<LinksT>("hierarchy", false)
, "hierarchy holder")
    .def("__call__", &unique_ptr<HierarchyT>::get,
        return_internal_reference<>(),
        "get holding hierarchy")
    .def("reset", &unique_ptr<HierarchyT>::reset,
        "reset holding hierarhy")
    ;

以Python unique_ptr<HierarchyT>创建shierarchy并将其传递给通过引用接受它的函数。
Python代码:

hier = mc.shierarchy()
mc.clusterize(hier, nds)

其中C ++函数是float clusterize(unique_ptr<HierarchyT>& hier,...) 然后在Python中访问结果调用hier()来从unique_ptr获取包装对象:

output(hier(), nds)

答案 3 :(得分:-1)

我认为现在没有办法做你想要的......原因是因为std::unique_ptr<Message> someFunc(const std::string &str)按值返回,这意味着两件事之一:

  1. 将复制返回值(但unique_ptr is not copyable);
  2. 返回值将被移动(现在问题是boost :: python不提供移动语义的支持)。 (嘿,我正在使用boost 1,53,在最新版本中不确定);
  3. someFunc()是否正在创建对象?如果是,我认为解决方案是创建一个包装器,如果没有,你可以通过引用返回:

    std::unique_ptr<Message>& someFunc(const std::string &str)
    

    公开课程:

    class_<std::unique_ptr<Message, std::default_delete<Message>>, boost::noncopyable>("unique_ptr_message")
        .def("get", &std::unique_ptr<Message>::get, return_value_policy<reference_existing_object>())
    ;
    

    以及功能:

    def("someFunc", someFunc,  return_value_policy<reference_existing_object>());
    
相关问题