Overte C++ Documentation
PortableHighResolutionClock.h
1 //
2 // PortableHighResolutionClock.h
3 // libraries/shared/src
4 //
5 // Created by Stephen Birarda on 2015-09-14.
6 // Copyright 2015 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 
12 // I discovered the low-resolution nature of the VC2012 implementation of std::chrono::high_resolution_clock
13 // while debugging a UDT issue with packet receive speeds. That lead to
14 // http://stackoverflow.com/questions/16299029/resolution-of-stdchronohigh-resolution-clock-doesnt-correspond-to-measureme
15 // which is where the implementation of this class is from.
16 
17 #pragma once
18 
19 #ifndef hifi_PortableHighResolutionClock_h
20 #define hifi_PortableHighResolutionClock_h
21 
22 #include <chrono>
23 
24 #include <QtCore/QMetaType>
25 
26 #if defined(_MSC_VER) && _MSC_VER < 1900
27 
28 #ifndef WIN32_LEAN_AND_MEAN
29 #define WIN32_LEAN_AND_MEAN
30 #endif
31 #include <windows.h>
32 
33 // The following struct is not compliant with the HF coding standard, but uses underscores to match the classes
34 // in std::chrono
35 
36 struct win_high_resolution_clock {
37  typedef long long rep;
38  typedef std::nano period;
39  typedef std::chrono::duration<rep, period> duration;
40  typedef std::chrono::time_point<win_high_resolution_clock> time_point;
41  static const bool is_steady = true;
42 
43  static time_point now();
44 };
45 
46 using p_high_resolution_clock = win_high_resolution_clock;
47 
48 #else
49 
50 using p_high_resolution_clock = std::chrono::high_resolution_clock;
51 
52 #endif
53 
54 Q_DECLARE_METATYPE(p_high_resolution_clock::time_point);
55 
56 #if defined(__GNUC__) && !defined(__clang__)
57 __attribute__((unused))
58 #endif
59 static const int timePointMetaTypeID = qRegisterMetaType<p_high_resolution_clock::time_point>();
60 
61 #endif // hifi_PortableHighResolutionClock_h