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#ifndef VIEW_PROXY_H
17fb299fa2Sopenharmony_ci#define VIEW_PROXY_H
18fb299fa2Sopenharmony_ci
19fb299fa2Sopenharmony_ci#include <memory>
20fb299fa2Sopenharmony_ci#include <string>
21fb299fa2Sopenharmony_ci#include <type_traits>
22fb299fa2Sopenharmony_ci#include "component/component_factory.h"
23fb299fa2Sopenharmony_ci#include "components/ui_view.h"
24fb299fa2Sopenharmony_ci#include "log/log.h"
25fb299fa2Sopenharmony_ci
26fb299fa2Sopenharmony_cinamespace Updater {
27fb299fa2Sopenharmony_ciclass ViewProxy final {
28fb299fa2Sopenharmony_cipublic:
29fb299fa2Sopenharmony_ci    ViewProxy() = default;
30fb299fa2Sopenharmony_ci    ViewProxy(std::unique_ptr<ComponentInterface> view, const std::string &message)
31fb299fa2Sopenharmony_ci        : view_(std::move(view)), errMsg_(message) { }
32fb299fa2Sopenharmony_ci    explicit ViewProxy(std::unique_ptr<ComponentInterface> view) : view_(std::move(view)) { }
33fb299fa2Sopenharmony_ci    ~ViewProxy() = default;
34fb299fa2Sopenharmony_ci    OHOS::UIView *operator->() const
35fb299fa2Sopenharmony_ci    {
36fb299fa2Sopenharmony_ci        static OHOS::UIView dummy;
37fb299fa2Sopenharmony_ci        if (view_ != nullptr) {
38fb299fa2Sopenharmony_ci            return view_->GetOhosView();
39fb299fa2Sopenharmony_ci        }
40fb299fa2Sopenharmony_ci        return &dummy;
41fb299fa2Sopenharmony_ci    }
42fb299fa2Sopenharmony_ci    template<typename T = OHOS::UIView>
43fb299fa2Sopenharmony_ci    T *As() const
44fb299fa2Sopenharmony_ci    {
45fb299fa2Sopenharmony_ci        std::string errMsg {};
46fb299fa2Sopenharmony_ci        return As<T>(errMsg);
47fb299fa2Sopenharmony_ci    }
48fb299fa2Sopenharmony_ci    template<typename T, std::enable_if_t<IS_UPDATER_COMPONENT<T>>* = nullptr>
49fb299fa2Sopenharmony_ci    T *As(std::string &errMsg) const
50fb299fa2Sopenharmony_ci    {
51fb299fa2Sopenharmony_ci        static T dummy;
52fb299fa2Sopenharmony_ci        if (view_ == nullptr) {
53fb299fa2Sopenharmony_ci            errMsg = errMsg_ + " view is null";
54fb299fa2Sopenharmony_ci            return &dummy;
55fb299fa2Sopenharmony_ci        }
56fb299fa2Sopenharmony_ci        if (std::string(dummy.GetComponentType()) != view_->GetComponentType()) {
57fb299fa2Sopenharmony_ci            errMsg = errMsg_ + " view's real type not matched";
58fb299fa2Sopenharmony_ci            LOG(ERROR) << errMsg;
59fb299fa2Sopenharmony_ci            return &dummy;
60fb299fa2Sopenharmony_ci        }
61fb299fa2Sopenharmony_ci        return static_cast<T *>(view_.get());
62fb299fa2Sopenharmony_ci    }
63fb299fa2Sopenharmony_ci    template<typename T = OHOS::UIView, std::enable_if_t<!IS_UPDATER_COMPONENT<T>>* = nullptr>
64fb299fa2Sopenharmony_ci    T *As(std::string &errMsg) const
65fb299fa2Sopenharmony_ci    {
66fb299fa2Sopenharmony_ci        static T dummy;
67fb299fa2Sopenharmony_ci        static_assert(std::is_base_of_v<OHOS::UIView, T>,
68fb299fa2Sopenharmony_ci            "template argument should be a derived class of OHOS::UIView");
69fb299fa2Sopenharmony_ci        if (view_ == nullptr) {
70fb299fa2Sopenharmony_ci            errMsg = errMsg_ + " view is null";
71fb299fa2Sopenharmony_ci            return &dummy;
72fb299fa2Sopenharmony_ci        }
73fb299fa2Sopenharmony_ci        OHOS::UIView *ohosView = view_->GetOhosView();
74fb299fa2Sopenharmony_ci        if constexpr (std::is_same_v<OHOS::UIView, T>) {
75fb299fa2Sopenharmony_ci            return ohosView;
76fb299fa2Sopenharmony_ci        }
77fb299fa2Sopenharmony_ci        if (dummy.GetViewType() != ohosView->GetViewType()) {
78fb299fa2Sopenharmony_ci            errMsg = errMsg_ + " view's real type not matched";
79fb299fa2Sopenharmony_ci            LOG(ERROR) << errMsg;
80fb299fa2Sopenharmony_ci            return &dummy;
81fb299fa2Sopenharmony_ci        }
82fb299fa2Sopenharmony_ci        return static_cast<T *>(ohosView);
83fb299fa2Sopenharmony_ci    }
84fb299fa2Sopenharmony_ciprivate:
85fb299fa2Sopenharmony_ci    std::unique_ptr<ComponentInterface> view_ {nullptr};
86fb299fa2Sopenharmony_ci    std::string errMsg_ {};
87fb299fa2Sopenharmony_ci};
88fb299fa2Sopenharmony_ci} // namespace Updater
89fb299fa2Sopenharmony_ci#endif
90