我有一个类Type
,不能被复制,也不它包含默认构造函数。 我有第二类A
充当一组以上的类。 这第二类通过迭代器可以访问我的迭代器引用操作:
class A {
class iterator {
[...]
public:
Type & operator*()
{
return instance;
}
private:
Type instance;
}
[...]
};
现在揭露,我写了一个boost::python
代码看起来像这样:
class_<A>("A", [...])
.def("__iter__", iterator<A, return_internal_reference<> >())
.def("__len__", container_length_no_diff<A, A::iterator>)
;
添加打印消息发送给所有的迭代器操作(建设,分配,指针引用,破坏)代码的Python这样的后:
for o in AInstance:
print o.key
我得到的输出(修剪到重要组成部分):
construct 0xffffffff7fffd3e8
dereference: 0xffffffff7fffd3e8
destroy 0xffffffff7fffd3e8
get key 0xffffffff7fffd3e8
在上面的代码中的那些地址是仅仅地址instance
构件(或this
在方法调用)。 前三行所生产的iterator
,所述第四线是通过在吸气法印刷Type
。 所以,在某种程度上boost::python
包一切以这样一种方式,即:
- 创建迭代器
- 取消引用迭代器和存储基准
- 破坏迭代器(和对象包含)
- 使用在步骤2获得的参考
所以很明显return_internal_reference
不会表现得像说明(请注意,实际上它只是在typedef的with_custodian_and_ward_postcall<>
它应该保持的对象,只要方法调用的结果被引用。
所以我的问题是我怎么暴露这样一个迭代器,用Python boost::python
?
编辑:
正如有人指出,它可能不是很清楚:原来的容器中不包含类型的对象Type
。 它包含了一些BaseType
从中我能构建/修改对象Type
的对象。 所以iterator
在上面的例子中的作用就像transform_iterator
。
如果A
是拥有的情况下,一个容器Type
,然后再考虑其A::iterator
包含一个手柄Type
而不是具有的Type
:
class iterator {
[...]
private:
Type* instance; // has a handle to a Type instance.
};
代替:
class iterator {
[...]
private:
Type instance; // has a Type instance.
};
在Python,一个迭代将包含在其上可以迭代容器的引用。 这将延长一个迭代对象的寿命,并防止可迭代对象被迭代期间垃圾收集。
>>> from sys import getrefcount
>>> x = [1,2,3]
>>> getrefcount(x)
2 # One for 'x' and one for the argument within the getrefcount function.
>>> iter = x.__iter__()
>>> getrefcount(x)
3 # One more, as iter contains a reference to 'x'.
boost::python
支持这种行为。 下面是一个例子程序中,用Foo
是一个简单的类型不能被复制; FooContainer
是一个可迭代容器; 和FooContainer::iterator
是一个迭代器:
#include <boost/python.hpp>
#include <iterator>
// Simple example type.
class Foo
{
public:
Foo() { std::cout << "Foo constructed: " << this << std::endl; }
~Foo() { std::cout << "Foo destroyed: " << this << std::endl; }
void set_x( int x ) { x_ = x; }
int get_x() { return x_; }
private:
Foo( const Foo& ); // Prevent copy.
Foo& operator=( const Foo& ); // Prevent assignment.
private:
int x_;
};
// Container for Foo objects.
class FooContainer
{
private:
enum { ARRAY_SIZE = 3 };
public:
// Default constructor.
FooContainer()
{
std::cout << "FooContainer constructed: " << this << std::endl;
for ( int i = 0; i < ARRAY_SIZE; ++i )
{
foos_[ i ].set_x( ( i + 1 ) * 10 );
}
}
~FooContainer()
{
std::cout << "FooContainer destroyed: " << this << std::endl;
}
// Iterator for Foo types.
class iterator
: public std::iterator< std::forward_iterator_tag, Foo >
{
public:
// Constructors.
iterator() : foo_( 0 ) {} // Default (empty).
iterator( const iterator& rhs ) : foo_( rhs.foo_ ) {} // Copy.
explicit iterator(Foo* foo) : foo_( foo ) {} // With position.
// Dereference.
Foo& operator*() { return *foo_; }
// Pre-increment
iterator& operator++() { ++foo_; return *this; }
// Post-increment.
iterator operator++( int )
{
iterator tmp( foo_ );
operator++();
return tmp;
}
// Comparison.
bool operator==( const iterator& rhs ) { return foo_ == rhs.foo_; }
bool operator!=( const iterator& rhs )
{
return !this->operator==( rhs );
}
private:
Foo* foo_; // Contain a handle to foo; FooContainer owns Foo.
};
// begin() and end() are requirements for the boost::python's
// iterator< container > spec.
iterator begin() { return iterator( foos_ ); }
iterator end() { return iterator( foos_ + ARRAY_SIZE ); }
private:
FooContainer( const FooContainer& ); // Prevent copy.
FooContainer& operator=( const FooContainer& ); // Prevent assignment.
private:
Foo foos_[ ARRAY_SIZE ];
};
BOOST_PYTHON_MODULE(iterator_example)
{
using namespace boost::python;
class_< Foo, boost::noncopyable >( "Foo" )
.def( "get_x", &Foo::get_x )
;
class_< FooContainer, boost::noncopyable >( "FooContainer" )
.def("__iter__", iterator< FooContainer, return_internal_reference<> >())
;
}
下面是示例输出:
>>> from iterator_example import FooContainer
>>> fc = FooContainer()
Foo constructed: 0x8a78f88
Foo constructed: 0x8a78f8c
Foo constructed: 0x8a78f90
FooContainer constructed: 0x8a78f88
>>> for foo in fc:
... print foo.get_x()
...
10
20
30
>>> fc = foo = None
FooContainer destroyed: 0x8a78f88
Foo destroyed: 0x8a78f90
Foo destroyed: 0x8a78f8c
Foo destroyed: 0x8a78f88
>>>
>>> fc = FooContainer()
Foo constructed: 0x8a7ab48
Foo constructed: 0x8a7ab4c
Foo constructed: 0x8a7ab50
FooContainer constructed: 0x8a7ab48
>>> iter = fc.__iter__()
>>> fc = None
>>> iter.next().get_x()
10
>>> iter.next().get_x()
20
>>> iter = None
FooContainer destroyed: 0x8a7ab48
Foo destroyed: 0x8a7ab50
Foo destroyed: 0x8a7ab4c
Foo destroyed: 0x8a7ab48
我认为整个问题是,我没有完全理解什么语义应该iterator
类提供。 似乎通过迭代器返回的值必须是有效的,只要容器存在,没有迭代器。
这意味着, boost::python
正确的行为,并有两个办法,解决:
- 使用
boost::shared_ptr
- 通过返回值
少了几分有效的方法比我试图这样做,但貌似没有别的办法。
编辑:我已经研究出了解决办法(不仅是可能的,但它似乎很好地工作): 升压蟒蛇容器,迭代器和项目寿命
下面是相关示例: https://wiki.python.org/moin/boost.python/iterator 。
您可以通过常量/非const 引用 返回迭代器值 :
...
.def("__iter__"
, range<return_value_policy<copy_non_const_reference> >(
&my_sequence<heavy>::begin
, &my_sequence<heavy>::end))
这个想法是,正如你所说,我们应该绑定到容器的寿命,而不是返回值迭代器的寿命。