Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MagicIterator
- MagicIterator
- MagicIterator
- class cont_iter
- : public boost::iterator_adaptor<
- cont_iter // Derived
- , std::list<Container::Item>::iterator // Base
- , IInterface* // Value
- , boost::forward_traversal_tag // CategoryOrTraversal
- , IInterface* // Reference :)
- >
- {
- public:
- cont_iter()
- : cont_iter::iterator_adaptor_() {}
- explicit cont_iter(const cont_iter::iterator_adaptor_::base_type& p)
- : cont_iter::iterator_adaptor_(p) {}
- private:
- friend class boost::iterator_core_access;
- IInterface* dereference() { return this->base()->pObject->GetInterface(); }
- };
- namespace detail {
- template<typename T, typename U>
- struct constify;
- template<typename T, typename U>
- struct constify<T*, U*> {
- typedef T * type;
- };
- template<typename T, typename U>
- struct constify<T*, U const*> {
- typedef T const * type;
- };
- }
- template<typename DstType,
- typename Container,
- typename InputIterator>
- struct MagicIterator;
- class Container
- {
- private:
- struct Item
- {
- Object* pObject;
- };
- std::list<Item> m_items;
- public:
- // required by every Container for the iterator
- typedef std::list<Item> iterator;
- typedef std::list<Item> const_iterator;
- // convenience declarations
- typedef MagicIterator< IInterface*, Container, iterator >
- item_iterator;
- typedef MagicIterator< IInterface*, Container, const_iterator >
- const_item_iterator;
- item_iterator Begin();
- item_iterator End();
- };
- template<typename DstType,
- typename Container = Container,
- typename InputIterator = typename Container::iterator>
- struct MagicIterator :
- // pick either const T or T, depending on whether it's a const_iterator.
- std::iterator<std::input_iterator_tag,
- typename detail::constify<
- DstType,
- typename InputIterator::value_type*>::type> {
- typedef std::iterator<std::input_iterator_tag,
- typename detail::constify<
- DstType,
- typename InputIterator::value_type*>::type> base;
- MagicIterator():wrapped() { }
- explicit MagicIterator(InputIterator const& it):wrapped(it) { }
- MagicIterator(MagicIterator const& that):wrapped(that.wrapped) { }
- typename base::value_type operator*() {
- return (*wrapped).pObject->GetInterface();
- }
- MagicIterator& operator++() {
- ++wrapped;
- return *this;
- }
- MagicIterator operator++(int) {
- MagicIterator it(*this);
- wrapped++;
- return it;
- }
- bool operator==(MagicIterator const& it) const {
- return it.wrapped == wrapped;
- }
- bool operator!=(MagicIterator const& it) const {
- return !(*this == it);
- }
- InputIterator wrapped;
- };
- // now that the iterator adepter is defined, we can define Begin and End
- inline Container::item_iterator Container::Begin() {
- return item_iterator(m_items.begin());
- }
- inline Container::item_iterator Container::End() {
- return item_iterator(m_items.end());
- }
- for(MagicIterator<IInterface*> it = c.Begin(); it != c.End(); ++it) {
- // ...
- }
- template<typename T>
- class IteratorImplementation
- {
- public:
- virtual ~IteratorImplementation() = 0;
- virtual T &operator*() = 0;
- virtual const T &operator*() const = 0;
- virtual Iterator<T> &operator++() = 0;
- virtual Iterator<T> &operator--() = 0;
- };
- template<typename T>
- class Iterator
- {
- public:
- Iterator(IteratorImplementation<T> * = 0);
- ~Iterator();
- T &operator*();
- const T &operator*() const;
- Iterator<T> &operator++();
- Iterator<T> &operator--();
- private:
- IteratorImplementation<T> *i;
- }
- Iterator::Iterator(IteratorImplementation<T> *impl) :
- i(impl)
- {
- }
- Iterator::~Iterator()
- {
- delete i;
- }
- T &Iterator::operator*()
- {
- if(!impl)
- {
- // Throw exception if you please.
- return;
- }
- return (*impl)();
- }
- // etc.
- class ObjectContainer
- {
- public:
- void insert(Object *o);
- // ...
- Iterator<Object *> begin();
- Iterator<Object *> end();
- private:
- class CustomIteratorImplementation :
- public IteratorImplementation<Object *>
- {
- public:
- // Re-implement stuff here.
- }
- };
- Iterator<Object *> ObjectContainer::begin()
- {
- CustomIteratorImplementation *impl = new CustomIteratorImplementation(); // Wish we had C++0x's "var" here. ;P
- return Iterator<Object *>(impl);
- }
- template<typename T>
- class MagicIterator
- {
- public:
- MagicIterator(std::vector<T>::const_iterator i)
- {
- vector_const_iterator = i;
- }
- // Reimplement similarly for more types.
- MagicIterator(std::vector<T>::iterator i);
- MagicIterator(std::list<T>::const_iterator i);
- MagicIterator(std::list<T>::iterator i);
- // Reimplement operators here...
- private:
- std::vector<T>::const_iterator vector_const_iterator;
- std::vector<T>::iterator vector_iterator;
- std::list<T>::const_iterator list_const_iterator;
- std::list<T>::iterator list_iterator;
- };
- // C++0x
- template<typename T>
- class Iterator :
- public T::iterator
- {
- using T::iterator::iterator;
- };
- for(Iterator<Container> i = c.begin(); i != c.end(); ++i)
- {
- // ...
- }
Add Comment
Please, Sign In to add comment