Vanetza
 
Loading...
Searching...
No Matches
manual_runtime.hpp
1#ifndef MANUAL_RUNTIME_HPP_IPFSK6ZA
2#define MANUAL_RUNTIME_HPP_IPFSK6ZA
3
4#include <vanetza/common/runtime.hpp>
5#include <boost/multi_index_container.hpp>
6#include <boost/multi_index/hashed_index.hpp>
7#include <boost/multi_index/member.hpp>
8#include <boost/multi_index/ordered_index.hpp>
9
10namespace vanetza
11{
12
13/**
14 * ManualRuntime is a manually triggered Runtime implementation.
15 * Ensure that time progress is triggered monotonically!
16 */
17class ManualRuntime : public Runtime
18{
19public:
20 ManualRuntime() = default;
21
22 /**
23 * Create runtime
24 * \param init initialization value of internal clock
25 */
26 explicit ManualRuntime(Clock::time_point init);
27
28 /**
29 * Trigger absolute time progress
30 *
31 * All expired callbacks will be invoked
32 * \param tp new time point, has to be greater than now
33 */
34 void trigger(Clock::time_point tp);
35
36 /**
37 * Trigger relative time progress
38 *
39 * All expired callbacks will be invoked
40 * \param d advance time by this duration
41 */
42 void trigger(Clock::duration d);
43
44 /**
45 * Reset runtime
46 *
47 * Drops all scheduled callbacks and resets internal clock
48 * \param tp new time point
49 */
50 void reset(Clock::time_point tp);
51
52 /**
53 * Get time point of next scheduled event
54 * \note time point might belong to an expired event, i.e. next() < now()
55 * \return time point of next event or time_point::max if none
56 */
57 Clock::time_point next() const;
58
59 // Runtime interface (see header there for details)
60 void schedule(Clock::time_point, const Callback&, const void* = nullptr) override;
61 void schedule(Clock::duration, const Callback&, const void* = nullptr) override;
62 void cancel(const void* scope) override;
63 Clock::time_point now() const override;
64
65private:
67 {
68 ScheduledCallback(Clock::time_point tp, const Callback& cb, const void* scope) :
69 deadline(tp), callback(cb), scope(scope) {}
70
71 ScheduledCallback(const ScheduledCallback&) = delete;
72 ScheduledCallback& operator=(const ScheduledCallback&) = delete;
73
75 ScheduledCallback& operator=(ScheduledCallback&&) = default;
76
77 Clock::time_point deadline;
78 Callback callback;
79 const void* scope;
80 };
81
82 struct by_deadline {};
83 using time_index = boost::multi_index::ordered_non_unique<
84 boost::multi_index::tag<by_deadline>,
85 boost::multi_index::member<ScheduledCallback, Clock::time_point, &ScheduledCallback::deadline>>;
86 struct by_scope {};
87 using scope_index = boost::multi_index::hashed_non_unique<
88 boost::multi_index::tag<by_scope>,
89 boost::multi_index::member<ScheduledCallback, const void*, &ScheduledCallback::scope>>;
90 using queue_type = boost::multi_index_container<ScheduledCallback,
91 boost::multi_index::indexed_by<time_index, scope_index>>;
92
93 void trigger();
94
95 Clock::time_point m_now;
96 queue_type m_queue;
97};
98
99} // namespace vanetza
100
101#endif /* MANUAL_RUNTIME_HPP_IPFSK6ZA */
102
void trigger(Clock::time_point tp)
void cancel(const void *scope) override
Clock::time_point next() const
void reset(Clock::time_point tp)
Clock::time_point now() const override
void schedule(Clock::time_point, const Callback &, const void *=nullptr) override