File: RingBuffer.hpp

package info (click to toggle)
libprojectm 1.2.0-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 6,264 kB
  • ctags: 11,793
  • sloc: ansic: 24,587; cpp: 18,410; makefile: 60; sh: 18
file content (65 lines) | stat: -rw-r--r-- 1,519 bytes parent folder | download
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
55
56
57
58
59
60
61
62
63
64
65
#ifndef RING_BUFFER_HPP
#define RING_BUFFER_HPP
/// Code courtesy of: http://www.osix.net/modules/article/?id=464
 
template<typename kind> 
class RingBuffer { 
public:

	//static const unsigned long RING_BUFFER_SIZE = 1024;
	static const unsigned long RING_BUFFER_SIZE = 32768;
private:

    kind buffer[RING_BUFFER_SIZE]; 
    unsigned int current_element; 
public: 
    RingBuffer() : current_element(0) { 
    } 
 
    RingBuffer(const RingBuffer& old_ring_buf) { 
        memcpy(buffer, old_ring_buf.buffer, RING_BUFFER_SIZE*sizeof(kind)); 
        current_element = old_ring_buf.current_element; 
    } 
 
    RingBuffer operator = (const RingBuffer& old_ring_buf) { 
        memcpy(buffer, old_ring_buf.buffer, RING_BUFFER_SIZE*sizeof(kind)); 
        current_element = old_ring_buf.current_element; 
    } 
 
    ~RingBuffer() { } 
 
    void append(kind value) { 
        if (current_element == RING_BUFFER_SIZE) { 
            current_element = 0; 
        }
 
        buffer[current_element] = value; 
 
        ++current_element; 
    }
 

    kind back() {
	
	if (current_element == 0) {
            current_element = RING_BUFFER_SIZE;
        }
	--current_element;
 	return buffer[current_element];
    }

 
    kind forward() { 
        if(current_element >= RING_BUFFER_SIZE) { 
            current_element = 0; 
        } 
 
        ++current_element; 
        return( buffer[(current_element-1)] ); 
    } 
 
    int current() { 
        return (current_element % RING_BUFFER_SIZE); 
    }
}; 
#endif