包装迭代句柄以在基于范围的for循环中使用
问题描述:
我使用的API带有使用 void * $ c>句柄的迭代功能。
I use an API that comes with an iteration functionality using a void*
handle.
void* handle = BrowseInit();
while (BrowseGetNext(handle))
{
// ...
int x = BrowseGetData(handle);
}
BrowseFree(handle);
我该如何将其包装到C ++ 11迭代器中以用于基于范围的-循环?由于句柄
的值实际上并没有改变,所以我需要 operator!=()
进行一些欺骗。 / p>
How would I go about wrapping this into a C++11 iterator for use in range-based for-loops? Since the value of the handle
doesn't actually change, I need some trickery in operator != ()
.
class Iterator
{
public:
friend class it;
class it
{
public:
it(Iterator* data) : _data(data) { }
bool operator != (const it& other) const
{
// what should I put in here?
}
const it& operator ++ ()
{
BrowseGetNext(_data->_handle);
}
int operator * () const
{
return BrowseGetData(_data->_handle);
}
private:
Iterator* _data;
};
Iterator() : _handle(BrowseInit()) { }
~Iterator()
{
BrowseFree(_handle);
}
it begin() const
{
return it(this);
}
it end() const
{
return it(nullptr);
}
private:
void* _handle;
};
答
这应该可行。
class Iterator
{
public:
friend class it;
class it
{
public:
// Constructor for begin().
it(Iterator* data) : _data(data), index_(0) { }
// Constructor for end().
it(Iterator* data, int) : _data(data), index_(-1) { }
bool operator != (const it& other) const
{
return !(*this == other);
}
bool operator == (const it& other) const
{
return ( this->_data == other._data && this->_index == rhs._index );
}
const it& operator ++ ()
{
// Increment the index if there's more data.
// Otherwise, set it to -1.
if ( BrowseGetNext(_data->_handle) )
{
++index_;
}
else
{
index_ = -1;
}
}
int operator * () const
{
return BrowseGetData(_data->_handle);
}
private:
Iterator* _data;
int _index;
};
Iterator() : _handle(BrowseInit()) { }
~Iterator()
{
BrowseFree(_handle);
}
it begin() const
{
return it(this);
}
it end() const
{
return it(this, 0);
}
private:
void* _handle;
};
更新:设置 iterator_traits
it
Update: Setup iterator_traits
for it
template <> std::iterator_traits<Iterator::it>
{
typedef int difference_type;
typedef int value_type;
typedef int* pointer;
typedef int& reference;
typedef std::forward_iterator_tag iterator_category;
};
感谢Yakk的建议。
更新2
不是将专用于 Iterator :: it
,从 std :: iterator
派生 Iterator :: it
class it : public std::iterator<std::forward_iterator_tag, int, int>
{
....
};