The Battle for Wesnoth  1.19.5+dev
Public Types | Public Member Functions | Private Attributes | List of all members
utils::optimer< ResolutionType, ClockType > Struct Template Reference

Reports time elapsed at the end of an object scope. More...

#include <optimer.hpp>

Public Types

using clock = ClockType
 
using resolution = ResolutionType
 
using point = typename clock::time_point
 
using interval = typename clock::duration
 
using report_callback = std::function< void(const optimer &)>
 

Public Member Functions

 optimer (report_callback f=report_callback{})
 Constructor, which starts the timer. More...
 
 ~optimer ()
 Destructor, which invokes the report callback. More...
 
point reset ()
 Resets the timer back to zero and returns the previous tick value. More...
 
point start () const
 Returns the start time point. More...
 
interval elapsed () const
 Returns the elapsed time value. More...
 
interval lap ()
 Resets the starting tick and returns the elapsed time. More...
 

Private Attributes

point start_
 
report_callback repf_
 

Detailed Description

template<typename ResolutionType, typename ClockType = std::chrono::steady_clock>
struct utils::optimer< ResolutionType, ClockType >

Reports time elapsed at the end of an object scope.

The constructor accepts a callable that takes the optimer as its only argument, which can then be used to write the elapsed time to a stream, variable, or anything else that's desirable for a particular use case.

Template Parameters
ResolutionTypeClock resolution – milliseconds, microseconds, etc.
ClockTypeClock type – a monotonic clock by default.

Definition at line 36 of file optimer.hpp.

Member Typedef Documentation

◆ clock

template<typename ResolutionType , typename ClockType = std::chrono::steady_clock>
using utils::optimer< ResolutionType, ClockType >::clock = ClockType

Definition at line 38 of file optimer.hpp.

◆ interval

template<typename ResolutionType , typename ClockType = std::chrono::steady_clock>
using utils::optimer< ResolutionType, ClockType >::interval = typename clock::duration

Definition at line 41 of file optimer.hpp.

◆ point

template<typename ResolutionType , typename ClockType = std::chrono::steady_clock>
using utils::optimer< ResolutionType, ClockType >::point = typename clock::time_point

Definition at line 40 of file optimer.hpp.

◆ report_callback

template<typename ResolutionType , typename ClockType = std::chrono::steady_clock>
using utils::optimer< ResolutionType, ClockType >::report_callback = std::function<void(const optimer&)>

Definition at line 42 of file optimer.hpp.

◆ resolution

template<typename ResolutionType , typename ClockType = std::chrono::steady_clock>
using utils::optimer< ResolutionType, ClockType >::resolution = ResolutionType

Definition at line 39 of file optimer.hpp.

Constructor & Destructor Documentation

◆ optimer()

template<typename ResolutionType , typename ClockType = std::chrono::steady_clock>
utils::optimer< ResolutionType, ClockType >::optimer ( report_callback  f = report_callback{})
inlineexplicit

Constructor, which starts the timer.

The report callback may be an empty object, in which case the destructor will not do anything. This may be useful in order to obtain and keep track of elapsed time manually for other purposes than console printing.

Definition at line 51 of file optimer.hpp.

◆ ~optimer()

template<typename ResolutionType , typename ClockType = std::chrono::steady_clock>
utils::optimer< ResolutionType, ClockType >::~optimer ( )
inline

Destructor, which invokes the report callback.

Definition at line 61 of file optimer.hpp.

References utils::optimer< ResolutionType, ClockType >::repf_.

Member Function Documentation

◆ elapsed()

template<typename ResolutionType , typename ClockType = std::chrono::steady_clock>
interval utils::optimer< ResolutionType, ClockType >::elapsed ( ) const
inline

Returns the elapsed time value.

Definition at line 81 of file optimer.hpp.

References utils::optimer< ResolutionType, ClockType >::start_.

Referenced by utils::operator<<().

◆ lap()

template<typename ResolutionType , typename ClockType = std::chrono::steady_clock>
interval utils::optimer< ResolutionType, ClockType >::lap ( )
inline

Resets the starting tick and returns the elapsed time.

Note
This method is preferred to calling elapsed() followed by reset() since it guarantees resetting to the same tick as is used for duration (or, in other words, clock::now() is called only once.)

Definition at line 93 of file optimer.hpp.

References utils::optimer< ResolutionType, ClockType >::reset(), and utils::optimer< ResolutionType, ClockType >::start_.

◆ reset()

template<typename ResolutionType , typename ClockType = std::chrono::steady_clock>
point utils::optimer< ResolutionType, ClockType >::reset ( )
inline

Resets the timer back to zero and returns the previous tick value.

Definition at line 69 of file optimer.hpp.

References utils::optimer< ResolutionType, ClockType >::start_.

Referenced by utils::optimer< ResolutionType, ClockType >::lap().

◆ start()

template<typename ResolutionType , typename ClockType = std::chrono::steady_clock>
point utils::optimer< ResolutionType, ClockType >::start ( ) const
inline

Returns the start time point.

Definition at line 75 of file optimer.hpp.

References utils::optimer< ResolutionType, ClockType >::start_.

Member Data Documentation

◆ repf_

template<typename ResolutionType , typename ClockType = std::chrono::steady_clock>
report_callback utils::optimer< ResolutionType, ClockType >::repf_
private

Definition at line 101 of file optimer.hpp.

Referenced by utils::optimer< ResolutionType, ClockType >::~optimer().

◆ start_

template<typename ResolutionType , typename ClockType = std::chrono::steady_clock>
point utils::optimer< ResolutionType, ClockType >::start_
private

The documentation for this struct was generated from the following file: