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 #define WIN32_LEAN_AND_MEAN
29 #include <windows.h>
30 
31 // The following struct is not compliant with the HF coding standard, but uses underscores to match the classes
32 // in std::chrono
33 
34 struct win_high_resolution_clock {
35  typedef long long rep;
36  typedef std::nano period;
37  typedef std::chrono::duration<rep, period> duration;
38  typedef std::chrono::time_point<win_high_resolution_clock> time_point;
39  static const bool is_steady = true;
40 
41  static time_point now();
42 };
43 
44 using p_high_resolution_clock = win_high_resolution_clock;
45 
46 #else
47 
48 using p_high_resolution_clock = std::chrono::high_resolution_clock;
49 
50 #endif
51 
52 Q_DECLARE_METATYPE(p_high_resolution_clock::time_point);
53 
54 #if defined(__GNUC__) && !defined(__clang__)
55 __attribute__((unused))
56 #endif
57 static const int timePointMetaTypeID = qRegisterMetaType<p_high_resolution_clock::time_point>();
58 
59 #endif // hifi_PortableHighResolutionClock_h