• 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 OHOS_ACELITE_LINK_QUEUE_H
17 #define OHOS_ACELITE_LINK_QUEUE_H
18 
19 #include "memory_heap.h"
20 #include "non_copyable.h"
21 
22 namespace OHOS {
23 namespace ACELite {
24 class QueueNode final : public MemoryHeap {
25 public:
26     ACE_DISALLOW_COPY_AND_MOVE(QueueNode);
QueueNode(const char * data)27     explicit QueueNode(const char* data) : data_(data), next_(nullptr) {}
28     ~QueueNode() = default;
SetNodeData(const char * data)29     void SetNodeData(const char *data)
30     {
31         data_ = data;
32     }
33 
SetNodeNext(QueueNode * next)34     void SetNodeNext(QueueNode *next)
35     {
36         next_ = next;
37     }
38 
GetNodeData()39     const char *GetNodeData() const
40     {
41         return data_;
42     }
43 
GetNodeNext()44     QueueNode *GetNodeNext() const
45     {
46         return next_;
47     }
48 
49 private:
50     const char *data_;
51     QueueNode *next_;
52 };
53 
54 class LinkQueue final : public MemoryHeap {
55 public:
56     ACE_DISALLOW_COPY_AND_MOVE(LinkQueue);
LinkQueue()57     LinkQueue() : maxSize_(0)
58     {
59         InitQueue();
60     }
61 
LinkQueue(uint32_t maxSize)62     explicit LinkQueue(uint32_t maxSize) : maxSize_(maxSize)
63     {
64         InitQueue();
65     }
66 
~LinkQueue()67     ~LinkQueue()
68     {
69         FreeNode();
70     }
71 
GetFront()72     const QueueNode *GetFront() const
73     {
74         return front_;
75     }
76 
GetRear()77     const QueueNode *GetRear() const
78     {
79         return rear_;
80     }
81 
82     void InitQueue();
83     bool IsEmpty() const;
84     bool IsFull() const;
85 
86     /**
87      * @brief Enqueue the address of a character array
88      * @param value Enqueue the first address of a character array
89      * @return  if the Enqueue is successful return true
90      *
91      * Note: The value field of the queue stores the address information of the character array, and does not manage
92      * the life cycle of the character array. Please ensure that the data is legal when the queue is dequeued.
93      */
94     bool Enqueue(const char *value);
95 
96     /**
97      * @brief Dequeue the address of a character array
98      * @param value Dequeue the first address of the character array, if you don't care about
99      * the value of the dequeue, you can pass this parameter empty
100      * @return  if the Dequeue is successful return true
101      */
102     bool Dequeue(const char **value);
103     uint32_t LengthQueue() const;
104     QueueNode *GetNext() const;
105     void FreeNode();
106 private:
107     QueueNode *front_;
108     QueueNode *rear_;
109     uint32_t maxSize_;
110     uint32_t length_;
111 };
112 } // namespace ACELite
113 } // namespace OHOS
114 #endif // OHOS_ACELITE_LINK_QUEUE_H
115 
116