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