开发者

Make my C++ Class iterable via BOOST_FOREACH

I have a class which I want to expose a list of structs (which just contain some integers). I don't want the outside to modify these data, just iterate over it and read them Example:

struct TestData
{
  int x;
  int y;
  // ot开发者_StackOverflowher data as well
}

class IterableTest
{
  public:
    // expose TestData here
};

now in my code I want to use my class like this:

IterableTest test;
BOOST_FOREACH(const TestData& data, test.data())
{
  // do something with data
}

I've already read this article http://accu.org/index.php/journals/1527 about memberspaces. However, I don't want to (or can't) save all TestData in an internal vector or something. This is because the class itself doesn't own the storage, i.e. there is actually no underlying container which can be accessed directly by the class. The class itself can query an external component to get the next, previous or ith element, though.

So basically I want my class to behave as if it had a collection, but in fact it doesn't have one. Any ideas?


It sounds like you have to write your own iterators.

The Boost.Iterator library has a number of helpful templates. I've used their Iterator Facade base class a couple of times, and it's nice and easy to define your own iterators using it.

But even without it, iterators aren't rocket science. They just have to expose the right operators and typedefs. In your case, they're just going to be wrappers around the query function they have to call when they're incremented.

Once you have defined an iterator class, you just have to add begin() and end() member functions to your class.

It sounds like the basic idea is going to have to be to call your query function when the iterator is incremented, to get the next value. And dereference should then return the value retrieved from the last query call.

It may help to take a look at the standard library stream_iterators for some of the semantics, since they also have to work around some fishy "we don't really have a container, and we can't create iterators pointing anywhere other than at the current stream position" issues.

For example, assuming you need to call a query() function which returns NULL when you've reached the end of the sequence, creating an "end-iterator" is going to be tricky. But really, all you need is to define equality so that "iterators are equal if they both store NULL as their cached value". So initialize the "end" iterator with NULL.

It may help to look up the required semantics for input iterators, or if you're reading the documentation for Boost.Iterator, for single-pass iterators specifically. You probably won't be able to create multipass iterators. So look up exactly what behavior is required for a single-pass iterator, and stick to that.


If your collection type presents a standard container interface, you don't need to do anything to make BOOST_FOREACH work with your type. In other words, if your type has iterator and const_iterator nested typedefs, and begin() and end() member functions, BOOST_FOREACH already knows how to iterate over your type. No further action is required.

http://boost-sandbox.sourceforge.net/libs/foreach/doc/html/boost_foreach/extending_boost_foreach.html


From the Boost FOR_EACH documentation page:

BOOST_FOREACH iterates over sequences. But what qualifies as a sequence, exactly? Since BOOST_FOREACH is built on top of Boost.Range, it automatically supports those types which Boost.Range recognizes as sequences. Specifically, BOOST_FOREACH works with types that satisfy the Single Pass Range Concept. For example, we can use BOOST_FOREACH with:

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜