Make my C++ Class iterable via BOOST_FOREACH

前端 未结 3 439
失恋的感觉
失恋的感觉 2020-12-30 09:11

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 the

3条回答
  •  余生分开走
    2020-12-30 10:03

    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.

提交回复
热议问题