/***************************************************************************
 *   Copyright (C) 2000-2010 by Noel Novelli                               *
 *   novelli@lif.univ-mrs.fr                                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#ifndef __THREADTIME_H__
#define __THREADTIME_H__

#ifdef WIN32
#include <windows.h>
#else
#include <sys/time.h>
#include <sys/resource.h>
#endif // WIN32

class ThreadTime
{
#ifdef WIN32
        // Handle of the thread
    HANDLE m_hThread;
        // Begining of measure if we use ::GetThreadTimes
    FILETIME m_Creat, m_Exit, m_Kern, m_User;
        // Begining of mesure if we use ::GetThreadTimes
    DWORD m_startTime;

    int ThreadTimeElapsedSystemTime(SYSTEMTIME *elapsed) const;
#else
    struct rusage m_usage;
#endif

  public:

    ThreadTime( );
    int ThreadTimeInit( );
    int ThreadTimeElapsedTime( std::ostream &os = std::cerr ) const;

    int getElapsedTimeStrSec( std::string &sTime, double &timeInSec ) const;
    int getElapsedTimeSec( double &timeInSec ) const;

};  // CThreadTime

class CThreadCumulatedTime
{
    ThreadTime _threadTime;
    double _dCumulTime;

    public:
        CThreadCumulatedTime( ) : _threadTime( ), _dCumulTime( )
        {
            start();
        }
        
        void start( );
        void stop( );
        void reset( );
        
        void getTimeStrSec( std::string &sTime, double &timeSec ) const;
        void getTimeSec( double &timeSec ) const;
};

#endif // __THREADTIME_H__

