總之,Boost.Python不支持移動語義,因此不支持std::unique_ptr
。 Boost.Python的news/change log沒有跡象表明它已被更新爲C++ 11移動語義。此外,這一feature request對於unique_ptr
的支持在一年內還沒有被觸及。
不過,Boost.Python支持通過std::auto_ptr
將對象的獨佔所有權轉移到Python和從Python轉移。由於unique_ptr
本質上是auto_ptr
一個更安全的版本,它應該是相當直截了當地使用unique_ptr
的API適應使用auto_ptr
的API:
- 當C++所有權轉移到Python,C++函數必須:
- 當Python所有權轉移給C++,C++函數必須:
- 經由
auto_ptr
接受實例。 FAQ提到從C++返回的帶有manage_new_object
策略的指針將通過std::auto_ptr
進行管理。
- 具有經由
release()
給定一個API /庫不能被改變auto_ptr
釋放控制到一個unique_ptr
:
/// @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>);
};
的SpamFactory::make()
和SpamFactory::consume()
需要通過被包裹輔助功能。
函數從C++將所有權轉移給Python可以通過將創建的Python功能對象的功能被總包裹:
/// @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...>()
);
}
拉姆達委託給原有的功能,和實例的Python releases()
所有權,和調用策略表明Python將獲得從lambda返回的值的所有權。 mpl::vector
描述了對Boost.Python的調用簽名,允許它正確地管理語言之間的函數調度。
的adapt_unique
結果公開爲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()});
}
輔助功能委託給原始函數,並將Boost.Python提供的auto_ptr
轉換爲API所需的unique_ptr
。所述SpamFactory_consume
輔助功能被公開爲SpamFactory.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)
;
}
交互式Python:
>>> 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)
非常感謝您提供詳細的答案!我會盡快嘗試,但代碼看起來不錯! – schlimpf
那麼定義爲add_property的std :: unique_ptr呢?我是否必須將它封裝在類定義上,我在其中添加實際的屬性或定義to_python_converter會是一個更好的做法? –