Created
March 1, 2018 19:44
-
-
Save bassosimone/2e592f6d57cb3b8ffb74e03b8646af2d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// make templates work in Visual Studio | |
// xref: https://github.com/measurement-kit/measurement-kit/pull/1536 | |
#include <stdlib.h> | |
#include <time.h> | |
#include <iostream> | |
// experiment with several combinations of modifiers | |
#if 1 | |
#define SI static inline | |
#else | |
#define SI // nothing | |
#endif | |
namespace mkp { | |
template <decltype(::timespec_get) func> | |
double MockableNow() noexcept { | |
timespec ts{}; | |
if (func(&ts, TIME_UTC) != TIME_UTC) { | |
abort(); | |
} | |
return ts.tv_sec + (double)ts.tv_nsec / 1'000'000'000; | |
} | |
SI double Now() noexcept { return MockableNow<::timespec_get>(); } | |
template <decltype(Now) func> | |
double MockableMakeDeadline(double timeout) noexcept { | |
return func() + timeout; | |
} | |
SI double MakeDeadline(double timeout) noexcept { | |
return MockableMakeDeadline<Now>(timeout); | |
} | |
} // namespace mkp | |
int main() { | |
std::clog << mkp::MakeDeadline(10.0) << "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment