AGX Dynamics  2.37.3.2
TimerBlock.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #pragma once
18 
19 #include <agx/Timer.h>
20 #include <agx/String.h>
21 
22 namespace agxUtil
23 {
28  class TimerBlock
29  {
30  public:
31  using OnDeleteCallback = std::function<void( agx::UInt64, agx::UInt64 )>;
32 
33  public:
51  template<typename... ArgsT>
52  static TimerBlock create( const agx::String& format, ArgsT&&... args )
53  {
54  agx::String printout = agx::String::format( format.c_str(), std::forward<ArgsT>( args )... );
55  return TimerBlock( [printout]( agx::UInt64 start, agx::UInt64 stop )
56  {
57  std::cout << "{Timer Block}: " << printout << std::endl;
58  std::cout << " - Execute time: " << agx::Timer::convertToMs( stop - start ) << " ms." << std::endl;
59  } );
60  }
61 
62  public:
68  inline TimerBlock( OnDeleteCallback onDeleteCallback )
69  : m_onDeleteCallback( onDeleteCallback ),
70  m_start( agx::Timer::getCurrentTick() ),
71  m_stop( agx::InvalidIndex )
72  {
73  }
74 
78  inline ~TimerBlock()
79  {
80  if ( m_stop == agx::InvalidIndex )
81  stop();
82 
83  if ( m_onDeleteCallback )
84  m_onDeleteCallback( m_start, m_stop );
85  }
86 
90  inline void stop()
91  {
92  m_stop = agx::Timer::getCurrentTick();
93  }
94 
95  private:
96  OnDeleteCallback m_onDeleteCallback;
97  agx::UInt64 m_start;
98  agx::UInt64 m_stop;
99  };
100 }
Utility class to time a scope.
Definition: TimerBlock.h:28
std::function< void(agx::UInt64, agx::UInt64)> OnDeleteCallback
Definition: TimerBlock.h:31
AGXCORE_EXPORT const InvalidIndexStruct InvalidIndex
static TimerBlock create(const agx::String &format, ArgsT &&... args)
Create timer block given format string.
Definition: TimerBlock.h:52
static UInt64 getCurrentTick()
Definition: Timer.h:256
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
void stop()
Stop the timer before we leave the scope.
Definition: TimerBlock.h:90
~TimerBlock()
Print during destruction.
Definition: TimerBlock.h:78
TimerBlock(OnDeleteCallback onDeleteCallback)
Construct given title.
Definition: TimerBlock.h:68
uint64_t UInt64
Definition: Integer.h:41
static Real64 convertToMs(UInt64 ticks)
Definition: Timer.h:312
The agxUtil namespace contain classes and methods for utility functionality.
static AgXString format(const char *format,...)
C printf formatting of a string.
Definition: String.h:471