File: intrusive_queue.h

package info (click to toggle)
etlcpp 20.39.4%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 18,232 kB
  • sloc: cpp: 245,721; ansic: 10,254; sh: 1,481; asm: 301; python: 281; makefile: 24
file content (292 lines) | stat: -rw-r--r-- 10,385 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
///\file

/******************************************************************************
The MIT License(MIT)

Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com

Copyright(c) 2016 John Wellbelove

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/

#ifndef ETL_INTRUSIVE_QUEUE_INCLUDED
#define ETL_INTRUSIVE_QUEUE_INCLUDED

#include "platform.h"
#include "type_traits.h"
#include "error_handler.h"
#include "intrusive_links.h"

#include <stddef.h>

namespace etl
{
  //***************************************************************************
  /// Exception base for intrusive queue
  ///\ingroup intrusive_queue
  //***************************************************************************
  class intrusive_queue_exception : public etl::exception
  {
  public:

    intrusive_queue_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
      : exception(reason_, file_name_, line_number_)
    {
    }
  };

  //***************************************************************************
  /// intrusive_queue empty exception.
  ///\ingroup intrusive_queue
  //***************************************************************************
  class intrusive_queue_empty : public intrusive_queue_exception
  {
  public:

    intrusive_queue_empty(string_type file_name_, numeric_type line_number_)
      : intrusive_queue_exception(ETL_ERROR_TEXT("intrusive_queue:empty", ETL_INTRUSIVE_QUEUE_FILE_ID"A"), file_name_, line_number_)
    {
    }
  };

  //***************************************************************************
  /// intrusive_queue_value_is_already_linked exception.
  ///\ingroup intrusive_queue
  //***************************************************************************
  class intrusive_queue_value_is_already_linked : public intrusive_queue_exception
  {
  public:

    intrusive_queue_value_is_already_linked(string_type file_name_, numeric_type line_number_)
      : intrusive_queue_exception(ETL_ERROR_TEXT("intrusive_queue:value is already linked", ETL_INTRUSIVE_QUEUE_FILE_ID"B"), file_name_, line_number_)
    {
    }
  };

  //***************************************************************************
  ///\ingroup queue
  /// Base for intrusive queue. Stores elements derived any ETL type that supports an 'etl_next' pointer member.
  /// \tparam TLink  The link type that the value is derived from.
  //***************************************************************************
  template <typename TLink>
  class intrusive_queue_base
  {
  public:

    // Node typedef.
    typedef TLink link_type;

    //*************************************************************************
    /// Adds a value to the queue.
    ///\param value The value to push to the queue.
    //*************************************************************************
    void push(link_type& value)
    {
      ETL_ASSERT_OR_RETURN(!value.is_linked(), ETL_ERROR(intrusive_queue_value_is_already_linked));

      if (empty())
      {
        terminator.etl_next = &value;
      }
      else
      {
        p_back->etl_next = &value;
      }

      p_back = &value;
      value.etl_next = &terminator;

      ++current_size;
    }

    //*************************************************************************
    /// Removes the oldest item from the queue.
    /// Undefined behaviour if the queue is already empty.
    //*************************************************************************
    void pop()
    {
#if defined(ETL_CHECK_PUSH_POP)
      ETL_ASSERT_OR_RETURN(!empty(), ETL_ERROR(intrusive_queue_empty));
#endif

      link_type* p_front = terminator.etl_next;

      link_type* p_next = p_front->etl_next;
      terminator.etl_next = p_next;

      p_front->clear();

      if (empty())
      {
        p_back = &terminator;
      }

      --current_size;
    }

    //*************************************************************************
    /// Removes the oldest item from the queue and pushes it to the destination.
    /// Undefined behaviour if the queue is already empty.
    /// NOTE: The destination must be an intrusive container that supports a push(TLink) member function.
    //*************************************************************************
    template <typename TContainer>
    void pop_into(TContainer& destination)
    {
      link_type* p_link = terminator.etl_next;
      pop();
      destination.push(*p_link);
    }

    //*************************************************************************
    /// Clears the queue to the empty state.
    //*************************************************************************
    void clear()
    {
      while (!empty())
      {
        pop();
      }

      current_size = 0;
    }

    //*************************************************************************
    /// Checks if the queue is in the empty state.
    //*************************************************************************
    bool empty() const
    {
      return current_size == 0;
    }

    //*************************************************************************
    /// Returns the number of elements.
    //*************************************************************************
    size_t size() const
    {
      return current_size;
    }

  protected:

    //*************************************************************************
    /// Constructor
    //*************************************************************************
    intrusive_queue_base()
      : p_back (&terminator)
      , current_size(0)
    {
      terminator.etl_next = &terminator;
    }

    //*************************************************************************
    /// Destructor
    //*************************************************************************
    ~intrusive_queue_base()
    {
    }

    link_type* p_back;     ///< Pointer to the current back of the queue.
    link_type  terminator; ///< This link terminates the queue and points to the front of the queue.

    size_t current_size; ///< Counts the number of elements in the list.
  };

  //***************************************************************************
  ///\ingroup queue
  /// An intrusive queue. Stores elements derived from any type that supports an 'etl_next' pointer member.
  /// \warning This queue cannot be used for concurrent access from multiple threads.
  /// \tparam TValue The type of value that the queue holds.
  /// \tparam TLink  The link type that the value is derived from.
  //***************************************************************************
  template <typename TValue, typename TLink>
  class intrusive_queue : public etl::intrusive_queue_base<TLink>
  {
  public:

    // Node typedef.
    typedef typename etl::intrusive_queue_base<TLink> link_type;

    // STL style typedefs.
    typedef TValue            value_type;
    typedef value_type*       pointer;
    typedef const value_type* const_pointer;
    typedef value_type&       reference;
    typedef const value_type& const_reference;
    typedef size_t            size_type;

    //*************************************************************************
    /// Constructor
    //*************************************************************************
    intrusive_queue()
      : intrusive_queue_base<TLink>()
    {
    }

    //*************************************************************************
    /// Gets a reference to the value at the front of the queue.
    /// Undefined behaviour if the queue is empty.
    /// \return A reference to the value at the front of the queue.
    //*************************************************************************
    reference front()
    {
      return *static_cast<TValue*>(this->terminator.etl_next);
    }

    //*************************************************************************
    /// Gets a reference to the value at the back of the queue.
    /// Undefined behaviour if the queue is empty.
    /// \return A reference to the value at the back of the queue.
    //*************************************************************************
    reference back()
    {
      return *static_cast<TValue*>(this->p_back);
    }

    //*************************************************************************
    /// Gets a const reference to the value at the front of the queue.
    /// Undefined behaviour if the queue is empty.
    /// \return A const reference to the value at the front of the queue.
    //*************************************************************************
    const_reference front() const
    {
      return *static_cast<const TValue*>(this->terminator.etl_next);
    }

    //*************************************************************************
    /// Gets a reference to the value at the back of the queue.
    /// Undefined behaviour if the queue is empty.
    /// \return A reference to the value at the back of the queue.
    //*************************************************************************
    const_reference back() const
    {
      return *static_cast<const TValue*>(this->p_back);
    }

  private:

    // Disable copy construction and assignment.
    intrusive_queue(const intrusive_queue&);
    intrusive_queue& operator = (const intrusive_queue& rhs);
  };
}

#endif