opqit
opqit is a library that supplies a template class opqit::opaque_iterator<> that allows you to wrap any standard C++ iterator. This has two primary uses:
- it can significantly reduce compile-time header dependencies. Think of
opaque_iteratoras an implementation of the pimpl idiom for iterators - it can restrict the interface of existing iterator objects; perhaps you want to expose the iterators of a
std::vector<X>member object, but as forward iterators, rather than random access iterators.
Recent activity
Code
Clone the repository using mercurial:
> hg clone http://bitbucket.org/edd/opqit
Or get a zip file of the code.
Quick start
Your header:
#include <opqit/opaque_iterator_fwd.hpp> // a single header, no nested #includes
#include <string>
class todolist : public noncopyable
{
public:
typedef opqit::opaque_iterator<std::string, opqit::bidir> iterator;
todolist();
~todolist();
iterator begin();
iterator end();
// ...
private:
struct impl;
impl* pimpl_;
};
In your implementation:
#include "todolist.h"
#include <opqit/opaque_iterator.hpp> // the 'full' opqit header
#include <vector>
struct todolist::impl
{
std::vector<std::string> items;
};
todolist::todolist() : pimpl_(new impl) { }
todolist::~todolist() { delete pimpl_; }
todolist::iterator todolist::begin()
{
return pimpl_->items.begin();
}
todolist::iterator todolist::end()
{
return pimpl_->items.end();
}
Further reading
- The opqit wiki
- On the Tension Between Object-Oriented and Generic Programming in C++ by Thomas Becker
- Adobe's
any_iterator
Comments
[22/04/2012 at 19:00:55]
Thanks Chris, fixed now.
All original content copyright© Edd Dawson.
Any opinions expressed by Edd are his own and are not necessarily shared by his employer. Or by anyone else, in fact.
All source code appearing on this website that was written by Edd Dawson is made available under the terms of the Boost software license version 1.0 unless otherwise stated or implied by the license associated with the work from which the code is derived.

chrisvroberts
[22/04/2012 at 18:34:12]
Hello again Edd! Now that I have the answer...
In the example impl above,
todolist::end()should presumably callendrather thanbegin.