Skip to content

Commit 6050539

Browse files
authored
Merge pull request #2 from heplesser/jv_timers
Fix build failure with clang/macos
2 parents dfba6a7 + 1bcc121 commit 6050539

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

nestkernel/stopwatch.h

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,26 @@ constexpr bool use_threaded_timers = true;
5353
constexpr bool use_threaded_timers = false;
5454
#endif
5555

56-
enum StopwatchGranularity
56+
enum class StopwatchGranularity
5757
{
5858
Normal, //<! Always measure stopwatch
5959
Detailed //<! Only measure if detailed stopwatches are activated
6060
};
6161

62-
enum StopwatchParallelism
62+
enum class StopwatchParallelism
6363
{
6464
MasterOnly, //<! Only the master thread owns a stopwatch
6565
Threaded //<! Every thread measures an individual stopwatch
6666
};
6767

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+
6876
/********************************************************************************
6977
* Stopwatch *
7078
* Accumulates time between start and stop, and provides the elapsed time *
@@ -93,7 +101,7 @@ enum StopwatchParallelism
93101
********************************************************************************/
94102
namespace timers
95103
{
96-
enum timeunit_t : size_t
104+
enum class timeunit_t : size_t
97105
{
98106
NANOSEC = 1,
99107
MICROSEC = NANOSEC * 1000,
@@ -111,7 +119,7 @@ template < clockid_t clock_type >
111119
class StopwatchTimer
112120
{
113121
template < StopwatchGranularity, StopwatchParallelism, typename >
114-
friend class Stopwatch;
122+
friend class nest::Stopwatch;
115123

116124
public:
117125
typedef size_t timestamp_t;
@@ -134,13 +142,14 @@ class StopwatchTimer
134142
* want only the last measurement, you have to reset the timer, before stating the measurement.
135143
* Does not change the running state.
136144
*/
137-
double elapsed( timeunit_t timeunit = SECONDS ) const;
145+
double elapsed( timeunit_t timeunit = timeunit_t::SECONDS ) const;
138146

139147
//! Resets the stopwatch.
140148
void reset();
141149

142150
//! 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;
144153

145154
private:
146155
//! Returns, whether the stopwatch is running.
@@ -210,7 +219,7 @@ StopwatchTimer< clock_type >::elapsed( timeunit_t timeunit ) const
210219
// stopped before, get time of current measurement + last measurements
211220
time_elapsed = _end - _beg + _prev_elapsed;
212221
}
213-
return static_cast< double >( time_elapsed ) / timeunit;
222+
return static_cast< double >( time_elapsed ) / static_cast< double >( timeunit );
214223
#else
215224
return 0.;
216225
#endif
@@ -237,24 +246,24 @@ StopwatchTimer< clock_type >::print( const std::string& msg, timeunit_t timeunit
237246
os << msg << e;
238247
switch ( timeunit )
239248
{
240-
case NANOSEC:
249+
case timeunit_t::NANOSEC:
241250
os << " nanosec.";
242-
case MICROSEC:
251+
case timeunit_t::MICROSEC:
243252
os << " microsec.";
244253
break;
245-
case MILLISEC:
254+
case timeunit_t::MILLISEC:
246255
os << " millisec.";
247256
break;
248-
case SECONDS:
257+
case timeunit_t::SECONDS:
249258
os << " sec.";
250259
break;
251-
case MINUTES:
260+
case timeunit_t::MINUTES:
252261
os << " min.";
253262
break;
254-
case HOURS:
263+
case timeunit_t::HOURS:
255264
os << " h.";
256265
break;
257-
case DAYS:
266+
case timeunit_t::DAYS:
258267
os << " days.";
259268
break;
260269
default:
@@ -274,7 +283,7 @@ StopwatchTimer< clock_type >::get_current_time()
274283
{
275284
timespec now;
276285
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 );
278287
}
279288

280289
template < clockid_t clock_type >
@@ -285,7 +294,7 @@ operator<<( std::ostream& os, const StopwatchTimer< clock_type >& stopwatch )
285294
return os;
286295
}
287296

288-
}
297+
} // namespace timers
289298

290299

291300
/** This is the base template for all Stopwatch specializations.
@@ -515,5 +524,5 @@ class Stopwatch< detailed_timer,
515524
std::vector< timers::StopwatchTimer< CLOCK_THREAD_CPUTIME_ID > > cputime_timers_;
516525
};
517526

518-
} /* namespace timer */
527+
} /* namespace nest */
519528
#endif /* STOPWATCH_H */

0 commit comments

Comments
 (0)