Skip to main content

stopwatch.cpp File

C++ source file with implementations for the µTest++ stopwatch. More...

Included Headers​

Namespaces Index​

namespacemicro_os_plus

The primary namespace for the µOS++ framework. More...

namespacemicro_test_plus

Primary namespace for the µTest++ testing framework. More...

namespacedetail

Internal implementation details for the µTest++ framework. More...

Functions Index​

voidread_clock (timespec &ts) noexcept

Reads the system clock. More...

Description​

C++ source file with implementations for the µTest++ stopwatch.

This source file contains the implementation of the stopwatch class of the µTest++ framework. It provides the member functions that read the system clock (monotonic on POSIX) at the start and at the end of the measured interval, and that compute the elapsed time between the two readings, and read_clock(), which isolates the platform-specific access to the system clock.

All definitions reside within the micro_os_plus::micro_test_plus::detail namespace.

This file must be included when building the µTest++ library.

Functions​

read_clock()​

void micro_os_plus::micro_test_plus::detail::read_clock (timespec & ts)
noexcept

Reads the system clock.

Isolates the platform-specific access to the system clock, used by stopwatch and by the deprecated timestamp class. The clock is monotonic on POSIX; without a clock, ts is left unchanged, so the callers clear it first when a zero value must mean that the clock was not read.

Parameters
[out] ts

The timespec where the reading is stored.

Returns

Nothing.

On Windows, the current time is captured via timespec_get() with TIME_UTC. On POSIX platforms with CLOCK_MONOTONIC defined, clock_gettime(CLOCK_MONOTONIC) is used to obtain a monotonic reading. On platforms where neither macro is defined, ts is left unchanged.

Definition at line 66 of file stopwatch.cpp.

66 read_clock (timespec& ts) noexcept
67 {
68#if defined(_WIN32)
69 timespec_get (&ts, TIME_UTC);
70#elif defined(CLOCK_MONOTONIC)
71 clock_gettime (CLOCK_MONOTONIC, &ts);
72#else
73 (void)ts;
74#endif // defined(_WIN32) || defined(CLOCK_MONOTONIC)
75 }

Reference micro_os_plus::micro_test_plus::detail::read_clock.

Referenced by micro_os_plus::micro_test_plus::detail::timestamp::timestamp, micro_os_plus::micro_test_plus::detail::read_clock, micro_os_plus::micro_test_plus::detail::stopwatch::start, micro_os_plus::micro_test_plus::detail::stopwatch::stop, micro_os_plus::micro_test_plus::detail::stopwatch::timestamp_begin and micro_os_plus::micro_test_plus::detail::stopwatch::timestamp_end.

File Listing​

The file content with the documentation metadata removed is:

1/*
2 * This file is part of the µOS++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2021-2026 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can be
9 * obtained from https://opensource.org/licenses/mit.
10 */
11
12// ----------------------------------------------------------------------------
13
31
32// ----------------------------------------------------------------------------
33
35
36// ----------------------------------------------------------------------------
37
38#if defined(__GNUC__)
39#pragma GCC diagnostic ignored "-Waggregate-return"
40#if defined(__clang__)
41#pragma clang diagnostic ignored "-Wpre-c++17-compat"
42#pragma clang diagnostic ignored "-Wc++98-compat"
43#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
44#endif // defined(__clang__)
45#endif // defined(__GNUC__)
46
47// ============================================================================
48
49#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_ENABLED)
50
51// ----------------------------------------------------------------------------
52
54{
55 // --------------------------------------------------------------------------
56
65 void
66 read_clock (timespec& ts) noexcept
67 {
68#if defined(_WIN32)
69 timespec_get (&ts, TIME_UTC);
70#elif defined(CLOCK_MONOTONIC)
71 clock_gettime (CLOCK_MONOTONIC, &ts);
72#else
73 (void)ts;
74#endif // defined(_WIN32) || defined(CLOCK_MONOTONIC)
75 }
76
77 // --------------------------------------------------------------------------
78
86 void
87 stopwatch::start (void) noexcept
88 {
89 start_time_ = {};
90 stop_time_ = {};
92 }
93
100 void
101 stopwatch::stop (void) noexcept
102 {
103 stop_time_ = {};
105 }
106
115 std::chrono::microseconds
116 stopwatch::elapsed (void) const noexcept
117 {
118 if (!has_elapsed ())
119 {
120 return std::chrono::microseconds::zero ();
121 }
122
123 const std::chrono::nanoseconds delta = std::chrono::seconds{
124 stop_time_.tv_sec - start_time_.tv_sec
125 } + std::chrono::nanoseconds{ stop_time_.tv_nsec - start_time_.tv_nsec };
126
127 return std::chrono::duration_cast<std::chrono::microseconds> (delta);
128 }
129
130#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_DEPRECATED_ENABLED)
137 // Deprecated since 6.0.0; use start().
138 void
140 {
142 {
144 }
145 }
146
153 // Deprecated since 6.0.0; use stop().
154 void
156 {
158 {
160 }
161 }
162#endif // defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_DEPRECATED_ENABLED)
163
164 // --------------------------------------------------------------------------
165} // namespace micro_os_plus::micro_test_plus::detail
166
167// ----------------------------------------------------------------------------
168
169#endif // defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_ENABLED)
170
171// ----------------------------------------------------------------------------

Generated via doxygen2docusaurus 2.2.8 by Doxygen 1.18.0.