OpenLexocad  28.0
Timer.h
Go to the documentation of this file.
1 #pragma once
2 #include <map>
3 #include <set>
4 #include <string>
5 
6 
7 #define PP_CAT(a, b) PP_CAT_I(a, b)
8 #define PP_CAT_I(a, b) PP_CAT_II(~, a##b)
9 #define PP_CAT_II(p, res) res
10 
11 #define PP_UNIQUE_NAME(base) PP_CAT(base, __COUNTER__)
12 
13 #define PROFILECLEAR \
14  { \
15  Base::Timer::clearAll(); \
16  }
17 #define PROFILEENABLE(a) \
18  { \
19  Base::Timer::enable(a); \
20  }
21 #define PROFILESTART(a) \
22  { \
23  Base::Timer::profileStart(a); \
24  }
25 #define PROFILESTOP(a) \
26  { \
27  Base::Timer::profileStop(a); \
28  }
29 #define PROFILESCOPED(a) Base::scopedProfile PP_UNIQUE_NAME(scopedProfile__)(a);
30 
31 
32 namespace Base
33 {
34 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
35 
36 class LX_BASE_EXPORT timerIFace
37 {
38 public:
39  virtual void resetTotal() = 0;
40  virtual void reset() = 0;
41  virtual double elapsedMS() = 0;
42  virtual double elapsed() const = 0;
43  virtual double start() = 0;
44  virtual void restart() = 0;
45  virtual double stop() = 0;
46  virtual double totalSeconds() = 0;
47  virtual double totalMS() = 0;
48  virtual long count() = 0;
49  virtual std::string getName() = 0;
50  virtual timerIFace* getParent() = 0;
51  virtual void setParent(timerIFace*) = 0;
52  virtual void addChild(timerIFace*) = 0;
53  virtual std::set<timerIFace*> getChildren() = 0;
54 };
55 
56 class LX_BASE_EXPORT timer_dummy : public timerIFace
57 {
58 public:
59  timer_dummy(){};
60  void resetTotal(){};
61  void reset(){};
62  double elapsedMS() { return 0.0; };
63  double elapsed() const { return 0.0; };
64  double start() { return 0.0; };
65  void restart(){};
66  double stop() { return 0.0; };
67  double totalSeconds() { return 0.0; };
68  double totalMS() { return 0.0; };
69  long count() { return 0; };
70  std::string getName() { return std::string(); };
71  timerIFace* getParent() { return 0; };
72  void setParent(timerIFace*){};
73  void addChild(timerIFace*){};
74  std::set<timerIFace*> getChildren() { return std::set<timerIFace*>(); };
75 };
76 
77 class TimerP;
78 
79 class LX_BASE_EXPORT Timer : public timerIFace
80 {
81 public:
82  Timer();
83  Timer(std::string n);
84  ~Timer();
85  void resetTotal();
86  void reset();
87  double elapsedMS();
88  double elapsed() const;
89  double start();
90  void restart();
91  double stop();
92  double totalSeconds();
93  double totalMS();
94  long count();
95  double createdTime();
96  std::string getName() { return _name; };
97  void setParent(timerIFace*);
98  timerIFace* getParent();
99  ;
100 
101  void addChild(timerIFace* c) { _children.insert(c); };
102  std::set<timerIFace*> getChildren() { return _children; };
103 
104  static timerIFace* getTimer(std::string name);
105  static void stopTimer(std::string name);
106  static std::map<std::string, timerIFace*> getTimerMap();
107  static void setCurrent(timerIFace* p);
108  static timerIFace* getCurrent();
109 
110  static void clearAll();
111  double startTiming();
112  static std::map<void*, double> TimeStorage;
113 
114  static void enable(bool v);
115  static bool isEnabled();
116 
117 
118  static void profileStart(const char* a);
119  static void profileStop(const char* a);
120 
121  static void printTimer(const char* header);
122 
123 
124 
125 private:
126  TimerP* _pimpl;
127  double _totalseconds;
128  long _count;
129  std::string _name;
130  timerIFace* _parentTimer = 0;
131  std::set<timerIFace*> _children;
132  static timerIFace* _currentTimer;
133  static bool _enabled;
134  static std::map<std::string, timerIFace*> _timerMap;
135 };
136 
137 static Base::timer_dummy my_timer_dummy;
138 static Base::timer_dummy* my_timer_dummy_ptr = &my_timer_dummy;
139 
140 #else
141 
142 class LX_BASE_EXPORT timer
143 {
144  clock_t _start_time;
145 
146 public:
147  timer() { _start_time = clock(); }
148  void restart() { _start_time = clock(); }
149  double elapsed() const { return double(clock() - _start_time) / CLOCKS_PER_SEC; }
150 };
151 
152 #endif
153 
154 
155 class LX_BASE_EXPORT scopedProfile
156 {
157 public:
158  scopedProfile(const char* a) : m_name(a) { PROFILESTART(a); }
159  ~scopedProfile() { PROFILESTOP(m_name.c_str()); }
160 
161  std::string m_name;
162 };
163 
164 
165 } // namespace Base
PROFILESTART
#define PROFILESTART(a)
Definition: Timer.h:21
Base::scopedProfile
Definition: Timer.h:156
Base::timer::restart
void restart()
Definition: Timer.h:148
Base::timer::timer
timer()
Definition: Timer.h:147
Base::timer::elapsed
double elapsed() const
Definition: Timer.h:149
Base::scopedProfile::~scopedProfile
~scopedProfile()
Definition: Timer.h:159
PROFILESTOP
#define PROFILESTOP(a)
Definition: Timer.h:25
Base::scopedProfile::m_name
std::string m_name
Definition: Timer.h:161
Base::timer
Definition: Timer.h:143
Base::scopedProfile::scopedProfile
scopedProfile(const char *a)
Definition: Timer.h:158
Base
Definition: AbstractXMLReader.h:5