KDStateMachineEditor  1.0.0
A framework for creating Qt State Machine metacode using a graphical user interface
ringbuffer.h
1 /*
2  This file is part of the KDAB State Machine Editor Library.
3 
4  Copyright (C) 2014-2015 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com.
5  All rights reserved.
6  Author: Kevin Funk <kevin.funk@kdab.com>
7 
8  Licensees holding valid commercial KDAB State Machine Editor Library
9  licenses may use this file in accordance with the KDAB State Machine Editor
10  Library License Agreement provided with the Software.
11 
12  This file may be distributed and/or modified under the terms of the
13  GNU Lesser General Public License version 2.1 as published by the
14  Free Software Foundation and appearing in the file LICENSE.LGPL.txt included.
15 
16  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
17  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18 
19  Contact info@kdab.com if any conditions of this licensing are not
20  clear to you.
21 */
22 
23 //krazy:excludeall=inline as this is a template
24 #ifndef KDSME_UTIL_RINGBUFFER_H
25 #define KDSME_UTIL_RINGBUFFER_H
26 
27 #include <QQueue>
28 #include <QList>
29 
30 namespace KDSME {
31 
44 template<class T>
46 {
47 public:
51  RingBuffer(int capacity = 10)
52  : m_capacity(capacity)
53  {}
54 
61  void setCapacity(int capacity)
62  {
63  Q_ASSERT(capacity > 0);
64  m_capacity = capacity;
65  cleanup();
66  }
67 
71  void enqueue(T t)
72  {
73  m_entries.enqueue(t);
74  cleanup();
75  }
76 
80  void clear()
81  {
82  m_entries.clear();
83  }
84 
85  inline const T& at(int i) const { return m_entries.at(i); }
86  inline int size() const { return m_entries.size(); }
91  inline T head() const { return m_entries.head(); }
96  inline T last() const { return m_entries.last(); }
97  inline QList<T> entries() const { return m_entries; }
98 
99  private:
100  void cleanup()
101  {
102  while (m_entries.size() > m_capacity) {
103  m_entries.dequeue();
104  }
105  }
106 
107  QQueue<T> m_entries;
108  int m_capacity;
109 };
110 
111 }
112 
113 #endif
T last() const
Definition: ringbuffer.h:96
T head() const
Definition: ringbuffer.h:91
RingBuffer(int capacity=10)
Definition: ringbuffer.h:51
void enqueue(T t)
Definition: ringbuffer.h:71
Definition: ringbuffer.h:45
void setCapacity(int capacity)
Definition: ringbuffer.h:61
Definition: abstractexporter.h:33
void clear()
Definition: ringbuffer.h:80

Klarälvdalens Datakonsult AB (KDAB)
Qt-related services and products
http://www.kdab.com/
https://github.com/KDAB/KDStateMachineEditor