1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
2 | // |
3 | // SPDX-License-Identifier: GPL-3.0-or-later |
4 | |
5 | #ifndef OPTIONSERVICE_H |
6 | #define OPTIONSERVICE_H |
7 | |
8 | #include "optiongenerator.h" |
9 | |
10 | #include "base/abstractwidget.h" |
11 | |
12 | #include "framework/framework.h" |
13 | |
14 | namespace dpfservice { |
15 | |
16 | class OptionService final : public dpf::PluginService, |
17 | dpf::AutoServiceRegister<OptionService>, |
18 | dpf::QtClassFactory<OptionGenerator>, |
19 | dpf::QtClassManager<OptionGenerator> |
20 | { |
21 | Q_OBJECT |
22 | Q_DISABLE_COPY(OptionService) |
23 | typedef dpf::QtClassManager<OptionGenerator> GeneratorOptManager; |
24 | typedef dpf::QtClassFactory<OptionGenerator> GeneratorOptFactory; |
25 | public: |
26 | inline static QString name() |
27 | { |
28 | return "org.deepin.service.OptionService" ; |
29 | } |
30 | |
31 | explicit OptionService(QObject *parent = nullptr) |
32 | : dpf::PluginService (parent) |
33 | { |
34 | |
35 | } |
36 | |
37 | /*! |
38 | * \brief supportGenerator 獲取支持的工程名稱 |
39 | * \return |
40 | */ |
41 | template<class T> |
42 | QStringList supportGeneratorName() |
43 | { |
44 | if (std::is_same<OptionGenerator, T>()) |
45 | return GeneratorOptFactory::createKeys(); |
46 | return {}; |
47 | } |
48 | |
49 | /*! |
50 | * \brief implGenerator 導入工程文件生成器對象 T = Project::Generator類泛化對象 |
51 | * \param name 生成器對象名稱(唯一鍵) |
52 | * \param errorString 錯誤信息 |
53 | */ |
54 | template<class T> |
55 | bool implGenerator(const QString &name, QString *errorString = nullptr) |
56 | { |
57 | if (std::is_base_of<OptionGenerator, T>()) |
58 | return GeneratorOptFactory::regClass<T>(name, errorString); |
59 | return false; |
60 | } |
61 | |
62 | /*! |
63 | * \brief createGenerator |
64 | * \param name 名稱 |
65 | * \param errorString 錯誤信息 |
66 | * \return 生成器對象實例 |
67 | */ |
68 | template<class T> |
69 | T *createGenerator(const QString &name, QString *errorString = nullptr) |
70 | { |
71 | if (std::is_base_of<OptionGenerator, T>()) { |
72 | Generator *generator = GeneratorOptManager::value(name); |
73 | if (!generator) { |
74 | generator = GeneratorOptFactory::create(name, errorString); |
75 | if (generator) |
76 | GeneratorOptManager::append(name, dynamic_cast<OptionGenerator*>(generator)); |
77 | } |
78 | return dynamic_cast<T*>(generator); |
79 | } |
80 | return nullptr; |
81 | } |
82 | }; |
83 | } //namespace dpfservice |
84 | |
85 | #endif // OPTIONSERVICE_H |
86 | |