#ifndef H_CORAL_PYTHON_COMPAT
#define H_CORAL_PYTHON_COMPAT
// This file is part of the Coral project.
// Please read the copyright notice in the LICENSE file.

#include <coral/lib/ref_counted.h>
#include <iostream>

#include <coral/core/PyOperation.h>

/* Supports Python iterator protocol */
template <class Iterator, class T> class EXPORT_coral PythonSequenceIteratorDirect : public coral::ref_counted {
 private:
	ref_counted* ref;
	Iterator i;
	Iterator end;
 public:
	PythonSequenceIteratorDirect(ref_counted* r, Iterator begin, Iterator theend) throw () : ref_counted(), ref(r), i(begin), end(theend) {
		coral_assert(r);
		r->ref();
	}
	virtual ~PythonSequenceIteratorDirect() throw () {
	     this->ref->unref();
	}
	PythonSequenceIteratorDirect* __iter__() throw () { return this; }
	T next() throw () {
		if (this->i == this->end)
			return NULL;
		T f = (*this->i);
		++this->i;
		return f;
	}
};


/* Supports Python iterator protocol */
/* This has the difference in that it uses .second in next() */
template <class Iterator, class T> class EXPORT_coral PythonSequenceIteratorSecond : public coral::ref_counted {
 private:
	ref_counted* ref;
	Iterator i;
	Iterator end;
 public:
	PythonSequenceIteratorSecond(ref_counted* r, Iterator begin, Iterator theend) throw () : ref_counted(), ref(r), i(begin), end(theend) {
		coral_assert(r);
		r->ref();
	}
	virtual ~PythonSequenceIteratorSecond() throw () {
	     this->ref->unref();
	}
	PythonSequenceIteratorSecond* __iter__() throw () { return this; }
	T next() throw () {
		if (this->i == this->end)
			return NULL;
		T f = (*this->i).second;
		++this->i;
		return f;
	}
};

#endif
