poco/Foundation/include/Poco/Stopwatch.h

109 lines
1.7 KiB
C
Raw Normal View History

2012-04-29 20:52:25 +02:00
//
// Stopwatch.h
//
// Library: Foundation
// Package: DateTime
// Module: Stopwatch
//
// Definition of the Stopwatch class.
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
2012-04-29 20:52:25 +02:00
//
#ifndef Foundation_Stopwatch_INCLUDED
#define Foundation_Stopwatch_INCLUDED
#include "Poco/Foundation.h"
#include "Poco/Clock.h"
2012-04-29 20:52:25 +02:00
namespace Poco {
class Foundation_API Stopwatch
/// A simple facility to measure time intervals
/// with microsecond resolution.
{
public:
Stopwatch();
~Stopwatch();
void start();
/// Starts (or restarts) the stopwatch.
2022-07-07 11:18:20 +02:00
2012-04-29 20:52:25 +02:00
void stop();
/// Stops or pauses the stopwatch.
2022-07-07 11:18:20 +02:00
2012-04-29 20:52:25 +02:00
void reset();
/// Resets the stopwatch.
2022-07-07 11:18:20 +02:00
2012-04-29 20:52:25 +02:00
void restart();
/// Resets and starts the stopwatch.
2022-07-07 11:18:20 +02:00
Clock::ClockDiff elapsed() const;
2012-04-29 20:52:25 +02:00
/// Returns the elapsed time in microseconds
/// since the stopwatch started.
2022-07-07 11:18:20 +02:00
2012-04-29 20:52:25 +02:00
int elapsedSeconds() const;
/// Returns the number of seconds elapsed
/// since the stopwatch started.
static Clock::ClockVal resolution();
2012-04-29 20:52:25 +02:00
/// Returns the resolution of the stopwatch.
private:
Stopwatch(const Stopwatch&);
Stopwatch& operator = (const Stopwatch&);
Clock _start;
Clock::ClockDiff _elapsed;
bool _running;
2012-04-29 20:52:25 +02:00
};
//
// inlines
//
inline void Stopwatch::start()
{
if (!_running)
{
_start.update();
_running = true;
}
}
inline void Stopwatch::stop()
{
if (_running)
{
Clock current;
2012-04-29 20:52:25 +02:00
_elapsed += current - _start;
_running = false;
}
}
inline int Stopwatch::elapsedSeconds() const
{
return int(elapsed()/resolution());
}
inline Clock::ClockVal Stopwatch::resolution()
2012-04-29 20:52:25 +02:00
{
return Clock::resolution();
2012-04-29 20:52:25 +02:00
}
} // namespace Poco
#endif // Foundation_Stopwatch_INCLUDED