-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtility.h
241 lines (210 loc) · 6.04 KB
/
Utility.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#pragma once
#ifdef USE_GCC
#include <sys/time.h>
#endif
#include"BaseLib/CommonDefines.h"
#include"BaseLib/Export.h"
namespace BaseLib
{
/**
* Mac OS X does not have CLOCK_REALTIME defined, so I define it here to make code compile.
*
* TODO: How can be sure this does not have serious drawbacks?
*/
//#ifdef MACX
//#define CLOCK_REALTIME 0
//#endif
/**
* @brief The Utility class provides some platform independent functions.
*/
class DLL_STATE Utility
{
private:
Utility();
public:
/**
* Measure how much tima function call takes. Uses "Execute around object" software design pattern.
*/
// template <typename Functor, typename ... Args>
// auto measure(Functor f, Args && ... args)
// -> decltype(f(std::forward<Args>(args)...))
// {
// struct scoped_timer
// {
// scoped_timer() : now_(std::chrono::high_resolution_clock::now()) {}
// ~scoped_timer()
// {
// auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - now_).count();
// std::cout << "Time elapsed: " << elapsed << "ms" << std::endl;
// }
// private:
// std::chrono::high_resolution_clock::time_point const now_;
// } scoped_timer;
// return f(std::forward<Args>(args)...);
// }
/**
* @brief GenerateUUID generates a universally unique id (UUID) aka. globally unique id (GUID).
*
* @param length defines the length of the returned UUID
* @return a UUID
*/
static std::vector<char> GenerateUUID(unsigned int length = 16);
/**
* @brief GetTypeName generates a typename based on type T
* @return TYPE_NAME(T)
*/
template <typename T>
static std::string GetTypeName()
{
return TYPE_NAME(T);
}
/**
* TODO: This only checks if T is
*/
template<typename Base, typename T>
static bool instanceof(T* t)
{
return dynamic_cast<Base*>(t) != nullptr;
//return std::is_base_of<Base, T>::value;
}
template <typename T, typename V>
static std::shared_ptr<T> To(std::shared_ptr<V> value)
{
return std::dynamic_pointer_cast<T>(value);
}
/**
* @brief ProcessorClockTime
*
* Returns the processor time consumed by the program.
* The value returned is expressed in clock ticks, which are units of time of a constant
* but system-specific length (with a relation of CLOCKS_PER_SEC clock ticks per second).
*
* The epoch used as reference by clock varies between systems, but it is related to the
* program execution (generally its launch). To calculate the actual processing time of a program,
* the value returned by clock shall be compared to a value returned by a previous call to the same function.
*
* @return
*/
static clock_t ProcessorClockTime();
/**
* @brief ComputeElapsedProcessorClockMS
* @param startTime
* @return
*/
static int64 ComputeElapsedProcessorClockMS(int64 startTime);
/**
* @brief ComputeElapsedProcessorClockMsec
* @param startTime
* @return
*/
static int64 ComputeElapsedProcessorClockMsec(int64 startTime);
/**
* @brief getCurrentTimeMs
*
* @return time in msec since epoch
*/
static int64 GetCurrentTimeMs();
/**
* @brief PrintCurrentTimeMs
* @return
*/
static std::string PrintCurrentTimeMsec();
/**
* @brief GetCurrentTimeSec
*
* @return time in seconds since epoch
*/
static int64 GetCurrentTimeSec();
/**
* @brief ClockInitWaitTimevalMsec
* @param tv
* @param msecs
* @return
*/
static int ClockInitWaitTimevalMsec(struct timeval &tv, uint64 msecs);
/**
* @brief ClockInitWaitTimevalUsec
* @param tv
* @param usecs
* @return
*/
static int ClockInitWaitTimevalUsec(struct timeval &tv, uint64 usecs);
/**
* @brief clockInitWaitTimespecMsec
*
* @param ts
* @param nsecs
* @return
*/
static int ClockInitWaitTimespecMsec(struct timespec &ts, uint64 msecs);
/**
* @brief clockInitWaitTimespecUsec
*
* @param ts
* @param nsecs
* @return
*/
static int ClockInitWaitTimespecUsec(struct timespec &ts, uint64 usecs);
/**
* @brief clockInitWaitTimespecNsec
*
* @param ts
* @param nsecs
* @return
*/
static int ClockInitWaitTimespecNsec(struct timespec &ts, uint64 nsecs);
/**
* @brief clockInitWaitTimeMsec
*
* @param ts
* @param msecs
* @return 0 if success
*/
static int ClockInitEpochWaitTimeMsec(struct timespec &ts, uint64 msecs);
/**
* @brief clockInitWaitTimeUsec
*
* @param ts
* @param usecs
* @return 0 if success
*/
static int ClockInitEpochWaitTimeUsec(struct timespec &ts, uint64 usecs);
/**
* @brief clockInitWaitTimeNsec
*
* @param ts
* @param nsecs
* @return 0 if success
*/
static int ClockInitEpochWaitTimeNsec(struct timespec &ts, uint64 nsecs);
/**
* @brief clockGetRealTime
*
* Mac OS X does not support clock_gettime, so this function hides the fact. In addition, CLOCK_REALTIME
* is not found on Mac OS X either, so I simply create a specialized function for this.
*
* Windows has its own implementation.
*
* @param typeœ
* @param ts
* @return 0 if success
*/
static int ClockCurrentTime(struct timespec &ts);
/**
* @brief pthread_timedjoin
*
* A portable implementation of pthread_timedjoin_np. It includes a "hack" of a function to support
* the timedjoin on Mac OS X. Windows has its own implementation.
*
*
* @param td
* @param res
* @param ts
* @return
*/
#ifndef WIN32
static int pthread_timedjoin(pthread_t td, void **res, struct timespec *ts);
static int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const timespec *ts);
#endif
};
}