@@ -53,18 +53,26 @@ constexpr bool use_threaded_timers = true;
53
53
constexpr bool use_threaded_timers = false ;
54
54
#endif
55
55
56
- enum StopwatchGranularity
56
+ enum class StopwatchGranularity
57
57
{
58
58
Normal, // <! Always measure stopwatch
59
59
Detailed // <! Only measure if detailed stopwatches are activated
60
60
};
61
61
62
- enum StopwatchParallelism
62
+ enum class StopwatchParallelism
63
63
{
64
64
MasterOnly, // <! Only the master thread owns a stopwatch
65
65
Threaded // <! Every thread measures an individual stopwatch
66
66
};
67
67
68
+ // Forward class declaration required here because friend declaration
69
+ // in timers::StopwatchTimer must refer to nest::Stopwatch to be correct,
70
+ // and that requires the name to be known from before. See
71
+ // https://stackoverflow.com/questions/30418270/clang-bug-namespaced-template-class-friend
72
+ // for details.
73
+ template < StopwatchGranularity, StopwatchParallelism, typename >
74
+ class Stopwatch ;
75
+
68
76
/* *******************************************************************************
69
77
* Stopwatch *
70
78
* Accumulates time between start and stop, and provides the elapsed time *
@@ -93,7 +101,7 @@ enum StopwatchParallelism
93
101
********************************************************************************/
94
102
namespace timers
95
103
{
96
- enum timeunit_t : size_t
104
+ enum class timeunit_t : size_t
97
105
{
98
106
NANOSEC = 1 ,
99
107
MICROSEC = NANOSEC * 1000 ,
@@ -111,7 +119,7 @@ template < clockid_t clock_type >
111
119
class StopwatchTimer
112
120
{
113
121
template < StopwatchGranularity, StopwatchParallelism, typename >
114
- friend class Stopwatch ;
122
+ friend class nest :: Stopwatch;
115
123
116
124
public:
117
125
typedef size_t timestamp_t ;
@@ -134,13 +142,14 @@ class StopwatchTimer
134
142
* want only the last measurement, you have to reset the timer, before stating the measurement.
135
143
* Does not change the running state.
136
144
*/
137
- double elapsed ( timeunit_t timeunit = SECONDS ) const ;
145
+ double elapsed ( timeunit_t timeunit = timeunit_t :: SECONDS ) const ;
138
146
139
147
// ! Resets the stopwatch.
140
148
void reset ();
141
149
142
150
// ! This method prints out the currently elapsed time.
143
- void print ( const std::string& msg = " " , timeunit_t timeunit = SECONDS, std::ostream& os = std::cout ) const ;
151
+ void
152
+ print ( const std::string& msg = " " , timeunit_t timeunit = timeunit_t ::SECONDS, std::ostream& os = std::cout ) const ;
144
153
145
154
private:
146
155
// ! Returns, whether the stopwatch is running.
@@ -210,7 +219,7 @@ StopwatchTimer< clock_type >::elapsed( timeunit_t timeunit ) const
210
219
// stopped before, get time of current measurement + last measurements
211
220
time_elapsed = _end - _beg + _prev_elapsed;
212
221
}
213
- return static_cast < double >( time_elapsed ) / timeunit;
222
+ return static_cast < double >( time_elapsed ) / static_cast < double >( timeunit ) ;
214
223
#else
215
224
return 0 .;
216
225
#endif
@@ -237,24 +246,24 @@ StopwatchTimer< clock_type >::print( const std::string& msg, timeunit_t timeunit
237
246
os << msg << e;
238
247
switch ( timeunit )
239
248
{
240
- case NANOSEC:
249
+ case timeunit_t :: NANOSEC:
241
250
os << " nanosec." ;
242
- case MICROSEC:
251
+ case timeunit_t :: MICROSEC:
243
252
os << " microsec." ;
244
253
break ;
245
- case MILLISEC:
254
+ case timeunit_t :: MILLISEC:
246
255
os << " millisec." ;
247
256
break ;
248
- case SECONDS:
257
+ case timeunit_t :: SECONDS:
249
258
os << " sec." ;
250
259
break ;
251
- case MINUTES:
260
+ case timeunit_t :: MINUTES:
252
261
os << " min." ;
253
262
break ;
254
- case HOURS:
263
+ case timeunit_t :: HOURS:
255
264
os << " h." ;
256
265
break ;
257
- case DAYS:
266
+ case timeunit_t :: DAYS:
258
267
os << " days." ;
259
268
break ;
260
269
default :
@@ -274,7 +283,7 @@ StopwatchTimer< clock_type >::get_current_time()
274
283
{
275
284
timespec now;
276
285
clock_gettime ( clock_type, &now );
277
- return now.tv_nsec + now.tv_sec * timeunit_t ::SECONDS;
286
+ return now.tv_nsec + now.tv_sec * static_cast < long >( timeunit_t ::SECONDS ) ;
278
287
}
279
288
280
289
template < clockid_t clock_type >
@@ -285,7 +294,7 @@ operator<<( std::ostream& os, const StopwatchTimer< clock_type >& stopwatch )
285
294
return os;
286
295
}
287
296
288
- }
297
+ } // namespace timers
289
298
290
299
291
300
/* * This is the base template for all Stopwatch specializations.
@@ -515,5 +524,5 @@ class Stopwatch< detailed_timer,
515
524
std::vector< timers::StopwatchTimer< CLOCK_THREAD_CPUTIME_ID > > cputime_timers_;
516
525
};
517
526
518
- } /* namespace timer */
527
+ } /* namespace nest */
519
528
#endif /* STOPWATCH_H */
0 commit comments