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 TEST_WUKONG_WUKONG_TREE_H 17 #define TEST_WUKONG_WUKONG_TREE_H 18 19 #include <vector> 20 21 #include "wukong_define.h" 22 23 namespace OHOS { 24 namespace WuKong { 25 class WuKongTree { 26 public: GetRoot(const std::shared_ptr<WuKongTree> & child)27 static const std::shared_ptr<WuKongTree> GetRoot(const std::shared_ptr<WuKongTree>& child) 28 { 29 if (child->GetParent() == nullptr) { 30 return child; 31 } 32 return GetRoot(child->GetParent()); 33 } 34 35 public: WuKongTree()36 WuKongTree() : nodeId_(0), index_(0), inputCount_(0), parent_(), children_(0) 37 { 38 } ~WuKongTree()39 virtual ~WuKongTree() 40 { 41 } 42 43 /** 44 * @brief Get node id for node specification value. 45 * @return uint64 specification value. 46 */ GetNodeId()47 virtual uint64_t GetNodeId() 48 { 49 return nodeId_; 50 } 51 52 /** 53 * @brief Get Index for value list index. 54 * @return uint32 index value. 55 */ GetIndex()56 virtual uint32_t GetIndex() 57 { 58 return index_; 59 } 60 61 /** 62 * @brief Get node parent pointer, if root node return nullptr. 63 * @return WuKongTree shared pointer. 64 */ GetParent()65 virtual const std::shared_ptr<WuKongTree> GetParent() 66 { 67 TRACK_LOG_STR("current note has parent (%d), Node (0x%016llX)", !parent_.expired(), nodeId_); 68 return parent_.lock(); 69 } 70 71 /** 72 * @brief Get node children pointer, return 0 if no child. 73 * @return WuKongTree vector. 74 */ GetChildren()75 virtual const std::vector<std::shared_ptr<WuKongTree>>& GetChildren() 76 { 77 return children_; 78 } 79 80 /** 81 * @brief Get node input event count for input statistics. 82 * @return uint32 statistics value. 83 */ GetInputCount()84 virtual uint32_t GetInputCount() 85 { 86 return inputCount_; 87 } 88 /** 89 * @brief Are they equal, and check nodeId. 90 * @param other WuKongTree shared pointer. 91 * @return return compare result 92 */ IsEqual(const std::shared_ptr<WuKongTree> & other)93 virtual bool IsEqual(const std::shared_ptr<WuKongTree>& other) 94 { 95 if (other == nullptr) { 96 return false; 97 } 98 return (nodeId_ == other->nodeId_); 99 } 100 101 protected: 102 friend class TreeManager; 103 virtual bool SetNodeId() = 0; SetIndex(uint32_t index)104 virtual void SetIndex(uint32_t index) 105 { 106 index_ = index; 107 } SetParent(const std::shared_ptr<WuKongTree> & parent)108 virtual void SetParent(const std::shared_ptr<WuKongTree>& parent) 109 { 110 parent_ = parent; 111 } AddChild(const std::shared_ptr<WuKongTree> & child)112 virtual void AddChild(const std::shared_ptr<WuKongTree>& child) 113 { 114 children_.push_back(child); 115 } AddInputCount()116 virtual void AddInputCount() 117 { 118 inputCount_++; 119 } 120 bool RecursUpdateNodeIndex(const uint32_t offset); 121 uint64_t GetSubName(std::string name, uint32_t count, bool isAbility = 0); 122 void GetClearnAbility(std::string &name); 123 124 /** 125 * @brief node specification value. 126 */ 127 uint64_t nodeId_; 128 /** 129 * @brief value list index. 130 */ 131 uint32_t index_; 132 /** 133 * @brief input statistics. 134 */ 135 uint32_t inputCount_; 136 /** 137 * @brief node parent pointer. 138 */ 139 std::weak_ptr<WuKongTree> parent_; 140 /** 141 * @brief node children pointers. 142 */ 143 std::vector<std::shared_ptr<WuKongTree>> children_; 144 }; 145 } // namespace WuKong 146 } // namespace OHOS 147 #endif // TEST_WUKONG_WUKONG_TREE_H 148