1 /* 2 * Copyright (c) 2024 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 EMITTER_H 17 #define EMITTER_H 18 19 #include "emitter_log.h" 20 #include "emitter_common.h" 21 22 namespace OHOS::EventsEmitter { 23 class CallbackImpl { 24 public: 25 CallbackImpl(std::string name, std::function<void(CEventData)> callback); 26 27 std::string name; 28 std::function<void(CEventData)> callback = nullptr; 29 }; 30 31 struct CallbackInfo { 32 std::atomic<bool> once = false; 33 std::shared_ptr<CallbackImpl> callbackImpl = nullptr; 34 ~CallbackInfo(); 35 }; 36 37 class Emitter { 38 public: 39 static int32_t On(uint32_t eventId, std::shared_ptr<CallbackImpl> &callback); 40 41 static int32_t On(char* eventId, std::shared_ptr<CallbackImpl> &callback); 42 43 static int32_t Once(uint32_t eventId, std::shared_ptr<CallbackImpl> &callback); 44 45 static int32_t Once(char* eventId, std::shared_ptr<CallbackImpl> &callback); 46 47 static void Off(uint32_t eventId); 48 49 static void Off(char* eventId); 50 51 static void Off(uint32_t eventId, std::shared_ptr<CallbackImpl> &callback); 52 53 static void Off(char* eventId, std::shared_ptr<CallbackImpl> &callback); 54 55 static void Emit(uint32_t eventId, uint32_t priority, CEventData data); 56 57 static void Emit(char* eventId, uint32_t priority, CEventData data); 58 59 static uint32_t GetListenerCount(uint32_t eventId); 60 61 static uint32_t GetListenerCount(std::string eventId); 62 }; 63 } 64 65 #endif