1 /* 2 * Copyright (c) 2025 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 DUMP_INFO_FACTORY_H 16 #define DUMP_INFO_FACTORY_H 17 #include <functional> 18 #include <string> 19 #include <map> 20 #include <memory> 21 #include "dump_info.h" 22 #include "dfx_log.h" 23 namespace OHOS { 24 namespace HiviewDFX { 25 using CreateObjectFunc = std::shared_ptr<DumpInfo>(*)(); 26 class DumpInfoFactory { 27 public: GetInstance()28 static DumpInfoFactory& GetInstance() 29 { 30 static DumpInfoFactory instance; 31 return instance; 32 } 33 DumpInfoFactory(const DumpInfoFactory&) = delete; 34 DumpInfoFactory &operator=(const DumpInfoFactory&) = delete; RegisterClass(const std::string & className,CreateObjectFunc func)35 int RegisterClass(const std::string& className, CreateObjectFunc func) 36 { 37 classMap_[className] = func; 38 return 0; 39 } CreateObject(const std::string & className)40 std::shared_ptr<DumpInfo> CreateObject(const std::string& className) 41 { 42 auto iter = classMap_.find(className); 43 if (iter != classMap_.end()) { 44 return (iter->second)(); 45 } 46 return nullptr; 47 } 48 private: 49 DumpInfoFactory() = default; 50 std::map<std::string, CreateObjectFunc> classMap_; 51 }; 52 53 #define REGISTER_DUMP_INFO_CLASS(ClassName) \ 54 static int instance##ClassName = \ 55 DumpInfoFactory::GetInstance().RegisterClass(#ClassName, ClassName::CreateInstance) 56 } 57 } 58 #endif