Add base::Chrono class.

This commit is contained in:
David Capello 2012-03-22 15:04:36 -03:00
parent fffc32548a
commit 25884b0fb7
5 changed files with 121 additions and 0 deletions

View File

@ -14,6 +14,7 @@ if(HAVE_SCHED_YIELD)
endif()
add_library(base-lib
chrono.cpp
convert_to.cpp
errno_string.cpp
exception.cpp

34
src/base/chrono.cpp Normal file
View File

@ -0,0 +1,34 @@
// ASEPRITE base library
// Copyright (C) 2001-2012 David Capello
//
// This source file is ditributed under a BSD-like license, please
// read LICENSE.txt for more information.
#include "config.h"
#include "base/chrono.h"
#ifdef _WIN32
#include "base/chrono_win32.h"
#else
#include "base/chrono_unix.h"
#endif
namespace base {
Chrono::Chrono() : m_impl(new ChronoImpl) {
}
Chrono::~Chrono() {
delete m_impl;
}
void Chrono::reset() {
m_impl->reset();
}
double Chrono::elapsed() const {
return m_impl->elapsed();
}
} // namespace base

26
src/base/chrono.h Normal file
View File

@ -0,0 +1,26 @@
// ASEPRITE base library
// Copyright (C) 2001-2012 David Capello
//
// This source file is ditributed under a BSD-like license, please
// read LICENSE.txt for more information.
#ifndef BASE_CHRONO_H_INCLUDED
#define BASE_CHRONO_H_INCLUDED
namespace base {
class Chrono {
public:
Chrono();
~Chrono();
void reset();
double elapsed() const;
private:
class ChronoImpl;
ChronoImpl* m_impl;
};
} // namespace base
#endif // BASE_CHRONO_H_INCLUDED

29
src/base/chrono_unix.h Normal file
View File

@ -0,0 +1,29 @@
// ASEPRITE base library
// Copyright (C) 2001-2012 David Capello
//
// This source file is ditributed under a BSD-like license, please
// read LICENSE.txt for more information.
#include <time.h>
#include <sys/time.h>
class base::Chrono::ChronoImpl {
public:
ChronoImpl() {
reset();
}
void reset() {
gettimeofday(&m_point, NULL);
}
double elapsed() const {
struct timeval now;
gettimeofday(&now, NULL);
return (double)(now.tv_sec + (double)now.tv_usec/1000000) -
(double)(m_point.tv_sec + (double)m_point.tv_usec/1000000);
}
private:
struct timeval m_point;
};

31
src/base/chrono_win32.h Normal file
View File

@ -0,0 +1,31 @@
// ASEPRITE base library
// Copyright (C) 2001-2012 David Capello
//
// This source file is ditributed under a BSD-like license, please
// read LICENSE.txt for more information.
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
class base::Chrono::ChronoImpl {
public:
ChronoImpl() {
QueryPerformanceFrequency(&m_freq);
reset();
}
void reset() {
QueryPerformanceCounter(&m_point);
}
double elapsed() const {
LARGE_INTEGER now;
QueryPerformanceCounter(&now);
return static_cast<double>(now.QuadPart - m_point.QuadPart)
/ static_cast<double>(m_freq.QuadPart);
}
private:
LARGE_INTEGER m_point;
LARGE_INTEGER m_freq;
};