C++ ring Buffer

I am wondering if anyone have some tips to how I can make a ring buffer in c++. It's need to be able to store strings.

This is really important to me so all kinds of help is very appreciated.
Tips, tutorials, and whatnot.

Last edited on
I'd rather make it myself but I can barrely find anything. I might have to go with that, so I thank you for the link anyway.
Last edited on
Why would you prefer to make it yourself? The point of programming is to write less code, not more. I could understand wanting to learn how they work, but if you intend to actually use it in your code, you should use a more well-tested and expertly designed existing library rather than something you made yourself and think might work. I wouldn't even trust myself to create a working circular buffer.
Well, I want to make it myself for my project. I have installed the btw, but I would still make it myself, but the internet is so empty on this subject.
Would it be a fixed capacity ring buffer (eg. store up to a maximum of 1000 strings) or a resizeable capacity ring buffer?
A fixed would be great, because I am also going to make some threads that can write to and from this buffer.
Last edited on
The general idea would be something along these lines
(caveat: incomplete, not thread-safe, not the most efficient, untested etc.):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <string>
#include <vector>

struct ring_buffer
{
    ring_buffer( std::size_t cap ) : buffer(cap) {}
    bool empty() const { return sz == 0 ; }
    bool full() const { return sz == buffer.size() ; }

    void push( std::string str )
    {
        if( last >= buffer.size() ) last = 0 ;
        buffer[last] = str ;
        ++last ;
        if( full() ) first = (first+1) %  buffer.size() ;
        else ++sz ;
    }

    std::string& operator[] ( std::size_t pos )
    {
        auto p = ( first + pos ) % buffer.size() ;
        return buffer[p] ;
    }

    std::ostream& print( std::ostream& stm = std::cout ) const
    {
        if( first < last )
            for( std::size_t i = first ; i < last ; ++i ) std::cout << buffer[i] << ' ' ;
        else
        {
            for( std::size_t i = first ; i < buffer.size() ; ++i ) std::cout << buffer[i] << ' ' ;
            for( std::size_t i = 0 ; i < last ; ++i ) std::cout << buffer[i] << ' ' ;
        }
        return stm ;
    }

    private:
        std::vector<std::string> buffer ;
        std::size_t first = 0 ;
        std::size_t last = 0 ;
        std::size_t sz = 0 ;
};

int main()
{
    ring_buffer rb(8) ;

    for( int i = 10 ; i < 30 ; ++i )
    {
        rb.push( std::to_string(i) ) ;
        rb.print() << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/f940bac04da42b86
Thank you very much for this. You definetly have some more programming experience than me. Could you explain a couple of sentences for me?

ring_buffer( std::size_t cap ) : buffer(cap) {}

Is there another way to write this sentence? The sentence above.

auto p = ( first + pos ) % buffer.size() ;

I have also never seen the auto keyword.

I am not especially familiar with these either
bool empty() const { return sz == 0 ; }
bool full() const { return sz == buffer.size() ; }

But I think I can figure out that by myself. Just by printing out I can see that it's not working exactly as supposed but I am trying to fix that.
Last edited on
> ring_buffer( std::size_t cap ) : buffer(cap) {}

Initialise the buffer to hold 'cap' empty strings.
Constructor (3) http://en.cppreference.com/w/cpp/container/vector/vector

Another way to write it: ring_buffer( std::size_t cap ) { buffer.resize(cap) ; }
http://en.cppreference.com/w/cpp/container/vector/resize


> auto p = ( first + pos ) % buffer.size() ;

auto: http://www.stroustrup.com/C++11FAQ.html#auto

Another way to write it: std::size_t p = ( first + pos ) % buffer.size() ;


Perhaps you should read a tutorial on vectors: https://cal-linux.com/tutorials/vectors.html
Topic archived. No new replies allowed.