mr-edd.co.uk :: horsing around with the C++ programming language

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:

  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

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

Comments

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

Edd

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

Thanks Chris, fixed now.

(optional)
(optional)
(required, hint)

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