2 Copyright (c) 2018-2019 Nokia.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
18 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 * platform project (RICP).
22 #ifndef SHAREDDATALAYER_TIMER_HPP_
23 #define SHAREDDATALAYER_TIMER_HPP_
29 namespace shareddatalayer
35 * @brief One shot timer
37 * Timer is a one shot timer which expires once after being armed.
42 using Duration = std::chrono::steady_clock::duration;
44 using Callback = std::function<void()>;
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
51 * @param engine The associated Engine instance.
53 explicit Timer(Engine& engine);
58 * Arm this timer. If already armed, then first disarm and then arm.
60 * @param duration Duration until this timer expires starting from
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.
68 void arm(const Duration& duration, const Callback& cb);
71 * Disarm this timer if armed. If not armed, then nothing is done.
78 * Check if this timer is armed.
80 * @return <code>true</code>, if this timer is armed, otherwise
83 bool isArmed() const { return armed; }
85 Timer(Timer&&) = delete;
86 Timer(const Timer&) = delete;
87 Timer& operator = (Timer&&) = delete;
88 Timer& operator = (const Timer&) = delete;
93 using Queue = std::multimap<Timer::Duration, std::pair<const Timer*, Timer::Callback>>;
97 /* The iterator is valid only while this timer is armed */
98 Queue::iterator iterator;