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