
#ifndef _WorkQueue_H_
#define _WorkQueue_H_

#include "assert.h"
#include <stdlib.h>
#include <iostream.h>

#include "protectedobject.h"
#include "globalstate.h"
#include "workrec.h"

class WorkQueue {

	private:
		PWorkRec		 *_recs;
		int                      _current_rec;

		int                      _size;
		int                      _alloc_size;

//if we own them, then we're responsible for deleting them
		int                      _own_rw;
	public: 
		WorkQueue(int own_rw = 0);
		WorkQueue(const WorkQueue &W) { CopyFrom(W); }
		virtual ~WorkQueue();

		WorkQueue& CopyFrom(const WorkQueue&);
		WorkQueue& operator=(const WorkQueue &W) { return CopyFrom(W); }

//		friend ostream& operator<<(ostream&, const WorkQueue&);
//		friend istream& operator>>(istream&, WorkQueue&);

//the workqueue needs to be locked for these ops to be MP-safe.
		int GetCurrent (void) const {return _current_rec;} 
		void SetCurrent (int newcur) {_current_rec = newcur;}

		PWorkRec operator[] (int num) const {
if ( (num < 0) || (num >= _size) )
{
      cerr << "asked for " << num << " from queue of size " << _size << " with current " << _current_rec << endl;
assert (0);
}
return _recs[num];}

//once they're added here, we own them, and we'll delete them when
//necessary.
		void AddWorkRec(WorkRec *WR);
		void Clear();

		int Empty() const { return (_size == 0); }
		int Done() const {return (_current_rec >= _size);}

		int Size() const { return _size; }
		void SetSize (int size);
};


class ProtectedWorkQueue : public ProtectedObject, public WorkQueue {
};


#endif
