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 #ifndef HIVIEW_BASE_APP_EVENT_PUBLISHER_FACTORY_H
16 #define HIVIEW_BASE_APP_EVENT_PUBLISHER_FACTORY_H
17 
18 #include <string>
19 #include <unordered_map>
20 
21 #include "defines.h"
22 
23 namespace OHOS {
24 namespace HiviewDFX {
25 class DllExport AppEventPublisherFactory {
26 public:
27     static void RegisterPublisher(const std::string& name);
28     static void UnregisterPublisher(const std::string& name);
29     static bool IsPublisher(const std::string& name);
30 
31 private:
32     static std::shared_ptr<std::unordered_map<std::string, bool>> GetPublisherRegistryMap();
33 };
34 
35 class PublisherRegister {
36 public:
PublisherRegister(const std::string & name)37     PublisherRegister(const std::string& name)
38     {
39         AppEventPublisherFactory::RegisterPublisher(name);
40     };
~PublisherRegister()41     ~PublisherRegister(){};
42 };
43 
44 #define REGISTER_PUBLISHER__(ClassName)                                                        \
45 class RegisterPublisher##ClassName {                                                           \
46     private:                                                                                   \
47         static const PublisherRegister g_staticPublisherRegister;                              \
48 };                                                                                             \
49 const PublisherRegister RegisterPublisher##ClassName::g_staticPublisherRegister(#ClassName);   \
50 
51 #define PUBLISHER_ASSERT(ClassName)                                                            \
52     class AppEventPublisher;                                                                   \
53     static_assert((std::is_base_of<AppEventPublisher, ClassName>::value),                      \
54         "Not a AppEventPublisher Class");
55 
56 #define REGISTER_PUBLISHER(ClassName)                                                          \
57     PUBLISHER_ASSERT(ClassName)                                                                \
58     REGISTER_PUBLISHER__(ClassName)
59 } // namespace HiviewDFX
60 } // namespace OHOS
61 
62 #endif // HIVIEW_BASE_APP_EVENT_PUBLISHER_FACTORY_H