• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "component_factory.h"
17 #include "box_progress_adapter.h"
18 #include "img_view_adapter.h"
19 #include "label_btn_adapter.h"
20 #include "text_label_adapter.h"
21 
22 namespace Updater {
23 /*
24  * T is component, T::SpecificInfoType is the specific info of this component.
25  * ex. T is ImgViewAdapter, then T::SpecificInfoType is UxImageInfo
26  * T is LabelBtnAdapter, then T::SpecificInfoType is UxLabelBtnInfo
27  * T is BoxProgressAdapter, then T::SpecificInfoType is UxBoxProgressInfo
28  * T is TextLabelAdapter, then T::SpecificInfoType is UxLabelInfo
29  */
30 template<typename T>
31 class CreateFunctor {
32 public:
operator ()(const typename T::SpecificInfoType & specific) const33     auto operator()([[maybe_unused]] const typename T::SpecificInfoType &specific) const
34         -> std::function<std::unique_ptr<OHOS::UIView>(const UxViewInfo &info)>
35     {
36         return [] (const UxViewInfo &info) { return std::make_unique<T>(info); };
37     }
38 };
39 
40 template<typename T>
41 class CheckFunctor {
42 public:
operator ()(const typename T::SpecificInfoType & specific) const43     auto operator()(const typename T::SpecificInfoType &specific) const
44         -> std::function<bool(const UxViewInfo &info)>
45     {
46         return [&specific] ([[maybe_unused]] const UxViewInfo &info) { return T::IsValid(specific); };
47     }
48 };
49 
50 template<template<typename> typename Functor, typename ...Component>
51 class ComponentFactory::Visitor<Functor, std::tuple<Component...>>: Functor<Component>... {
52 public:
53     /*
54      * overloading only works within the same scope.
55      * so import overloaded operator() into this scope
56      * from base class
57      */
58     using Functor<Component>::operator()...;
Visit(const UxViewInfo & info) const59     auto Visit(const UxViewInfo &info) const
60     {
61         return std::visit(*this, info.specificInfo)(info);
62     }
63 };
64 
65 /*
66  * all supported component types listed here, you
67  * should add a template argument when add a new
68  * component type
69  */
70 using ComponentTypes = std::tuple<BoxProgressAdapter, ImgViewAdapter, TextLabelAdapter, LabelBtnAdapter>;
71 
72 static_assert(std::tuple_size_v<ComponentTypes> == std::variant_size_v<SpecificInfo>,
73     "SpecificInfo's size should be equal to the ComponentTypes' size");
74 
CreateUxComponent(const UxViewInfo & info)75 std::unique_ptr<OHOS::UIView> ComponentFactory::CreateUxComponent(const UxViewInfo &info)
76 {
77     return Visitor<CreateFunctor, ComponentTypes> {}.Visit(info);
78 }
79 
CheckUxComponent(const UxViewInfo & info)80 bool ComponentFactory::CheckUxComponent(const UxViewInfo &info)
81 {
82     return Visitor<CheckFunctor, ComponentTypes> {}.Visit(info);
83 }
84 } // namespace Updater
85