circular_buffer iterators

I have a simple use case for circular_buffer but I need a few advices. There is one producer and one consumer which only inspects elements and circular buffer will only be keeping history of last CB_SIZE elements. As far as I have understood there are two ways to access buffer elements, using indexes and operator[] or using iterators. I have chosen iterators. Since adding elements to buffer can invalidate iterators what is the proper way to check if they are still valid? Is it comparing it to .end() and .begin()? typedef boost::circular_buffer<data_type> CircularBuffer; CircularBuffer cb(CB_SIZE); CircularBuffer::iterator it = cb.begin(); ... if( it >= cb.begin() && it < cb.end() ){ cout << *it; } Regards, BoskoM.

Hi Bosko, there is no way of checking if the iterator is still valid (except in debug mode). I recommend you either to rely on operator[] or to get a new interator from the buffer every time consumer accesses it. Regards, Jan ________________________________ From: Bosko Markovic <boskoma@gmail.com> To: boost-users@lists.boost.org Sent: Mon, 20 September, 2010 23:17:55 Subject: [Boost-users] circular_buffer iterators I have a simple use case for circular_buffer but I need a few advices. There is one producer and one consumer which only inspects elements and circular buffer will only be keeping history of last CB_SIZE elements. As far as I have understood there are two ways to access buffer elements, using indexes and operator[] or using iterators. I have chosen iterators. Since adding elements to buffer can invalidate iterators what is the proper way to check if they are still valid? Is it comparing it to .end() and .begin()? typedef boost::circular_buffer<data_type> CircularBuffer; CircularBuffer cb(CB_SIZE); CircularBuffer::iterator it = cb.begin(); ... if( it >= cb.begin() && it < cb.end() ){ cout << *it; } Regards, BoskoM. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Hi Jan, According to your reply it seams that when using operator[] I must also keep reading index updated, i.e. after operation like push/pop/insert I also must change reading index accordingly so it still points to old/correct element. It must be done manually, library will not do that part of the job for me. Thank you for your explanation. Regards, Bosko. On Tue, Sep 21, 2010 at 12:17 AM, Bosko Markovic <boskoma@gmail.com> wrote:
I have a simple use case for circular_buffer but I need a few advices. There is one producer and one consumer which only inspects elements and circular buffer will only be keeping history of last CB_SIZE elements. As far as I have understood there are two ways to access buffer elements, using indexes and operator[] or using iterators. I have chosen iterators. Since adding elements to buffer can invalidate iterators what is the proper way to check if they are still valid? Is it comparing it to .end() and .begin()?
typedef boost::circular_buffer<data_type> CircularBuffer; CircularBuffer cb(CB_SIZE); CircularBuffer::iterator it = cb.begin(); ... if( it >= cb.begin() && it < cb.end() ){ cout << *it; }
Regards, BoskoM.
participants (2)
-
Bosko Markovic
-
Jan Gaspar