
// You can redistribute this file and/or modify it under the terms of the MIT License.
// 
// Copyright (c) 2015 Saurabh Garg
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// 
// Author(s): Saurabh Garg
// Email: saurabhgarg@mysoc.net

#include "Timer.h"
#include <Windows.h>

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
struct TimerPriv
{
	LARGE_INTEGER   mStartTime;
	LARGE_INTEGER   mStopTime;
	static LONGLONG mFrequency;  // Frequency remains fixed during execution of a program.
	static LONGLONG mCorrection; // Correction remains fixed during execution of a program.
	
	TimerPriv()
	{
		mStartTime  = {0};
		mStopTime   = {0};
	}
};

LONGLONG TimerPriv::mFrequency  = 0;
LONGLONG TimerPriv::mCorrection = 0;
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //


// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
Timer::Timer()
{
	mPD = new TimerPriv;
	
	if(mPD->mFrequency == 0)
	{
		LARGE_INTEGER _frequency;
		QueryPerformanceFrequency(&_frequency);
		mPD->mFrequency = _frequency.QuadPart;
		
		// High-performance timer not available.
		if(mPD->mFrequency == 0)
		{
			// Set frequency to 1 so that there is no divide by zero error.
			// However, timer is not usable in this case.
			mPD->mFrequency = 1;
		}
		
		// Calibrate timer.
		LARGE_INTEGER _start, _stop;
		Sleep(0);
		QueryPerformanceCounter(&_start);
		QueryPerformanceCounter(&_stop);
		mPD->mCorrection = _stop.QuadPart - _start.QuadPart;
	}
}
Timer::~Timer()
{
	delete mPD;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //


// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
bool Timer::IsTimerAvailable()
{
	return mPD->mFrequency > 1 ? true : false;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //


// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
void Timer::Start()
{
	// Ensures that current thread will not be interrupted by any other 
	// thread for a while.
	Sleep(0);
	QueryPerformanceCounter(&mPD->mStartTime);
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //


// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
double Timer::Stop(TimeUnit timeUnit)
{
	QueryPerformanceCounter(&mPD->mStopTime);
	return TotalTimeElasped(timeUnit);
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //


// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
double Timer::TimeElapsedSinceStart(TimeUnit timeUnit)
{
	LARGE_INTEGER  _currentTime;
	QueryPerformanceCounter(&_currentTime);
	
	double _time = double(_currentTime.QuadPart - mPD->mStartTime.QuadPart - mPD->mCorrection);
	return (_time * timeUnit) / mPD->mFrequency;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //


// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
double Timer::TotalTimeElasped(TimeUnit timeUnit)
{
	double _time = double(mPD->mStopTime.QuadPart - mPD->mStartTime.QuadPart - mPD->mCorrection);
	return (_time * timeUnit) / mPD->mFrequency;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
