Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Convert xbt_cfg_set_as_string -> simgrid::config::set_as_string
[simgrid.git] / include / simgrid / chrono.hpp
1 /* Copyright (c) 2016-2018. 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 class SimulationClock {
41 public:
42   using rep        = double;
43   using period     = std::ratio<1>;
44   using duration   = std::chrono::duration<rep, period>;
45   using time_point = std::chrono::time_point<SimulationClock, duration>;
46   static constexpr bool is_steady = true;
47   static time_point now()
48   {
49     return time_point(duration(SIMIX_get_clock()));
50   }
51 };
52
53 /** Default duration for simulated time */
54 using SimulationClockDuration  = SimulationClock::duration;
55
56 /** Default time point for simulated time */
57 using SimulationClockTimePoint = SimulationClock::time_point;
58
59 // Durations based on doubles:
60 using nanoseconds = std::chrono::duration<double, std::nano>;
61 using microseconds = std::chrono::duration<double, std::micro>;
62 using milliseconds = std::chrono::duration<double, std::milli>;
63 using seconds = std::chrono::duration<double>;
64 using minutes = std::chrono::duration<double, std::ratio<60>>;
65 using hours = std::chrono::duration<double, std::ratio<3600>>;
66
67 /** A time point in the simulated time */
68 template<class Duration>
69 using SimulationTimePoint = std::chrono::time_point<SimulationClock, Duration>;
70
71 }
72
73 #endif