1fb299fa2Sopenharmony_ci/* 2fb299fa2Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License. 5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at 6fb299fa2Sopenharmony_ci * 7fb299fa2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fb299fa2Sopenharmony_ci * 9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and 13fb299fa2Sopenharmony_ci * limitations under the License. 14fb299fa2Sopenharmony_ci */ 15fb299fa2Sopenharmony_ci 16fb299fa2Sopenharmony_ci#include "component_factory.h" 17fb299fa2Sopenharmony_ci 18fb299fa2Sopenharmony_cinamespace Updater { 19fb299fa2Sopenharmony_ci/* 20fb299fa2Sopenharmony_ci * T is component, T::SpecificInfoType is the specific info of this component. 21fb299fa2Sopenharmony_ci * ex. T is ImgViewAdapter, then T::SpecificInfoType is UxImageInfo 22fb299fa2Sopenharmony_ci * T is LabelBtnAdapter, then T::SpecificInfoType is UxLabelBtnInfo 23fb299fa2Sopenharmony_ci * T is BoxProgressAdapter, then T::SpecificInfoType is UxBoxProgressInfo 24fb299fa2Sopenharmony_ci * T is TextLabelAdapter, then T::SpecificInfoType is UxLabelInfo 25fb299fa2Sopenharmony_ci */ 26fb299fa2Sopenharmony_citemplate<typename T> 27fb299fa2Sopenharmony_ciclass CreateFunctor { 28fb299fa2Sopenharmony_cipublic: 29fb299fa2Sopenharmony_ci auto operator()([[maybe_unused]] const typename T::SpecificInfoType &specific) const 30fb299fa2Sopenharmony_ci -> std::function<std::unique_ptr<ComponentInterface>(const UxViewInfo &info)> 31fb299fa2Sopenharmony_ci { 32fb299fa2Sopenharmony_ci return [] (const UxViewInfo &info) { return std::make_unique<T>(info); }; 33fb299fa2Sopenharmony_ci } 34fb299fa2Sopenharmony_ci}; 35fb299fa2Sopenharmony_ci 36fb299fa2Sopenharmony_citemplate<typename T> 37fb299fa2Sopenharmony_ciclass CheckFunctor { 38fb299fa2Sopenharmony_cipublic: 39fb299fa2Sopenharmony_ci auto operator()(const typename T::SpecificInfoType &specific) const 40fb299fa2Sopenharmony_ci -> std::function<bool(const UxViewInfo &info)> 41fb299fa2Sopenharmony_ci { 42fb299fa2Sopenharmony_ci return [&specific] ([[maybe_unused]] const UxViewInfo &info) { return T::IsValid(specific); }; 43fb299fa2Sopenharmony_ci } 44fb299fa2Sopenharmony_ci}; 45fb299fa2Sopenharmony_ci 46fb299fa2Sopenharmony_citemplate<template<typename> typename Functor, typename ...Component> 47fb299fa2Sopenharmony_ciclass Visitor : Functor<Component>... { 48fb299fa2Sopenharmony_cipublic: 49fb299fa2Sopenharmony_ci /* 50fb299fa2Sopenharmony_ci * overloading only works within the same scope. 51fb299fa2Sopenharmony_ci * so import overloaded operator() into this scope 52fb299fa2Sopenharmony_ci * from base class 53fb299fa2Sopenharmony_ci */ 54fb299fa2Sopenharmony_ci using Functor<Component>::operator()...; 55fb299fa2Sopenharmony_ci auto Visit(const UxViewInfo &info) const 56fb299fa2Sopenharmony_ci { 57fb299fa2Sopenharmony_ci return std::visit(*this, info.specificInfo)(info); 58fb299fa2Sopenharmony_ci } 59fb299fa2Sopenharmony_ci}; 60fb299fa2Sopenharmony_ci 61fb299fa2Sopenharmony_cistd::unique_ptr<ComponentInterface> ComponentFactory::CreateUxComponent(const UxViewInfo &info) 62fb299fa2Sopenharmony_ci{ 63fb299fa2Sopenharmony_ci return Visitor<CreateFunctor, COMPONENT_TYPE_LIST> {}.Visit(info); 64fb299fa2Sopenharmony_ci} 65fb299fa2Sopenharmony_ci 66fb299fa2Sopenharmony_cibool ComponentFactory::CheckUxComponent(const UxViewInfo &info) 67fb299fa2Sopenharmony_ci{ 68fb299fa2Sopenharmony_ci return Visitor<CheckFunctor, COMPONENT_TYPE_LIST> {}.Visit(info); 69fb299fa2Sopenharmony_ci} 70fb299fa2Sopenharmony_ci} // namespace Updater 71