13f4cbf05Sopenharmony_ci/* 23f4cbf05Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 33f4cbf05Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43f4cbf05Sopenharmony_ci * you may not use this file except in compliance with the License. 53f4cbf05Sopenharmony_ci * You may obtain a copy of the License at 63f4cbf05Sopenharmony_ci * 73f4cbf05Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83f4cbf05Sopenharmony_ci * 93f4cbf05Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103f4cbf05Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113f4cbf05Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123f4cbf05Sopenharmony_ci * See the License for the specific language governing permissions and 133f4cbf05Sopenharmony_ci * limitations under the License. 143f4cbf05Sopenharmony_ci */ 153f4cbf05Sopenharmony_ci 163f4cbf05Sopenharmony_ci#ifndef UTILS_EVENT_HANDLER_H 173f4cbf05Sopenharmony_ci#define UTILS_EVENT_HANDLER_H 183f4cbf05Sopenharmony_ci 193f4cbf05Sopenharmony_ci#include "errors.h" 203f4cbf05Sopenharmony_ci#include "io_event_common.h" 213f4cbf05Sopenharmony_cinamespace OHOS { 223f4cbf05Sopenharmony_cinamespace Utils { 233f4cbf05Sopenharmony_ci 243f4cbf05Sopenharmony_ciclass IOEventReactor; 253f4cbf05Sopenharmony_ci 263f4cbf05Sopenharmony_ciclass IOEventHandler { 273f4cbf05Sopenharmony_cipublic: 283f4cbf05Sopenharmony_ci IOEventHandler(); 293f4cbf05Sopenharmony_ci explicit IOEventHandler(int fd, EventId events = Events::EVENT_NONE, const EventCallback& cb = nullptr); 303f4cbf05Sopenharmony_ci IOEventHandler& operator=(const IOEventHandler&) = delete; 313f4cbf05Sopenharmony_ci IOEventHandler(const IOEventHandler&) = delete; 323f4cbf05Sopenharmony_ci IOEventHandler& operator=(const IOEventHandler&&) = delete; 333f4cbf05Sopenharmony_ci IOEventHandler(const IOEventHandler&&) = delete; 343f4cbf05Sopenharmony_ci virtual ~IOEventHandler(); 353f4cbf05Sopenharmony_ci 363f4cbf05Sopenharmony_ci bool Start(IOEventReactor* reactor); 373f4cbf05Sopenharmony_ci bool Stop(IOEventReactor* reactor); 383f4cbf05Sopenharmony_ci bool Update(IOEventReactor* reactor); 393f4cbf05Sopenharmony_ci 403f4cbf05Sopenharmony_ci inline void SetFd(int fd) 413f4cbf05Sopenharmony_ci { 423f4cbf05Sopenharmony_ci fd_ = fd; 433f4cbf05Sopenharmony_ci } 443f4cbf05Sopenharmony_ci 453f4cbf05Sopenharmony_ci inline void SetEvents(EventId events) 463f4cbf05Sopenharmony_ci { 473f4cbf05Sopenharmony_ci events_ = events; 483f4cbf05Sopenharmony_ci } 493f4cbf05Sopenharmony_ci 503f4cbf05Sopenharmony_ci inline void SetCallback(const EventCallback& cb) 513f4cbf05Sopenharmony_ci { 523f4cbf05Sopenharmony_ci cb_ = cb; 533f4cbf05Sopenharmony_ci } 543f4cbf05Sopenharmony_ci 553f4cbf05Sopenharmony_ci inline int GetFd() const 563f4cbf05Sopenharmony_ci { 573f4cbf05Sopenharmony_ci return fd_; 583f4cbf05Sopenharmony_ci } 593f4cbf05Sopenharmony_ci 603f4cbf05Sopenharmony_ci inline EventId GetEvents() const 613f4cbf05Sopenharmony_ci { 623f4cbf05Sopenharmony_ci return events_; 633f4cbf05Sopenharmony_ci } 643f4cbf05Sopenharmony_ci 653f4cbf05Sopenharmony_ci inline EventCallback GetCallback() const 663f4cbf05Sopenharmony_ci { 673f4cbf05Sopenharmony_ci return cb_; 683f4cbf05Sopenharmony_ci } 693f4cbf05Sopenharmony_ci 703f4cbf05Sopenharmony_ci inline IOEventHandler* Prev() const 713f4cbf05Sopenharmony_ci { 723f4cbf05Sopenharmony_ci return prev_; 733f4cbf05Sopenharmony_ci } 743f4cbf05Sopenharmony_ci 753f4cbf05Sopenharmony_ci inline IOEventHandler* Next() const 763f4cbf05Sopenharmony_ci { 773f4cbf05Sopenharmony_ci return next_; 783f4cbf05Sopenharmony_ci } 793f4cbf05Sopenharmony_ci 803f4cbf05Sopenharmony_ci void EnableRead(); 813f4cbf05Sopenharmony_ci void EnableWrite(); 823f4cbf05Sopenharmony_ci void DisableWrite(); 833f4cbf05Sopenharmony_ci void DisableAll(); 843f4cbf05Sopenharmony_ci 853f4cbf05Sopenharmony_ci inline bool IsActive() 863f4cbf05Sopenharmony_ci { 873f4cbf05Sopenharmony_ci return (prev_ != nullptr && enabled_); 883f4cbf05Sopenharmony_ci } 893f4cbf05Sopenharmony_ciprivate: 903f4cbf05Sopenharmony_ci IOEventHandler* prev_; 913f4cbf05Sopenharmony_ci IOEventHandler* next_; 923f4cbf05Sopenharmony_ci 933f4cbf05Sopenharmony_ci int fd_; 943f4cbf05Sopenharmony_ci EventId events_; 953f4cbf05Sopenharmony_ci EventCallback cb_; 963f4cbf05Sopenharmony_ci bool enabled_; 973f4cbf05Sopenharmony_ci 983f4cbf05Sopenharmony_ci friend class IOEventReactor; 993f4cbf05Sopenharmony_ci}; 1003f4cbf05Sopenharmony_ci 1013f4cbf05Sopenharmony_ci} // namespace Utils 1023f4cbf05Sopenharmony_ci} // namespace OHOS 1033f4cbf05Sopenharmony_ci#endif /* UTILS_EVENT_HANDLER_H_ */ 104