LibNRG  0.0.1
Networking for Real-time Games library
 All Classes Files Functions Variables Friends
nrg_ringbuffer.h
Go to the documentation of this file.
1 /*
2  LibNRG - Networking for Real-time Games
3 
4  Copyright (C) 2012-2014 Alex Baines <alex@abaines.me.uk>
5 
6  This software is provided 'as-is', without any express or implied
7  warranty. In no event will the authors be held liable for any damages
8  arising from the use of this software.
9 
10  Permission is granted to anyone to use this software for any purpose,
11  including commercial applications, and to alter it and redistribute it
12  freely, subject to the following restrictions:
13 
14  1. The origin of this software must not be misrepresented; you must not
15  claim that you wrote the original software. If you use this software
16  in a product, an acknowledgment in the product documentation would be
17  appreciated but is not required.
18  2. Altered source versions must be plainly marked as such, and must not be
19  misrepresented as being the original software.
20  3. This notice may not be removed or altered from any source distribution.
21 */
27 #ifndef NRG_RING_BUFFER_H
28 #define NRG_RING_BUFFER_H
29 #include "nrg_core.h"
30 #include <array>
31 #include <iterator>
32 #include <utility>
33 
34 namespace nrg {
35 
37 template<class T, size_t N>
38 class RingBuffer {
39 public:
41  RingBuffer() : data(), start_idx(0), end_idx(0){}
42 
44  class iterator : public std::iterator<std::forward_iterator_tag, const T> {
45  public:
47  iterator(const RingBuffer* const rb, size_t i) : buff(rb), idx(i){}
48 
49  bool operator<(const iterator& other) const {
50  size_t dist_a = calcDist(*this), dist_b = calcDist(other);
51  return dist_a < dist_b;
52  }
53 
54  bool operator==(const iterator& other) const {
55  return idx == other.idx;
56  }
57 
58  bool operator!=(const iterator& other) const {
59  return idx != other.idx;
60  }
61 
62  const iterator& operator++(void){
63  if(idx != (ssize_t)buff->end_idx) idx = (idx + 1) % buff->data.size();
64  return *this;
65  }
66 
67  const iterator& operator--(void){
68  if(idx != (ssize_t)buff->start_idx) --idx;
69  if(idx < 0) idx = buff->data.size()-1;
70  return *this;
71  }
72 
73  const T* operator->() const {
74  if(idx == buff->end_idx){
75  return nullptr;
76  } else {
77  return &buff->data[idx];
78  }
79  }
80 
81  const T& operator*() const {
82  return buff->data[idx];
83  }
84  private:
85  size_t calcDist(const iterator& i){
86  if(i.idx > i.buff.start_idx){
87  return i.idx - i.buff->start_idx;
88  } else {
89  return i.buff->size() - (i.buff->start_idx - i.idx);
90  }
91  }
92  const RingBuffer* buff;
93  ssize_t idx;
94  };
95 
97  T& next(void){
98  T& ret = data[end_idx];
99  end_idx = (end_idx + 1) % data.size();
100  if(end_idx == start_idx){
101  start_idx = (start_idx + 1) % data.size();
102  }
103  return ret;
104  }
105 
107  iterator begin() const {
108  return iterator(this, start_idx);
109  }
110 
112  iterator end() const {
113  return iterator(this, end_idx);
114  }
115 
117  std::reverse_iterator<iterator> rbegin() const {
118  return std::reverse_iterator<iterator>(end());
119  }
120 
122  std::reverse_iterator<iterator> rend() const {
123  return std::reverse_iterator<iterator>(begin());
124  }
125 
126 private:
127  std::array<T, N> data;
128  size_t start_idx, end_idx;
129 };
130 
131 }
132 
133 #endif
iterator begin() const
Returns an Iterator to the beginning of the RingBuffer.
Definition: nrg_ringbuffer.h:107
Iterator class for the RingBuffer.
Definition: nrg_ringbuffer.h:44
iterator end() const
Returns an Iterator to the end of the RingBuffer.
Definition: nrg_ringbuffer.h:112
iterator(const RingBuffer *const rb, size_t i)
Default Iterator Constructor.
Definition: nrg_ringbuffer.h:47
ring / circular buffer implementation
Definition: nrg_ringbuffer.h:38
std::reverse_iterator< iterator > rend() const
Returns a Reverse-Iterator to the reverse-end of the RingBuffer.
Definition: nrg_ringbuffer.h:122
Common defines and includes used by all the other nrg header files.
std::reverse_iterator< iterator > rbegin() const
Returns a Reverse-Iterator to the reverse-beginning of the RingBuffer.
Definition: nrg_ringbuffer.h:117
RingBuffer()
Default Constructor.
Definition: nrg_ringbuffer.h:41
T & next(void)
Moves the ringbuffer up one, and returns a reference to this element.
Definition: nrg_ringbuffer.h:97