/*********************************************************************
 * This file is part of the cpplibs suite.
 *
 * Copyright (C) 2001 Topi Mäenpää
 * All rights reserved.
 *
 * This program is free software. You can redistribute and/or modify
 * it under the terms of the free software licence found in the
 * accompanying file "COPYING". The licence terms must always be
 * redistributed with this source file. The above copyright notice
 * must be reproduced in all modified and unmodified copies of this
 * source file.
 *
 * $Revision: 1.3 $
 *********************************************************************/

#ifndef _TIMER_H
#define _TIMER_H

#include <time.h>
#include "Object.h"

namespace util
{
	/**
	 * Timer uses the system's real-time clock to perform
	 * nanosecond-resolution timing. In practical use, the maximum
	 * resolution is never achieved, but accurate timings are still
	 * possible.
	 **/
	class Timer : virtual public Object
	{
	public:
		/**
		 * Start the timer. If the timer is already started, the previous
		 * start time is lost.
		 **/
		void start() { clock_gettime(CLOCK_REALTIME,&_tmeStart); }

		/**
		 * Get the time elapsed from the last start() call. Time is
		 * measured in seconds.
		 **/
		double getTime();

		/**
		 * Get the time elapsed from the last start() call.
		 *
		 * @param seconds where to store the number of seconds elapsed
		 * @param nanoseconds where to store the number of nanoseconds
		 * elapsed
		 **/
		void getTime(int& seconds, long& nanoseconds);

	private:
		struct timespec _tmeStart;
	};
}

#endif
