1 /*
2 * Copyright (c) 2022 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 UPDATER_UI_COMPONENT_FACTORY_H
17 #define UPDATER_UI_COMPONENT_FACTORY_H
18
19 #include <functional>
20 #include <memory>
21 #include <string>
22 #include <tuple>
23 #include <variant>
24 #include "box_progress_adapter.h"
25 #include "component_common.h"
26 #include "img_view_adapter.h"
27 #include "label_btn_adapter.h"
28 #include "text_label_adapter.h"
29 #ifdef COMPONENT_EXT_INCLUDE
30 #include COMPONENT_EXT_INCLUDE
31 #endif
32
33 /*
34 * all supported component types listed here, you
35 * should add a template argument when add a new
36 * component type
37 */
38 #ifndef COMPONENT_EXT_TYPE_LIST
39 #define COMPONENT_TYPE_LIST BoxProgressAdapter, ImgViewAdapter, TextLabelAdapter, LabelBtnAdapter
40 #else
41 #define COMPONENT_TYPE_LIST BoxProgressAdapter, ImgViewAdapter, TextLabelAdapter, LabelBtnAdapter \
42 COMPONENT_EXT_TYPE_LIST
43 #endif
44
45 namespace Updater {
46 template<typename ... Components>
47 struct SpecificInfo {
48 using Type = std::variant<typename Components::SpecificInfoType...>;
49 };
50
51 class ComponentFactory final {
52 public:
53 static std::unique_ptr<ComponentInterface> CreateUxComponent(const UxViewInfo &info);
54 static bool CheckUxComponent(const UxViewInfo &info);
55 };
56
57 using SpecificInfoFunc = std::function<SpecificInfo<COMPONENT_TYPE_LIST>::Type()>;
58
59 template<typename ... Components>
GetSpecificInfoMap()60 const auto &GetSpecificInfoMap()
61 {
62 const static std::unordered_map<std::string, SpecificInfoFunc> specificInfoMap {
63 { Components::COMPONENT_TYPE, [] () { return typename Components::SpecificInfoType {}; }}...
64 };
65 return specificInfoMap;
66 }
67
68 struct UxViewInfo {
69 UxViewCommonInfo commonInfo {};
70 SpecificInfo<COMPONENT_TYPE_LIST>::Type specificInfo {};
71 };
72
73 namespace Detail {
74 template<typename T, typename ... Components>
75 inline constexpr bool CHECK_COMPONENT_LIST = (std::is_same_v<T, Components> || ...);
76 }
77
78 template<typename T>
79 inline constexpr bool IS_UPDATER_COMPONENT = Detail::CHECK_COMPONENT_LIST<T, COMPONENT_TYPE_LIST>;
80 } // namespace Updater
81 #endif
82