src/ldobjectiterator.h

Sun, 06 Sep 2015 16:08:22 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 06 Sep 2015 16:08:22 +0300
changeset 992
5a10b4e7bf4a
parent 956
2af18ba2665f
child 1005
a88cf5a32246
permissions
-rw-r--r--

Refactor PartDownloadRequest

#pragma once
#include "ldObject.h"
#include "ldDocument.h"

template<typename T>
class LDObjectIterator
{
public:
	LDObjectIterator (LDDocument* doc) :
		m_list (doc->objects()),
		m_i (-1)
	{
		seekTillValid();
	}

	LDObjectIterator (const LDObjectList& objs) :
		m_list (objs),
		m_i (-1)
	{
		seekTillValid();
	}

	bool outOfBounds() const
	{
		return m_i < 0 or m_i >= m_list.size();
	}

	T* get() const
	{
		return (T*) m_list[m_i];
	}

	bool isValid() const
	{
		return not outOfBounds() and get()->type() == T::SubclassType;
	}

	void seek (int i)
	{
		m_i = i;
	}

	void seekTillValid()
	{
		do ++m_i;
		while (not outOfBounds() and not isValid());
	}

	void rewindTillValid()
	{
		do --m_i;
		while (m_i >= 0 and not isValid());
	}

	int tell() const
	{
		return m_i;
	}

	T* operator*() const
	{
		return get();
	}

	T* operator->() const
	{
		return get();
	}

	void operator++()
	{
		seekTillValid();
	}

	void operator++ (int)
	{
		seekTillValid(); 
	}

	void operator--()
	{
		rewindTillValid();
	}

	void operator-- (int)
	{
		rewindTillValid();
	}

private:
	const LDObjectList& m_list;
	int m_i;
};

mercurial