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::atomic<bool> isDeleted = false;
34     std::atomic<bool> processed = false;
35     CallbackImpl *callbackImpl = nullptr;
36     ~CallbackInfo();
37 };
38 
39 class Emitter {
40 public:
41     static int32_t On(uint32_t eventId, CallbackImpl *callback);
42 
43     static int32_t On(char* eventId, CallbackImpl *callback);
44 
45     static int32_t Once(uint32_t eventId, CallbackImpl *callback);
46 
47     static int32_t Once(char* eventId, CallbackImpl *callback);
48 
49     static void Off(uint32_t eventId);
50 
51     static void Off(char* eventId);
52 
53     static void Off(uint32_t eventId, CallbackImpl *callback);
54 
55     static void Off(char* eventId, CallbackImpl *callback);
56 
57     static void Emit(uint32_t eventId, uint32_t priority, CEventData data);
58 
59     static void Emit(char* eventId, uint32_t priority, CEventData data);
60 
61     static uint32_t GetListenerCount(uint32_t eventId);
62 
63     static uint32_t GetListenerCount(std::string eventId);
64 };
65 }
66 
67 #endif