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 <functional> 21#include <map> 22#include <memory> 23#include <unistd.h> 24 25namespace OHOS { 26namespace Utils { 27 28class EventReactor; 29 30class EventHandler : public std::enable_shared_from_this<EventHandler> { 31public: 32 using Callback = std::function<void()>; 33 34 EventHandler(int fd, EventReactor* r); 35 EventHandler& operator=(const EventHandler&) = delete; 36 EventHandler(const EventHandler&) = delete; 37 EventHandler& operator=(const EventHandler&&) = delete; 38 EventHandler(const EventHandler&&) = delete; 39 virtual ~EventHandler() {} 40 41 int GetHandle() const { return (fd_); } 42 void SetHandle(int fd) { fd_ = fd; } 43 uint32_t Events() const { return (events_); } 44 45 void EnableRead(); 46 void DisableAll(); 47 48 const EventReactor* GetEventReactor() const { return reactor_; } 49 50 void SetReadCallback(const Callback& readCallback) { readCallback_ = readCallback; } 51 52 void HandleEvents(uint32_t events); 53 54private: 55 void Update(); 56 57private: 58 int fd_; 59 uint32_t events_; 60 EventReactor* reactor_; 61 62 Callback readCallback_; 63}; 64 65} 66} 67#endif /* UTILS_EVENT_HANDLER_H_ */ 68