HoverRace  2.0
Clock.h
Go to the documentation of this file.
1 
2 // Clock.h
3 //
4 // Copyright (c) 2013-2015 Michael Imamura.
5 //
6 // Licensed under GrokkSoft HoverRace SourceCode License v1.0(the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // A copy of the license should have been attached to the package from which
10 // you have taken this file. If you can not find the license you can not use
11 // this file.
12 //
13 //
14 // The author makes no representations about the suitability of
15 // this software for any purpose. It is provided "as is" "AS IS",
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17 // implied.
18 //
19 // See the License for the specific language governing permissions
20 // and limitations under the License.
21 
22 #pragma once
23 
24 #include "Duration.h"
25 #include "OS.h"
26 
27 #if defined(_WIN32) && defined(HR_ENGINE_SHARED)
28 # ifdef MR_ENGINE
29 # define MR_DllDeclare __declspec( dllexport )
30 # else
31 # define MR_DllDeclare __declspec( dllimport )
32 # endif
33 #else
34 # define MR_DllDeclare
35 #endif
36 
37 namespace HoverRace {
38 namespace Util {
39 
44 class MR_DllDeclare Clock : public std::enable_shared_from_this<Clock>
45 {
46 public:
47  Clock();
48  Clock(const Duration &init);
49 
50  // We can't copy the alarm signals, so we can't be copyable either.
51  Clock(const Clock&) = delete;
52  Clock(Clock&&) = default;
53  Clock &operator=(const Clock&) = delete;
54  Clock &operator=(Clock&&) = default;
55 
56  virtual ~Clock() { }
57 
58 public:
59  std::string FmtLong() const { return lastRead.FmtLong(); }
60  std::string FmtShort() const { return lastRead.FmtShort(); }
61 
62 public:
66  const Duration &GetTime() const noexcept { return lastRead; }
67  void SetTime(const Duration &duration = {}) noexcept;
68 
69  Duration Advance();
70 
71 public:
72  using alarmSignal_t = boost::signals2::signal<void()>;
73 
77  void ClearAlarms() noexcept
78  {
79  alarms.clear();
80  }
81 
94  template<class Fn>
95  boost::signals2::connection At(const Duration &duration, Fn &&fn)
96  {
97  auto &sig = alarms[duration];
98  if (!sig) {
99  sig.reset(new alarmSignal_t());
100  }
101  return sig->connect(std::forward<Fn>(fn));
102  }
103 
104 private:
108 
109  // Sorted list of pending alarms.
110  // We need to wrap the signal in a unique_ptr because depending on the
111  // Boost version, it may not have move semantics.
112  std::map<Duration, std::unique_ptr<alarmSignal_t>> alarms;
113 };
114 
115 inline std::ostream &operator<<(std::ostream &os, const Clock &clock)
116 {
117  clock.GetTime().FmtLong(os);
118  return os;
119 }
120 
121 } // namespace Util
122 } // namespace HoverRace
123 
124 #undef MR_DllDeclare
const Duration & GetTime() const noexcept
Retrieve the time of the last call to Advance().
Definition: Clock.h:66
Duration lastRead
Definition: Clock.h:105
std::map< Duration, std::unique_ptr< alarmSignal_t > > alarms
Definition: Clock.h:112
std::string FmtLong() const
Definition: Clock.h:59
void ClearAlarms() noexcept
Remove all alarms.
Definition: Clock.h:77
boost::signals2::signal< void()> alarmSignal_t
Definition: Clock.h:72
std::ostream & FmtLong(std::ostream &os) const
Stream out the full-length format.
Definition: Duration.cpp:39
std::ostream & operator<<(std::ostream &os, const Clock &clock)
Definition: Clock.h:115
virtual ~Clock()
Definition: Clock.h:56
Duration offset
Definition: Clock.h:107
A game clock that manages the current time in the simulation.
Definition: Clock.h:44
std::string FmtShort() const
Definition: Clock.h:60
#define MR_DllDeclare
Definition: Clock.h:34
Definition: Announcement.h:24
boost::signals2::connection At(const Duration &duration, Fn &&fn)
Set a one-shot alarm to run at a specific time.
Definition: Clock.h:95
Duration start
Definition: Clock.h:106
The relative time between two timestamps.
Definition: Duration.h:49