/***************************************************************************
*                                                                          *
*  Copyright (C) Golaem S.A.  All Rights Reserved.                         *
*                                                                          *
***************************************************************************/

#pragma once

#include "glmCoreDefinitions.h"
#include "glmTypes.h"
#include "glmAssert.h"
#include "glmCoreAPI.h"

#if GLM_IS_COMPILER_MSVC || defined(_MSC_VER)
#include "glmWindows.h"
#else
#include <time.h>
#endif

namespace glm
{
    /**
	* \brief Simple timer
	*
	* \ingroup time
	* \author Golaem <dev@golaem.com>
	*/
    class GLM_CORE_API Timer
    {
    public:
        /**
		* \brief Constructor.
		* \param unit the diviser for the unit of the counter (1 for seconds, 1000 for milliseconds, ...)
		*/
        Timer(unsigned int unit = 1);

        /**
		* \brief Destructor.
		*/
        virtual ~Timer();

        /**
		* \brief Start the timer.
		* \note If the timer is already started, it is stopped then restarted.
		* \return The time elapsed before the start.
		*/
        virtual Time start();

        /**
		* \brief Stop the timer.
		* \return The time at stop.
		*/
        virtual Time stop();

        /**
		* \brief Return the current time.
		*/
        Time getTime() const;

        /**
		* \brief Return true is the timer is started
		*/
        bool isStarted() const;

    private:
        bool _timerStarted;
        unsigned int _unit;
#ifdef _MSC_VER
        unsigned __int64 _performanceFrequency;
        unsigned __int64 _startValue;
        mutable unsigned __int64 _lastRequestedValue;
#else
        struct timespec _startValue;
        mutable struct timespec _lastRequestedValue;
        unsigned long _scaling;
#endif
    };
} // namespace glm
