rippled
manual_clock.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of Beast: https://github.com/vinniefalco/Beast
4  Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef BEAST_CHRONO_MANUAL_CLOCK_H_INCLUDED
21 #define BEAST_CHRONO_MANUAL_CLOCK_H_INCLUDED
22 
23 #include <ripple/beast/clock/abstract_clock.h>
24 #include <cassert>
25 
26 namespace beast {
27 
37 template <class Clock>
38 class manual_clock : public abstract_clock<Clock>
39 {
40 public:
41  using typename abstract_clock<Clock>::rep;
42  using typename abstract_clock<Clock>::duration;
43  using typename abstract_clock<Clock>::time_point;
44 
45 private:
47 
48 public:
50  : now_(now)
51  {
52  }
53 
55  now() const override
56  {
57  return now_;
58  }
59 
61  void
62  set(time_point const& when)
63  {
64  assert(!Clock::is_steady || when >= now_);
65  now_ = when;
66  }
67 
69  template <class Integer>
70  void
71  set(Integer seconds_from_epoch)
72  {
73  set(time_point(duration(std::chrono::seconds(seconds_from_epoch))));
74  }
75 
77  template <class Rep, class Period>
78  void
80  {
81  assert(!Clock::is_steady || (now_ + elapsed) >= now_);
82  now_ += elapsed;
83  }
84 
88  {
90  return *this;
91  }
92 };
93 
94 } // namespace beast
95 
96 #endif
beast::manual_clock::advance
void advance(std::chrono::duration< Rep, Period > const &elapsed)
Advance the clock by a duration.
Definition: manual_clock.h:79
beast::manual_clock::set
void set(Integer seconds_from_epoch)
Convenience for setting the time in seconds from epoch.
Definition: manual_clock.h:71
std::chrono::seconds
beast::manual_clock::set
void set(time_point const &when)
Set the current time of the manual clock.
Definition: manual_clock.h:62
beast::manual_clock::operator++
manual_clock & operator++()
Convenience for advancing the clock by one second.
Definition: manual_clock.h:87
beast::manual_clock::now_
time_point now_
Definition: manual_clock.h:46
beast::abstract_clock
Abstract interface to a clock.
Definition: abstract_clock.h:57
beast::manual_clock::now
time_point now() const override
Returns the current time.
Definition: manual_clock.h:55
beast::manual_clock::manual_clock
manual_clock(time_point const &now=time_point(duration(0)))
Definition: manual_clock.h:49
cassert
beast::abstract_clock::rep
typename Clock::rep rep
Definition: abstract_clock.h:60
beast::manual_clock
Manual clock implementation.
Definition: manual_clock.h:38
beast::abstract_clock::time_point
typename Clock::time_point time_point
Definition: abstract_clock.h:63
beast::abstract_clock::duration
typename Clock::duration duration
Definition: abstract_clock.h:62
beast
Definition: base_uint.h:641