KDStateMachineEditor API Documentation  1.2.50
ringbuffer.h
1 /*
2  This file is part of the KDAB State Machine Editor Library.
3 
4  SPDX-FileCopyrightText: 2014-2021 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
5  Author: Kevin Funk <kevin.funk@kdab.com>
6 
7  SPDX-License-Identifier: LGPL-2.1-only OR LicenseRef-KDAB-KDStateMachineEditor
8 
9  Licensees holding valid commercial KDAB State Machine Editor Library
10  licenses may use this file in accordance with the KDAB State Machine Editor
11  Library License Agreement provided with the Software.
12 
13  Contact info@kdab.com if any conditions of this licensing are not clear to you.
14 */
15 
16 //krazy:excludeall=inline as this is a template
17 #ifndef KDSME_UTIL_RINGBUFFER_H
18 #define KDSME_UTIL_RINGBUFFER_H
19 
20 #include <QQueue>
21 #include <QList>
22 
23 namespace KDSME {
24 
37 template<class T>
39 {
40 public:
44  RingBuffer(int capacity = 10)
45  : m_capacity(capacity)
46  {}
47 
54  void setCapacity(int capacity)
55  {
56  Q_ASSERT(capacity > 0);
57  m_capacity = capacity;
58  cleanup();
59  }
60 
64  void enqueue(T t)
65  {
66  m_entries.enqueue(t);
67  cleanup();
68  }
69 
73  void clear()
74  {
75  m_entries.clear();
76  }
77 
78  inline const T& at(int i) const { return m_entries.at(i); }
79  inline int size() const { return m_entries.size(); }
84  inline T head() const { return m_entries.head(); }
89  inline T last() const { return m_entries.last(); }
90  inline QList<T> entries() const { return m_entries; }
91 
92  private:
93  void cleanup()
94  {
95  while (m_entries.size() > m_capacity) {
96  m_entries.dequeue();
97  }
98  }
99 
100  QQueue<T> m_entries;
101  int m_capacity;
102 };
103 
104 }
105 
106 #endif
void enqueue(const T &t)
T dequeue()
T & head()
T last() const
Definition: ringbuffer.h:89
T head() const
Definition: ringbuffer.h:84
RingBuffer(int capacity=10)
Definition: ringbuffer.h:44
void enqueue(T t)
Definition: ringbuffer.h:64
Definition: ringbuffer.h:38
void setCapacity(int capacity)
Definition: ringbuffer.h:54
Definition: mainwindow.h:21
void clear()
Definition: ringbuffer.h:73

Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
KDStateMachineEditor
Create Qt State Machine metacode using a graphical user interface
https://github.com/KDAB/KDStateMachineEditor