cppcore
A small, portable C++ core library
Loading...
Searching...
No Matches
TQueue.h
Go to the documentation of this file.
1/*-----------------------------------------------------------------------------------------------
2The MIT License (MIT)
3
4Copyright (c) 2014-2026 Kim Kulling
5
6Permission is hereby granted, free of charge, to any person obtaining a copy of
7this software and associated documentation files (the "Software"), to deal in
8the Software without restriction, including without limitation the rights to
9use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10the Software, and to permit persons to whom the Software is furnished to do so,
11subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22-----------------------------------------------------------------------------------------------*/
23#pragma once
24
27
28namespace cppcore {
29
30//-------------------------------------------------------------------------------------------------
35//-------------------------------------------------------------------------------------------------
36template<class T, class TAlloc = TDefaultAllocator<T>>
37class TQueue {
38public:
40 TQueue() noexcept = default;
41
44 TQueue(const TQueue<T, TAlloc> &rhs );
45
47 ~TQueue();
48
51 void enqueue( const T &item );
52
57 bool dequeue( T &item );
58
61 T front();
62
65 bool isEmpty() const;
66
69 size_t size( );
70
72 void clear();
73
75 TQueue<T, TAlloc> &operator = ( const TQueue<T, TAlloc> &rhs );
76
78 bool operator == ( const TQueue<T, TAlloc> &rhs ) const;
79
80private:
81 TList<T> mQueueData{};
82};
83
84template<class T, class TAlloc>
86 mQueueData(rhs.mQueueData) {
87 // empty
88}
89
90template<class T, class TAlloc>
92 clear();
93}
94
95template<class T, class TAlloc>
96inline void TQueue<T, TAlloc>::enqueue( const T &item ) {
97 mQueueData.addBack( item );
98}
99
100template<class T, class TAlloc>
101inline bool TQueue<T, TAlloc>::dequeue( T &item ) {
102 item = mQueueData.front();
103 mQueueData.removeFront();
104 if ( isEmpty() ) {
105 return false;
106 }
107
108 return true;
109}
110
111template<class T, class TAlloc>
113 return mQueueData.front();
114}
115
116template<class T, class TAlloc>
117inline bool TQueue<T, TAlloc>::isEmpty() const {
118 return mQueueData.isEmpty();
119}
120
121template<class T, class TAlloc>
122inline size_t TQueue<T, TAlloc>::size( ) {
123 return mQueueData.size();
124}
125
126template<class T, class TAlloc>
128 mQueueData.clear();
129}
130
131template<class T, class TAlloc>
133 if ( *this == rhs ) {
134 return *this;
135 }
136
137 mQueueData = rhs.mQueueData;
138
139 return *this;
140}
141
142template<class T, class TAlloc>
143inline bool TQueue<T, TAlloc>::operator == ( const TQueue<T, TAlloc> &rhs ) const {
144 return mQueueData == rhs.mQueueData;
145}
146
147} // Namespace cppcore
This template class implements a simple double linked list container.
Definition TList.h:37
T front()
Returns the first item of the queue.
Definition TQueue.h:112
TQueue< T, TAlloc > & operator=(const TQueue< T, TAlloc > &rhs)
The assignment operator.
Definition TQueue.h:132
size_t size()
The number of enqueued items will be returned.
Definition TQueue.h:122
~TQueue()
The destructor.
Definition TQueue.h:91
void enqueue(const T &item)
A new item will be enqueued.
Definition TQueue.h:96
bool operator==(const TQueue< T, TAlloc > &rhs) const
The compare operator.
Definition TQueue.h:143
bool isEmpty() const
Returns true, if the queue is empty.
Definition TQueue.h:117
TQueue() noexcept=default
The default class constructor.
bool dequeue(T &item)
Tries to dequeue the next item from the queue.
Definition TQueue.h:101
void clear()
The queue will be cleared.
Definition TQueue.h:127