1 /* 2 * Copyright (c) 2021 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 #ifndef RENDER_SERVICE_CLIENT_CORE_UI_RS_BASE_NODE_H 16 #define RENDER_SERVICE_CLIENT_CORE_UI_RS_BASE_NODE_H 17 18 #include <memory> 19 #include <unistd.h> 20 #include <vector> 21 22 #include "common/rs_common_def.h" 23 24 namespace OHOS { 25 namespace Rosen { 26 class RSTransitionEffect; 27 28 class RS_EXPORT RSBaseNode : public std::enable_shared_from_this<RSBaseNode> { 29 public: 30 using WeakPtr = std::weak_ptr<RSBaseNode>; 31 using SharedPtr = std::shared_ptr<RSBaseNode>; 32 static inline constexpr RSUINodeType Type = RSUINodeType::BASE_NODE; 33 34 virtual ~RSBaseNode(); 35 36 void AddChild(SharedPtr child, int index); 37 void RemoveChild(SharedPtr child); 38 void RemoveFromTree(); 39 void ClearChildren(); 40 GetId()41 NodeId GetId() const 42 { 43 return id_; 44 } 45 GetType()46 virtual RSUINodeType GetType() const 47 { 48 return RSUINodeType::BASE_NODE; 49 } 50 51 template<typename T> 52 bool IsInstanceOf(); 53 54 // type-safe reinterpret_cast 55 template<typename T> ReinterpretCast(const std::shared_ptr<RSBaseNode> & node)56 static std::shared_ptr<T> ReinterpretCast(const std::shared_ptr<RSBaseNode>& node) 57 { 58 return node ? node->ReinterpretCastTo<T>() : nullptr; 59 } 60 template<typename T> ReinterpretCastTo()61 std::shared_ptr<T> ReinterpretCastTo() 62 { 63 return (IsInstanceOf<T>()) ? std::static_pointer_cast<T>(shared_from_this()) : nullptr; 64 } 65 virtual std::string DumpNode(int depth) const; 66 protected: 67 RSBaseNode(bool isRenderServiceNode); 68 RSBaseNode(const RSBaseNode&) = delete; 69 RSBaseNode(const RSBaseNode&&) = delete; 70 RSBaseNode& operator=(const RSBaseNode&) = delete; 71 RSBaseNode& operator=(const RSBaseNode&&) = delete; 72 OnAddChildren()73 virtual void OnAddChildren() {} OnRemoveChildren()74 virtual void OnRemoveChildren() {} 75 SharedPtr GetParent(); 76 SetId(const NodeId & id)77 void SetId(const NodeId& id) 78 { 79 id_ = id; 80 } 81 IsRenderServiceNode()82 bool IsRenderServiceNode() const 83 { 84 return isRenderServiceNode_; 85 } 86 87 private: 88 static NodeId GenerateId(); 89 NodeId id_; 90 const bool isRenderServiceNode_; 91 92 NodeId parent_ = 0; 93 std::vector<NodeId> children_; 94 void SetParent(NodeId parent); 95 void RemoveChildById(NodeId childId); 96 97 friend class RSUIDirector; 98 }; 99 } // namespace Rosen 100 } // namespace OHOS 101 102 #endif // RENDER_SERVICE_CLIENT_CORE_UI_RS_BASE_NODE_H 103