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 OHOS_ACELITE_LINK_STACK_H 17 #define OHOS_ACELITE_LINK_STACK_H 18 19 #include "memory_heap.h" 20 #include "non_copyable.h" 21 22 namespace OHOS { 23 namespace ACELite { 24 class StackNode final : public MemoryHeap { 25 public: 26 ACE_DISALLOW_COPY_AND_MOVE(StackNode); StackNode(const char * data)27 explicit StackNode(const char *data) : data_(data), prev_(nullptr), next_(nullptr) {} 28 ~StackNode() = default; 29 SetNodeData(const char * data)30 void SetNodeData(const char *data) 31 { 32 data_ = data; 33 } 34 SetNodePrev(StackNode * prev)35 void SetNodePrev(StackNode *prev) 36 { 37 prev_ = prev; 38 } 39 SetNodeNext(StackNode * next)40 void SetNodeNext(StackNode *next) 41 { 42 next_ = next; 43 } 44 GetNodeData()45 const char *GetNodeData() const 46 { 47 return data_; 48 } 49 GetNodePrev()50 StackNode *GetNodePrev() const 51 { 52 return prev_; 53 } 54 GetNodeNext()55 StackNode *GetNodeNext() const 56 { 57 return next_; 58 } 59 private: 60 const char *data_; 61 StackNode *prev_; 62 StackNode *next_; 63 }; 64 65 class LinkStack final : public MemoryHeap { 66 public: 67 ACE_DISALLOW_COPY_AND_MOVE(LinkStack); LinkStack()68 LinkStack() : maxSize_(0) 69 { 70 InitStack(); 71 } 72 LinkStack(uint32_t maxSize)73 explicit LinkStack(uint32_t maxSize) : maxSize_(maxSize) 74 { 75 InitStack(); 76 } 77 ~LinkStack()78 ~LinkStack() 79 { 80 FreeNode(); 81 } 82 GetTop()83 const StackNode *GetTop() const 84 { 85 return top_; 86 } 87 StackSize()88 uint32_t StackSize() const 89 { 90 return count_; 91 } 92 Peak()93 const char *Peak() const 94 { 95 return top_->GetNodeData(); 96 } 97 98 void InitStack(); 99 void FreeNode(); 100 bool IsEmpty() const; 101 bool IsFull() const; 102 103 /** 104 * @brief push the address of a character array the stack 105 * @param value Push the first address of the character array onto the stack 106 * @return return true if the push is successful 107 * 108 * Note: The value field that is pushed into the stack stores the address information of the 109 * character array and does not manage the life cycle of the character array. Please ensure 110 * that the data is legal when popping the stack. 111 */ 112 bool Push(const char *value); 113 114 /** 115 * @brief pop the address of a character array the stack 116 * @param value Pop the first address of the character array from the stack. If you don't care 117 * about the popped elements, please pass the parameter NULL. 118 * @return return true if the push is successful 119 */ 120 bool Pop(const char **value); 121 private: 122 StackNode *head_; 123 StackNode *top_; 124 uint32_t maxSize_; 125 uint32_t count_; 126 }; 127 } // namespace ACELite 128 } // namespace OHOS 129 #endif // OHOS_ACELITE_LINK_STACK_H 130 131