Circular buffer in Qt
-
I need to use an circular buffer and I found the QCircularBuffer class which seems to be the solution. But I noticed that it is a part of Qt3D library like it is a class defined for 3D applications. Maybe I'm wrong but since I think that a circular buffer is a common component in most applications (comunications for example, like in mine) I have a doubt in the use of the QCircularBuffer in my application. Maybe is there another class other than QCircularBuffer which define a circular buffer?
-
@Andrew23 said in Circular buffer in Qt:
Maybe is there another class other than QCircularBuffer which define a circular buffer?
I think not, because https://stackoverflow.com/questions/45415745/qcircularbuffer-functionality-without-qt3d offers you proposed code without 3D?
There was also a discussion of this > 9 years ago: https://development.qt-project.narkive.com/VWc3Ab98/new-circular-buffer-container-for-qt5 :)
There is also a circular buffer in Boost C++ libraries, https://www.qtcentre.org/threads/66466-qt-way-of-having-a-ring-or-circular-buffer-set
-
I would just write a templated class using QVector and 2 index variables to track head and tail. I say use QVector and preallocate the space and just read and write to locations. An empty buffer would just be head and tail pointing to the same index. If this is templated you can use it for more than one data type easily.
-
since it is wholely implmented in templates I wrapped boost::circular_buffer in a class that adds a semaphore lock and called it good.