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