KDStateMachineEditor API Documentation 2.1
Loading...
Searching...
No Matches
ringbuffer.h
Go to the documentation of this file.
1/*
2 This file is part of the KDAB State Machine Editor Library.
3
4 SPDX-FileCopyrightText: 2014 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
23namespace KDSME {
24
37template<class T>
39{
40public:
44 explicit RingBuffer(int capacity = 10)
45 : m_capacity(capacity)
46 {
47 }
48
55 void setCapacity(int capacity)
56 {
57 Q_ASSERT(capacity > 0);
58 m_capacity = capacity;
59 cleanup();
60 }
61
65 void enqueue(T t)
66 {
67 m_entries.enqueue(t);
68 cleanup();
69 }
70
74 void clear()
75 {
76 m_entries.clear();
77 }
78
79 inline const T &at(int i) const
80 {
81 return m_entries.at(i);
82 }
83 inline int size() const
84 {
85 return m_entries.size();
86 }
91 inline T head() const
92 {
93 return m_entries.head();
94 }
99 inline T last() const
100 {
101 return m_entries.last();
102 }
103 inline QList<T> entries() const
104 {
105 return m_entries;
106 }
107
108private:
109 void cleanup()
110 {
111 while (m_entries.size() > m_capacity) {
112 m_entries.dequeue();
113 }
114 }
115
116 QQueue<T> m_entries;
117 int m_capacity;
118};
119
120}
121
122#endif
RingBuffer(int capacity=10)
Definition ringbuffer.h:44
void setCapacity(int capacity)
Definition ringbuffer.h:55
const T & at(int i) const
Definition ringbuffer.h:79
int size() const
Definition ringbuffer.h:83
void enqueue(T t)
Definition ringbuffer.h:65
QList< T > entries() const
Definition ringbuffer.h:103

© 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
Generated on Tue Jul 15 2025 15:21:47 for KDStateMachineEditor API Documentation by doxygen 1.9.8