1/* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15#ifndef UTILS_TIMER_EVENT_HANDLER_H 16#define UTILS_TIMER_EVENT_HANDLER_H 17 18#include <functional> 19#include <memory> 20#include <cstdint> 21#include "event_handler.h" 22 23namespace OHOS { 24namespace Utils { 25constexpr int INVALID_TIMER_FD = -1; 26 27class EventReactor; 28 29class TimerEventHandler : public EventHandler { 30 using TimerCallback = std::function<void(int timerFd)>; 31 32public: 33 TimerEventHandler(EventReactor* p, uint32_t timeout, bool once); 34 ~TimerEventHandler() override; 35 36 TimerEventHandler(const TimerEventHandler&) = delete; 37 TimerEventHandler& operator=(const TimerEventHandler&) = delete; 38 TimerEventHandler(const TimerEventHandler&&) = delete; 39 TimerEventHandler& operator=(const TimerEventHandler&&) = delete; 40 41 uint32_t Initialize(); 42 void Uninitialize(); 43 44 void SetTimerCallback(const TimerCallback& callback) { callback_ = callback; } 45 46 uint32_t GetInterval() const { return interval_; } 47 48private: 49 void TimeOut(); 50 51private: 52 bool once_; 53 uint32_t interval_; 54 TimerCallback callback_; 55}; 56 57} // namespace Utils 58} // namespace OHOS 59#endif 60