CARLA
载入中...
搜索中...
未找到
LibCarla
source
carla
StopWatch.h
浏览该文件的文档.
1
// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2
// de Barcelona (UAB).
3
//
4
// This work is licensed under the terms of the MIT license.
5
// For a copy, see <https://opensource.org/licenses/MIT>.
6
7
#pragma once
// 防止头文件被多次包含,提高编译效率
8
9
#include <chrono>
// 引入 chrono 库,用于时间测量功能
10
#include <cstdint>
// 引入 cstdint 库,提供标准整数类型
11
12
namespace
carla
{
// 定义 carla 命名空间
13
namespace
detail {
// 定义 detail 命名空间,用于封装实现细节
14
15
template
<
typename
CLOCK>
// 模板类,允许传入不同类型的时钟
16
class
StopWatchTmpl
{
17
static_assert
(CLOCK::is_steady,
"The StopWatch's clock must be steady"
);
18
// 静态断言,确保传入的时钟类型是稳定的(steady),避免时间回退问题
19
20
public
:
21
using
clock
= CLOCK;
// 定义 clock 为传入的时钟类型别名
22
23
StopWatchTmpl
()
24
:
_start
(
clock
::now()),
// 初始化开始时间为当前时间
25
_end
(),
// 初始化结束时间为默认值
26
_is_running
(
true
) {}
// 初始化为秒表正在运行状态
27
28
void
Restart
() {
29
_is_running
=
true
;
// 设置秒表为运行状态
30
_start
= clock::now();
// 更新开始时间为当前时间
31
}
32
33
void
Stop
() {
34
_end
= clock::now();
// 更新结束时间为当前时间
35
_is_running
=
false
;
// 设置秒表为停止状态
36
}
37
38
typename
clock::duration
GetDuration
()
const
{
39
// 如果秒表正在运行,返回当前时间与开始时间的差值
40
// 否则,返回结束时间与开始时间的差值
41
return
_is_running
? clock::now() -
_start
:
_end
-
_start
;
42
}
43
44
template
<
class
RESOLUTION=std::chrono::milliseconds>
45
size_t
GetElapsedTime
()
const
{
46
// 将时间间隔转换为指定精度的时间单位(默认为毫秒),并返回其计数值
47
return
static_cast<
size_t
>
(std::chrono::duration_cast<RESOLUTION>(
GetDuration
()).count());
48
}
49
50
bool
IsRunning
()
const
{
51
return
_is_running
;
// 返回秒表是否正在运行的状态
52
}
53
54
private
:
55
typename
clock::time_point
_start
;
// 秒表开始时间点
56
typename
clock::time_point
_end
;
// 秒表结束时间点
57
bool
_is_running
;
// 秒表是否正在运行的状态
58
};
59
60
}
// namespace detail
61
62
using
StopWatch
=
detail::StopWatchTmpl<std::chrono::steady_clock>
;
63
// 实例化 StopWatchTmpl 模板,使用 std::chrono::steady_clock 作为时钟类型,定义 StopWatch 类型
64
65
}
// namespace carla
66
true
return true
Definition
ActorDispatcher.cpp:196
carla::detail::StopWatchTmpl
Definition
StopWatch.h:16
carla::detail::StopWatchTmpl::_is_running
bool _is_running
Definition
StopWatch.h:57
carla::detail::StopWatchTmpl::IsRunning
bool IsRunning() const
Definition
StopWatch.h:50
carla::detail::StopWatchTmpl::_end
clock::time_point _end
Definition
StopWatch.h:56
carla::detail::StopWatchTmpl::GetElapsedTime
size_t GetElapsedTime() const
Definition
StopWatch.h:45
carla::detail::StopWatchTmpl::Restart
void Restart()
Definition
StopWatch.h:28
carla::detail::StopWatchTmpl::clock
CLOCK clock
Definition
StopWatch.h:21
carla::detail::StopWatchTmpl::Stop
void Stop()
Definition
StopWatch.h:33
carla::detail::StopWatchTmpl::_start
clock::time_point _start
Definition
StopWatch.h:55
carla::detail::StopWatchTmpl::GetDuration
clock::duration GetDuration() const
Definition
StopWatch.h:38
carla::detail::StopWatchTmpl::StopWatchTmpl
StopWatchTmpl()
Definition
StopWatch.h:23
carla
CARLA模拟器的主命名空间。
Definition
Carla.cpp:139
制作者
1.10.0