rippled
Loading...
Searching...
No Matches
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 <xrpl/beast/clock/abstract_clock.h>
24#include <xrpl/beast/utility/instrumentation.h>
25
26namespace beast {
27
37template <class Clock>
38class manual_clock : public abstract_clock<Clock>
39{
40public:
41 using typename abstract_clock<Clock>::rep;
44
45private:
47
48public:
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 XRPL_ASSERT(
65 !Clock::is_steady || when >= now_,
66 "beast::manual_clock::set(time_point) : forward input");
67 now_ = when;
68 }
69
71 template <class Integer>
72 void
73 set(Integer seconds_from_epoch)
74 {
75 set(time_point(duration(std::chrono::seconds(seconds_from_epoch))));
76 }
77
79 template <class Rep, class Period>
80 void
82 {
83 XRPL_ASSERT(
84 !Clock::is_steady || (now_ + elapsed) >= now_,
85 "beast::manual_clock::advance(duration) : forward input");
86 now_ += elapsed;
87 }
88
92 {
94 return *this;
95 }
96};
97
98} // namespace beast
99
100#endif
Abstract interface to a clock.
typename Clock::rep rep
typename Clock::time_point time_point
typename Clock::duration duration
Manual clock implementation.
Definition: manual_clock.h:39
manual_clock & operator++()
Convenience for advancing the clock by one second.
Definition: manual_clock.h:91
void set(Integer seconds_from_epoch)
Convenience for setting the time in seconds from epoch.
Definition: manual_clock.h:73
void advance(std::chrono::duration< Rep, Period > const &elapsed)
Advance the clock by a duration.
Definition: manual_clock.h:81
time_point now() const override
Returns the current time.
Definition: manual_clock.h:55
void set(time_point const &when)
Set the current time of the manual clock.
Definition: manual_clock.h:62
manual_clock(time_point const &now=time_point(duration(0)))
Definition: manual_clock.h:49