Skip to content

Instantly share code, notes, and snippets.

@bassosimone
Created March 1, 2018 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bassosimone/2e592f6d57cb3b8ffb74e03b8646af2d to your computer and use it in GitHub Desktop.
Save bassosimone/2e592f6d57cb3b8ffb74e03b8646af2d to your computer and use it in GitHub Desktop.
// 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