-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR #7452 from Nir: Add time utility classes + UT
- Loading branch information
Showing
21 changed files
with
363 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# License: Apache 2.0. See LICENSE file in root directory. | ||
# Copyright(c) 2019 Intel Corporation. All Rights Reserved. | ||
|
||
target_sources(${LRS_TARGET} | ||
PRIVATE | ||
"${CMAKE_CURRENT_LIST_DIR}/time/common.h" | ||
"${CMAKE_CURRENT_LIST_DIR}/time/stopwatch.h" | ||
"${CMAKE_CURRENT_LIST_DIR}/time/timer.h" | ||
"${CMAKE_CURRENT_LIST_DIR}/time/periodic_timer.h" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// License: Apache 2.0. See LICENSE file in root directory. | ||
// Copyright(c) 2020 Intel Corporation. All Rights Reserved. | ||
|
||
#pragma once | ||
|
||
#include <chrono> | ||
|
||
namespace utilities | ||
{ | ||
namespace time | ||
{ | ||
using clock = std::chrono::steady_clock; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// License: Apache 2.0. See LICENSE file in root directory. | ||
// Copyright(c) 2020 Intel Corporation. All Rights Reserved. | ||
|
||
// Helper classes to keep track of time | ||
#pragma once | ||
|
||
#include "timer.h" | ||
|
||
namespace utilities | ||
{ | ||
namespace time | ||
{ | ||
// A timer class that wakes up every <delta> seconds/minutes/etc. on its bool operator: | ||
// Usage example: | ||
// periodic_timer timer( chrono::seconds( 5 )); | ||
// while(1) { | ||
// if( timer ) { /* do something every 5 seconds */ } | ||
// } | ||
class periodic_timer | ||
{ | ||
public: | ||
|
||
periodic_timer(clock::duration delta) | ||
: _delta(delta) | ||
{ | ||
} | ||
|
||
// Allow use of bool operator for checking time expiration and re trigger when time expired | ||
operator bool() const | ||
{ | ||
if (_delta.has_expired()) | ||
{ | ||
_delta.start(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
// Force time expiration | ||
void set_expired() | ||
{ | ||
_delta.set_expired(); | ||
} | ||
protected: | ||
timer _delta; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// License: Apache 2.0. See LICENSE file in root directory. | ||
// Copyright(c) 2020 Intel Corporation. All Rights Reserved. | ||
|
||
#pragma once | ||
|
||
#include "common.h" | ||
|
||
namespace utilities | ||
{ | ||
namespace time | ||
{ | ||
// Timer that counts forward in time (vs backwards in the 'timer' class) | ||
// It supply a basic stopwatch API, reset, get elapsed time.. | ||
class stopwatch | ||
{ | ||
public: | ||
|
||
stopwatch() | ||
{ | ||
_start = clock::now(); | ||
} | ||
|
||
// Reset the stopwatch time | ||
void reset(clock::time_point start_time = clock::now()) const | ||
{ | ||
_start = start_time; | ||
} | ||
|
||
// Get elapsed in milliseconds since timer creation | ||
double get_elapsed_ms() const | ||
{ | ||
return std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(get_elapsed()).count(); | ||
} | ||
|
||
// Get elapsed since timer creation | ||
clock::duration get_elapsed() const | ||
{ | ||
return clock::now() - _start; | ||
} | ||
|
||
// Get stopwatch start time | ||
clock::time_point get_start() const | ||
{ | ||
return _start; | ||
} | ||
|
||
protected: | ||
mutable clock::time_point _start; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// License: Apache 2.0. See LICENSE file in root directory. | ||
// Copyright(c) 2020 Intel Corporation. All Rights Reserved. | ||
|
||
// Helper classes to keep track of time | ||
#pragma once | ||
|
||
#include "stopwatch.h" | ||
|
||
namespace utilities | ||
{ | ||
namespace time | ||
{ | ||
// A timer counting backwards in time (vs forwards in the `stopwatch` class) | ||
// It supply basic timer API, start, has_expired.. | ||
class timer | ||
{ | ||
public: | ||
|
||
timer(clock::duration timeout) : _delta(timeout), sw() | ||
{ | ||
} | ||
|
||
// Start timer | ||
void start() const | ||
{ | ||
sw.reset(); | ||
} | ||
|
||
// Check if timer time expired | ||
bool has_expired() const | ||
{ | ||
return sw.get_start() + _delta <= clock::now(); | ||
} | ||
|
||
// Force time expiration | ||
void set_expired() | ||
{ | ||
sw.reset(clock::now() - (_delta + std::chrono::nanoseconds(100))); | ||
} | ||
protected: | ||
clock::duration _delta; | ||
stopwatch sw; | ||
}; | ||
} | ||
} |
Oops, something went wrong.