• 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 public:
32     TimerEventHandler(EventReactor* p, uint32_t timeout, bool once);
33     ~TimerEventHandler();
34 
35     TimerEventHandler(const TimerEventHandler&) = delete;
36     TimerEventHandler& operator=(const TimerEventHandler&) = delete;
37     TimerEventHandler(const TimerEventHandler&&) = delete;
38     TimerEventHandler& operator=(const TimerEventHandler&&) = delete;
39 
40     uint32_t Initialize();
41     void Uninitialize();
42 
SetTimerCallback(const TimerCallback & callback)43     void SetTimerCallback(const TimerCallback& callback) { callback_ = callback; }
44 
GetInterval()45     uint32_t GetInterval() const { return interval_; }
GetTimerFd()46     int GetTimerFd() const { return timerFd_; }
47 
48 private:
49     void TimeOut();
50 
51 private:
52     bool           once_;
53     int            timerFd_;
54     uint32_t       interval_;
55     EventReactor*  reactor_;
56 
57     std::unique_ptr<EventHandler> handler_;
58     TimerCallback                 callback_;
59 };
60 
61 } // namespace Utils
62 } // namespace OHOS
63 #endif
64