Remove Stopwatch interface and make it a class.

Review URL: https://codereview.chromium.org//12210065

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@18242 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
floitsch@google.com
2013-02-08 10:46:14 +00:00
parent caa7fe236d
commit 4e12cd56ea
3 changed files with 47 additions and 67 deletions
+1 -1
View File
@@ -4,7 +4,7 @@
// A VM patch of the stopwatch part of dart:core.
patch class _StopwatchImpl {
patch class Stopwatch {
// Returns the current clock tick.
/* patch */ static int _now() native "Stopwatch_now";
@@ -158,7 +158,7 @@ patch class DateTime {
// Patch for Stopwatch implementation.
patch class _StopwatchImpl {
patch class Stopwatch {
patch static int _frequency() => 1000000;
patch static int _now() => Primitives.numMicroseconds();
}
+45 -65
View File
@@ -7,70 +7,7 @@ part of dart.core;
/**
* A simple [Stopwatch] interface to measure elapsed time.
*/
abstract class Stopwatch {
/**
* Creates a [Stopwatch] in stopped state with a zero elapsed count.
*
* The following example shows how to start a [Stopwatch]
* right after allocation.
*
* Stopwatch stopwatch = new Stopwatch()..start();
*/
factory Stopwatch() => new _StopwatchImpl();
/**
* Starts the [Stopwatch]. The [elapsed] count is increasing monotonically.
* If the [Stopwatch] has been stopped, then calling start again restarts it
* without resetting the [elapsed] count.
* If the [Stopwatch] is currently running, then calling start does nothing.
*/
void start();
/**
* Stops the [Stopwatch]. The [elapsed] count stops increasing.
* If the [Stopwatch] is currently not running, then calling stop does
* nothing.
*/
void stop();
/**
* Resets the [elapsed] count to zero. This method does not stop or start
* the [Stopwatch].
*/
void reset();
/**
* Returns the elapsed number of clock ticks since calling [start] while the
* [Stopwatch] is running.
* Returns the elapsed number of clock ticks between calling [start] and
* calling [stop].
* Returns 0 if the [Stopwatch] has never been started.
* The elapsed number of clock ticks increases by [frequency] every second.
*/
int get elapsedTicks;
/**
* Returns the [elapsedTicks] counter converted to microseconds.
*/
int get elapsedMicroseconds;
/**
* Returns the [elapsedTicks] counter converted to milliseconds.
*/
int get elapsedMilliseconds;
/**
* Returns the frequency of the elapsed counter in Hz.
*/
int get frequency;
/**
* Returns wether the [StopWatch] is currently running.
*/
bool get isRunning;
}
class _StopwatchImpl implements Stopwatch {
class Stopwatch {
// The _start and _stop fields capture the time when [start] and [stop]
// are called respectively.
// If _start is null, then the [Stopwatch] has not been started yet.
@@ -79,8 +16,22 @@ class _StopwatchImpl implements Stopwatch {
int _start;
int _stop;
_StopwatchImpl() : _start = null, _stop = null {}
/**
* Creates a [Stopwatch] in stopped state with a zero elapsed count.
*
* The following example shows how to start a [Stopwatch]
* right after allocation.
*
* Stopwatch stopwatch = new Stopwatch()..start();
*/
Stopwatch() : _start = null, _stop = null {}
/**
* Starts the [Stopwatch]. The [elapsed] count is increasing monotonically.
* If the [Stopwatch] has been stopped, then calling start again restarts it
* without resetting the [elapsed] count.
* If the [Stopwatch] is currently running, then calling start does nothing.
*/
void start() {
if (isRunning) return;
if (_start == null) {
@@ -94,11 +45,20 @@ class _StopwatchImpl implements Stopwatch {
}
}
/**
* Stops the [Stopwatch]. The [elapsed] count stops increasing.
* If the [Stopwatch] is currently not running, then calling stop does
* nothing.
*/
void stop() {
if (!isRunning) return;
_stop = _now();
}
/**
* Resets the [elapsed] count to zero. This method does not stop or start
* the [Stopwatch].
*/
void reset() {
if (_start == null) return;
// If [_start] is not null, then the stopwatch had already been started. It
@@ -111,6 +71,14 @@ class _StopwatchImpl implements Stopwatch {
}
}
/**
* Returns the elapsed number of clock ticks since calling [start] while the
* [Stopwatch] is running.
* Returns the elapsed number of clock ticks between calling [start] and
* calling [stop].
* Returns 0 if the [Stopwatch] has never been started.
* The elapsed number of clock ticks increases by [frequency] every second.
*/
int get elapsedTicks {
if (_start == null) {
return 0;
@@ -118,16 +86,28 @@ class _StopwatchImpl implements Stopwatch {
return (_stop == null) ? (_now() - _start) : (_stop - _start);
}
/**
* Returns the [elapsedTicks] counter converted to microseconds.
*/
int get elapsedMicroseconds {
return (elapsedTicks * 1000000) ~/ frequency;
}
/**
* Returns the [elapsedTicks] counter converted to milliseconds.
*/
int get elapsedMilliseconds {
return (elapsedTicks * 1000) ~/ frequency;
}
/**
* Returns the frequency of the elapsed counter in Hz.
*/
int get frequency => _frequency();
/**
* Returns wether the [StopWatch] is currently running.
*/
bool get isRunning => _start != null && _stop == null;
external static int _frequency();