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 IEVENT_H 17 #define IEVENT_H 18 19 #include "ref_object.h" 20 #include "macro_utils.h" 21 #include "event_fd.h" 22 23 namespace DistributedDB { 24 using EventTime = int64_t; 25 using EventsMask = unsigned int; 26 using EventAction = std::function<int(EventsMask revents)>; 27 using EventFinalizer = std::function<void(void)>; 28 29 class IEvent : public virtual RefObject { 30 public: 31 enum EventType { 32 ET_READ = 0x01, 33 ET_WRITE = 0x02, 34 ET_ERROR = 0x04, 35 ET_TIMEOUT = 0x08, 36 }; 37 38 IEvent() = default; 39 DISABLE_COPY_ASSIGN_MOVE(IEvent); 40 41 virtual int SetAction(const EventAction &action, const EventFinalizer &finalizer = nullptr) = 0; 42 virtual int AddEvents(EventsMask events) = 0; 43 virtual int RemoveEvents(EventsMask events) = 0; 44 virtual int SetTimeout(EventTime timeout) = 0; 45 virtual int Detach(bool wait) = 0; 46 virtual void IgnoreFinalizer() = 0; 47 48 // The following 2 static methods is used to create real event objects, 49 // instead of an event object factory. 50 static IEvent *CreateEvent(EventTime timeout, int &errCode); 51 static IEvent *CreateEvent(EventFd fd, EventsMask events, EventTime timeout, int &errCode); 52 53 protected: ~IEvent()54 virtual ~IEvent() {}; 55 }; 56 } 57 58 #endif // IEVENT_H 59