src/ldobjectiterator.h

Sun, 30 Aug 2015 17:20:55 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 30 Aug 2015 17:20:55 +0300
changeset 969
b1742ee91d5b
parent 956
2af18ba2665f
child 1005
a88cf5a32246
permissions
-rw-r--r--

Split actions.cpp and actionsEdit.cpp into toolsets.

#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