Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of github.com:simgrid/simgrid
[simgrid.git] / include / simgrid / chrono.hpp
1 /* Copyright (c) 2016. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_CHRONO_HPP
7 #define SIMGRID_CHRONO_HPP
8
9 /** @file chrono.hpp Time support
10  *
11  *  Define clock, duration types, time point types compatible with the standard
12  *  C++ library API.
13  */
14
15 #include <chrono>
16 #include <ratio>
17
18 #include <simgrid/simix.h>
19
20 namespace simgrid {
21
22 /** @brief A C++ compatible [TrivialClock](http://en.cppreference.com/w/cpp/concept/TrivialClock) working with simulated-time.
23  *
24  * SimGrid uses `double` for representing the simulated time, where *durations* are expressed in seconds
25  * (with *infinite duration* expressed as a negative value) and
26  * *timepoints* are expressed as seconds from the beginning of the simulation.
27  * In contrast, all the C++ APIs use the much more sensible `std::chrono::duration` and`std::chrono::time_point`.
28  *
29  * This class can be used to build `std::chrono` objects that use simulated time,
30  * using #SimulationClockDuration and #SimulationClockTimePoint.
31  *
32  * This means it is possible to use (since C++14):
33  *
34  * @code{cpp}
35  * using namespace std::chrono_literals;
36  * simgrid::s4u::actor::sleep_for(42s);
37  * @endcode
38  *
39  */
40 struct SimulationClock {
41   using rep        = double;
42   using period     = std::ratio<1>;
43   using duration   = std::chrono::duration<rep, period>;
44   using time_point = std::chrono::time_point<SimulationClock, duration>;
45   static constexpr bool is_steady = true;
46   static time_point now()
47   {
48     return time_point(duration(SIMIX_get_clock()));
49   }
50 };
51
52 /** Default duration for simulated time */
53 using SimulationClockDuration  = SimulationClock::duration;
54
55 /** Default time point for simulated time */
56 using SimulationClockTimePoint = SimulationClock::time_point;
57
58 // Durations based on doubles:
59 using nanoseconds = std::chrono::duration<double, std::nano>;
60 using microseconds = std::chrono::duration<double, std::micro>;
61 using milliseconds = std::chrono::duration<double, std::milli>;
62 using seconds = std::chrono::duration<double>;
63 using minutes = std::chrono::duration<double, std::ratio<60>>;
64 using hours = std::chrono::duration<double, std::ratio<3600>>;
65
66 /** A time point in the simulated time */
67 template<class Duration>
68 using SimulationTimePoint = std::chrono::time_point<SimulationClock, Duration>;
69
70 }
71
72 #endif