:: horsing around with the C++ programming language


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:

  1. it can significantly reduce compile-time header dependencies. Think of opaque_iterator as an implementation of the pimpl idiom for iterators
  2. 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


Clone the repository using mercurial:

> hg clone

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
        typedef opqit::opaque_iterator<std::string, opqit::bidir> iterator;

        iterator begin();
        iterator end();

        // ...

        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



[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 call end rather than begin.


[22/04/2012 at 19:00:55]

Thanks Chris, fixed now.

(required, hint)

Links can be added like [this one ->], to my homepage.
Phrases and blocks of code can be enclosed in {{{triple braces}}}.
Any HTML markup will be escaped.