1
1
/*
2
- TimeThis : Simple stopwatch implementation with optional callback on destructor
3
- Version 1.0.0
2
+ TimeThis : Simple stopwatch implementation with optional callback on destructor
3
+ Version 1.0.0
4
4
5
- https://github.com/SiddiqSoft/TimeThis
5
+ https://github.com/SiddiqSoft/TimeThis
6
6
7
- BSD 3-Clause License
7
+ BSD 3-Clause License
8
8
9
- Copyright (c) 2021, Siddiq Software LLC
10
- All rights reserved.
9
+ Copyright (c) 2021, Siddiq Software LLC
10
+ All rights reserved.
11
11
12
- Redistribution and use in source and binary forms, with or without
13
- modification, are permitted provided that the following conditions are met:
12
+ Redistribution and use in source and binary forms, with or without
13
+ modification, are permitted provided that the following conditions are met:
14
14
15
- 1. Redistributions of source code must retain the above copyright notice, this
16
- list of conditions and the following disclaimer.
15
+ 1. Redistributions of source code must retain the above copyright notice, this
16
+ list of conditions and the following disclaimer.
17
17
18
- 2. Redistributions in binary form must reproduce the above copyright notice,
19
- this list of conditions and the following disclaimer in the documentation
20
- and/or other materials provided with the distribution.
18
+ 2. Redistributions in binary form must reproduce the above copyright notice,
19
+ this list of conditions and the following disclaimer in the documentation
20
+ and/or other materials provided with the distribution.
21
21
22
- 3. Neither the name of the copyright holder nor the names of its
23
- contributors may be used to endorse or promote products derived from
24
- this software without specific prior written permission.
22
+ 3. Neither the name of the copyright holder nor the names of its
23
+ contributors may be used to endorse or promote products derived from
24
+ this software without specific prior written permission.
25
25
26
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28
- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29
- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
30
- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31
- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32
- SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33
- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34
- OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
30
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
36
*/
37
37
38
38
#pragma once
39
- #ifndef TimeThis_HPP
40
- #define TimeThis_HPP 1
39
+ #ifndef TIMETHIS_HPP
40
+ #define TIMETHIS_HPP 1
41
41
42
42
#include < functional>
43
43
#include < chrono>
@@ -59,13 +59,16 @@ namespace siddiqsoft
59
59
{
60
60
// / @brief Calculates the duration since the creation of this object
61
61
// / @return Value representing the elapsed duration as timepoint
62
- auto elapsed () const { return std::chrono::system_clock::now () - startTimestamp; }
62
+ auto elapsed () const
63
+ {
64
+ return std::chrono::system_clock::now () - startTimestamp;
65
+ }
63
66
64
67
#if defined(__cpp_lib_source_location)
65
68
// / @brief When source_location is available, collect the calling location
66
69
explicit TimeThis (const std::source_location& sl = std::source_location::current())
67
- : sourceLocation(sl)
68
- , startTimestamp(std::chrono::system_clock::now())
70
+ : sourceLocation(sl)
71
+ , startTimestamp(std::chrono::system_clock::now())
69
72
{
70
73
}
71
74
@@ -74,9 +77,9 @@ namespace siddiqsoft
74
77
// / @param context Reference to the context
75
78
explicit TimeThis (std::function<void (const std::chrono::system_clock::duration&)>&& callback,
76
79
const std::source_location& sl = std::source_location::current())
77
- : sourceLocation(sl)
78
- , mCallback(std::move(callback))
79
- , startTimestamp(std::chrono::system_clock::now())
80
+ : sourceLocation(sl)
81
+ , mCallback(std::move(callback))
82
+ , startTimestamp(std::chrono::system_clock::now())
80
83
{
81
84
}
82
85
@@ -92,16 +95,16 @@ namespace siddiqsoft
92
95
#else
93
96
// / @brief Default constructor notes the start time
94
97
TimeThis ()
95
- : startTimestamp(std::chrono::system_clock::now())
98
+ : startTimestamp(std::chrono::system_clock::now())
96
99
{
97
100
}
98
101
99
102
// / @brief Construct an object which holds the callback to be executed upon destruction
100
103
// / @param callback The callback takes timepoint representing the final calculation of the delta
101
104
// / @param context Reference to the context
102
105
explicit TimeThis (std::function<void (const std::chrono::system_clock::duration&)>&& callback) noexcept
103
- : mCallback(std::move(callback))
104
- , startTimestamp(std::chrono::system_clock::now())
106
+ : mCallback(std::move(callback))
107
+ , startTimestamp(std::chrono::system_clock::now())
105
108
{
106
109
}
107
110
@@ -116,11 +119,13 @@ namespace siddiqsoft
116
119
}
117
120
#endif
118
121
119
- // / @brief Not supported. Makes no sense to copy another instance as the use-case should allow for a single task per callback.
122
+ // / @brief Not supported. Makes no sense to copy another instance as the use-case should allow for a single task per
123
+ // / callback.
120
124
// / @param ignored
121
125
TimeThis (TimeThis&) = delete ;
122
126
123
- // / @brief Not supported. Makes no sense to move another instance as the use-case should allow for a single task per callback.
127
+ // / @brief Not supported. Makes no sense to move another instance as the use-case should allow for a single task per
128
+ // / callback.
124
129
// / @param ignored
125
130
TimeThis (TimeThis&&) = delete ;
126
131
@@ -157,23 +162,24 @@ namespace siddiqsoft
157
162
158
163
#if defined __cpp_lib_format
159
164
// / @brief Formatter for std::format
160
- template <> struct std ::formatter<siddiqsoft::TimeThis> : std::formatter<std::string>
165
+ template <>
166
+ struct std ::formatter<siddiqsoft::TimeThis> : std::formatter<std::string>
161
167
{
162
168
auto format (const siddiqsoft::TimeThis& sv, std::format_context& ctx)
163
169
{
164
170
#if defined __cpp_lib_source_location
165
171
return std::formatter<std::string>::format (
166
- std::format (" {} started on {:%FT%T}Z took {}us" ,
172
+ std::format (" {} started on {:%FT%T}Z took {}us" ,
167
173
sv.sourceLocation .function_name (),
168
174
sv.startTimestamp ,
169
175
std::chrono::duration_cast<std::chrono::microseconds>(sv.elapsed ()).count ()),
170
- ctx);
176
+ ctx);
171
177
#else
172
178
return std::formatter<std::string>::format (
173
- std::format (" scope started on {:%FT%T}Z took {}us" ,
179
+ std::format (" scope started on {:%FT%T}Z took {}us" ,
174
180
std::chrono::duration_cast<std::chrono::microseconds>(sv.elapsed ()).count (),
175
181
sv.startTimestamp ),
176
- ctx);
182
+ ctx);
177
183
#endif
178
184
}
179
185
};
0 commit comments