Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include "package_worker_factory.h"
12 : #include "tsd_log.h"
13 :
14 : namespace tsd {
15 14 : PackageWorkerFactory& PackageWorkerFactory::GetInstance()
16 : {
17 14 : static PackageWorkerFactory inst;
18 14 : return inst;
19 : }
20 :
21 3 : bool PackageWorkerFactory::RegisterPackageWorker(const PackageWorkerType type, const PackageWorkerCreateFunc& func)
22 : {
23 3 : return PackageWorkerFactory::GetInstance().RegisterPackageWorkerCreator(type, func);
24 : }
25 :
26 3 : bool PackageWorkerFactory::RegisterPackageWorkerCreator(
27 : const PackageWorkerType type, const PackageWorkerCreateFunc& func)
28 : {
29 3 : std::lock_guard<std::mutex> lk(creatorMapMtx_);
30 3 : auto iter = creatorMap_.find(type);
31 3 : if (iter != creatorMap_.end()) {
32 1 : TSD_RUN_WARN("Register[%u] package worker already exist", static_cast<uint32_t>(type));
33 1 : return true;
34 : }
35 :
36 2 : creatorMap_[type] = func;
37 2 : TSD_RUN_INFO("Register[%u] package worker was created successfully", static_cast<uint32_t>(type));
38 2 : return true;
39 3 : }
40 :
41 11 : std::shared_ptr<BasePackageWorker> PackageWorkerFactory::CreatePackageWorker(
42 : const PackageWorkerType type, const PackageWorkerParas paras) const
43 : {
44 11 : const auto iter = creatorMap_.find(type);
45 11 : if (iter == creatorMap_.end()) {
46 1 : TSD_ERROR("Cannot find package worker create func, type=%u", static_cast<uint32_t>(type));
47 1 : return nullptr;
48 : }
49 :
50 10 : const std::shared_ptr<BasePackageWorker> inst = iter->second(paras);
51 10 : if (inst == nullptr) {
52 0 : TSD_ERROR("Create package worker failed by nullptr, type=%u", static_cast<uint32_t>(type));
53 0 : return nullptr;
54 : }
55 :
56 10 : TSD_INFO("Create package worker success, type=%u", static_cast<uint32_t>(type));
57 :
58 10 : return inst;
59 10 : }
60 : } // namespace tsd
|