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 18 namespace Updater { 19 /* 20 * T is component, T::SpecificInfoType is the specific info of this component. 21 * ex. T is ImgViewAdapter, then T::SpecificInfoType is UxImageInfo 22 * T is LabelBtnAdapter, then T::SpecificInfoType is UxLabelBtnInfo 23 * T is BoxProgressAdapter, then T::SpecificInfoType is UxBoxProgressInfo 24 * T is TextLabelAdapter, then T::SpecificInfoType is UxLabelInfo 25 */ 26 template<typename T> 27 class CreateFunctor { 28 public: 29 auto operator()([[maybe_unused]] const typename T::SpecificInfoType &specific) const 30 -> std::function<std::unique_ptr<ComponentInterface>(const UxViewInfo &info)> 31 { 32 return [] (const UxViewInfo &info) { return std::make_unique<T>(info); }; 33 } 34 }; 35 36 template<typename T> 37 class CheckFunctor { 38 public: 39 auto operator()(const typename T::SpecificInfoType &specific) const 40 -> std::function<bool(const UxViewInfo &info)> 41 { 42 return [&specific] ([[maybe_unused]] const UxViewInfo &info) { return T::IsValid(specific); }; 43 } 44 }; 45 46 template<template<typename> typename Functor, typename ...Component> 47 class Visitor : Functor<Component>... { 48 public: 49 /* 50 * overloading only works within the same scope. 51 * so import overloaded operator() into this scope 52 * from base class 53 */ 54 using Functor<Component>::operator()...; 55 auto Visit(const UxViewInfo &info) const 56 { 57 return std::visit(*this, info.specificInfo)(info); 58 } 59 }; 60 61 std::unique_ptr<ComponentInterface> ComponentFactory::CreateUxComponent(const UxViewInfo &info) 62 { 63 return Visitor<CreateFunctor, COMPONENT_TYPE_LIST> {}.Visit(info); 64 } 65 66 bool ComponentFactory::CheckUxComponent(const UxViewInfo &info) 67 { 68 return Visitor<CheckFunctor, COMPONENT_TYPE_LIST> {}.Visit(info); 69 } 70 } // namespace Updater 71