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 16 #ifndef UTILS_EVENT_HANDLER_H 17 #define UTILS_EVENT_HANDLER_H 18 19 #include <cstdint> 20 #include <map> 21 #include <functional> 22 23 namespace OHOS { 24 namespace Utils { 25 26 class EventReactor; 27 28 class EventHandler { 29 public: 30 using Callback = std::function<void()>; 31 32 EventHandler(int fd, EventReactor* r); 33 EventHandler& operator=(const EventHandler&) = delete; 34 EventHandler(const EventHandler&) = delete; 35 EventHandler& operator=(const EventHandler&&) = delete; 36 EventHandler(const EventHandler&&) = delete; ~EventHandler()37 ~EventHandler() {} 38 GetHandle()39 int GetHandle() const { return (fd_); } Events()40 uint32_t Events() const { return (events_); } 41 42 void EnableRead(); 43 void EnableWrite(); 44 void DisableWrite(); 45 void DisableAll(); 46 GetEventReactor()47 const EventReactor* GetEventReactor() const { return reactor_; } 48 SetCloseCallback(const Callback & closeCallback)49 void SetCloseCallback(const Callback& closeCallback) { closeCallback_ = closeCallback; } SetErrorCallback(const Callback & errorCallback)50 void SetErrorCallback(const Callback& errorCallback) { errorCallback_ = errorCallback; } SetReadCallback(const Callback & readCallback)51 void SetReadCallback(const Callback& readCallback) { readCallback_ = readCallback; } SetWriteCallback(const Callback & writeCallback)52 void SetWriteCallback(const Callback& writeCallback) { writeCallback_ = writeCallback; } 53 54 void HandleEvents(uint32_t events); 55 56 private: 57 void Update(); 58 59 private: 60 int fd_; 61 uint32_t events_; 62 EventReactor* reactor_; 63 64 Callback readCallback_; 65 Callback writeCallback_; 66 Callback closeCallback_; 67 Callback errorCallback_; 68 }; 69 70 } 71 } 72 #endif /* UTILS_EVENT_HANDLER_H_ */ 73