• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
22 namespace OHOS {
23 namespace Utils {
24 constexpr int INVALID_TIMER_FD = -1;
25 
26 class EventHandler;
27 class EventReactor;
28 
29 class TimerEventHandler {
30     using TimerCallback = std::function<void(int timerFd)>;
31 
32 public:
33     TimerEventHandler(EventReactor* p, uint32_t timeout, bool once);
34     ~TimerEventHandler();
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 
SetTimerCallback(const TimerCallback & callback)44     void SetTimerCallback(const TimerCallback& callback) { callback_ = callback; }
45 
GetInterval()46     uint32_t GetInterval() const { return interval_; }
GetTimerFd()47     int GetTimerFd() const { return timerFd_; }
48 
49 private:
50     void TimeOut();
51 
52 private:
53     bool           once_;
54     int            timerFd_;
55     uint32_t       interval_;
56     EventReactor*  reactor_;
57 
58     std::unique_ptr<EventHandler> handler_;
59     TimerCallback                 callback_;
60 };
61 
62 } // namespace Utils
63 } // namespace OHOS
64 #endif
65