#ifndef TIMERC_DEV_H
#define TIMERC_DEV_H


#include <pragma/exec_lib.h>
#include <devices/timer.h>
#include <stddef.h>


#ifdef TIMERPOOL
extern void *timerpool;
#include <ClassLibrary/MiscFunctions.h>
#endif


class TimerC {
public:
	TimerC();
	~TimerC();

	inline UBYTE GetSigBit() const { return tp->mp_SigBit; };
	inline ULONG GetSignal() const { return 1L<<tp->mp_SigBit; };

	inline void WaitTimer() { if(sent) ::WaitPort(tp); };
	inline BOOL GetTimerMsg() { BOOL res=TRUE; if(::GetMsg(tp)) sent--; else res=FALSE; return res; };

	inline void Abort() { GetTimerMsg(); if(sent) { ::AbortIO((IORequest *)tr); sent--; } };
	inline void Send(ULONG secs, ULONG micros)
	{
		Abort();
		tr->tr_time.tv_secs  = secs;
		tr->tr_time.tv_micro = micros;
		::SendIO((IORequest *)tr);
		sent++;
	};
	#ifdef TIMERPOOL
	TimerC *operator new(size_t size)
	{
		return (TimerC *)PooledAllocVec(timerpool, (ULONG)size);
	};

	void operator delete(void *obj)
	{
		PooledFreeVec(timerpool, obj);
	};
	#endif
private:
	timerequest *tr;
	MsgPort  *tp;
	LONG sent;
};


#endif
