blob: 28855be922e8171ce90482dccd722996714b5ea8 [file] [log] [blame]
Rolf Badorekef2bf512019-08-20 11:17:15 +03001/*
2 Copyright (c) 2018-2019 Nokia.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
Timo Tietavainena0745d22019-11-28 09:55:22 +020017/*
18 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 * platform project (RICP).
20*/
21
Rolf Badorekef2bf512019-08-20 11:17:15 +030022#ifndef SHAREDDATALAYER_TIMER_HPP_
23#define SHAREDDATALAYER_TIMER_HPP_
24
25#include <map>
26#include <functional>
27#include <chrono>
28
29namespace shareddatalayer
30{
31 class Engine;
32 class TimerFD;
33
34 /**
35 * @brief One shot timer
36 *
37 * Timer is a one shot timer which expires once after being armed.
38 */
39 class Timer
40 {
41 public:
42 using Duration = std::chrono::steady_clock::duration;
43
44 using Callback = std::function<void()>;
45
46 /**
47 * Create a new timer. Each timer is associated with an Engine instance
48 * and the associated Engine instance must exist as long as the timer
49 * exists.
50 *
51 * @param engine The associated Engine instance.
52 */
53 explicit Timer(Engine& engine);
54
55 ~Timer();
56
57 /**
58 * Arm this timer. If already armed, then first disarm and then arm.
59 *
60 * @param duration Duration until this timer expires starting from
61 * <i>now</i>.
62 * @param cb Callback function to be called when this timer
63 * expires. The callback will not be called if this
64 * timer is disarmed or deleted before expiration.
65 *
66 * @see disarm
67 */
68 void arm(const Duration& duration, const Callback& cb);
69
70 /**
71 * Disarm this timer if armed. If not armed, then nothing is done.
72 *
73 * @see arm
74 */
75 void disarm();
76
77 /**
78 * Check if this timer is armed.
79 *
80 * @return <code>true</code>, if this timer is armed, otherwise
81 * <code>false</code>.
82 */
83 bool isArmed() const { return armed; }
84
85 Timer(Timer&&) = delete;
86 Timer(const Timer&) = delete;
87 Timer& operator = (Timer&&) = delete;
88 Timer& operator = (const Timer&) = delete;
89
90 private:
91 friend class TimerFD;
92
93 using Queue = std::multimap<Timer::Duration, std::pair<const Timer*, Timer::Callback>>;
94
95 Engine& engine;
96 bool armed;
97 /* The iterator is valid only while this timer is armed */
98 Queue::iterator iterator;
99 };
100}
101
102#endif