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.
17 #ifndef SHAREDDATALAYER_TIMER_HPP_
18 #define SHAREDDATALAYER_TIMER_HPP_
24 namespace shareddatalayer
30 * @brief One shot timer
32 * Timer is a one shot timer which expires once after being armed.
37 using Duration = std::chrono::steady_clock::duration;
39 using Callback = std::function<void()>;
42 * Create a new timer. Each timer is associated with an Engine instance
43 * and the associated Engine instance must exist as long as the timer
46 * @param engine The associated Engine instance.
48 explicit Timer(Engine& engine);
53 * Arm this timer. If already armed, then first disarm and then arm.
55 * @param duration Duration until this timer expires starting from
57 * @param cb Callback function to be called when this timer
58 * expires. The callback will not be called if this
59 * timer is disarmed or deleted before expiration.
63 void arm(const Duration& duration, const Callback& cb);
66 * Disarm this timer if armed. If not armed, then nothing is done.
73 * Check if this timer is armed.
75 * @return <code>true</code>, if this timer is armed, otherwise
78 bool isArmed() const { return armed; }
80 Timer(Timer&&) = delete;
81 Timer(const Timer&) = delete;
82 Timer& operator = (Timer&&) = delete;
83 Timer& operator = (const Timer&) = delete;
88 using Queue = std::multimap<Timer::Duration, std::pair<const Timer*, Timer::Callback>>;
92 /* The iterator is valid only while this timer is armed */
93 Queue::iterator iterator;