Arduino Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
ring_buffer.hpp
Go to the documentation of this file.
1 // Author: Mario S. Könz <mskoenz@gmx.net>
2 // Date: 16.06.2013 00:05:59 EDT
3 // File: ring_buffer.hpp
4 
5 /* This program is free software. It comes without any warranty, to
6  * the extent permitted by applicable law. You can redistribute it
7  * and/or modify it under the terms of the Do What The Fuck You Want
8  * To Public License, Version 2, as published by Sam Hocevar. See
9  * http://www.wtfpl.net/ or COPYING for more details. */
10 
11 #ifndef __RING_BUFFER_HEADER
12 #define __RING_BUFFER_HEADER
13 
14 #include "../ustd/ard_assert.hpp"
15 #include "../ustd/array.hpp"
16 
17 namespace tool {
18  template<typename T, uint16_t N>
19  class ring_buffer {
20  public:
21  typedef uint16_t size_type;
22  typedef uint16_t count_type;
23  //------------------- ctors -------------------
24  ring_buffer(): count_(0) {
25  for(size_type i = 0; i < N; ++i) {
26  array_[i] = 0;
27  }
28  }
29  ring_buffer(ring_buffer const & arg): count_(arg.count_), array_(arg.array_) {
30  }
32  count_ = rhs.count_;
33  array_ = rhs.array_;
34  }
35  //------------------- ops -------------------
36  void push_front(T const & t) {
37  ++count_;
38  (*this)[0] = t;
39  }
40  void pop_front() {
41  (*this)[0] = T();
42  --count_;
43  }
44  //------------------- getter -------------------
45  T & operator[](size_type const & pos) {
46  ASSERT(pos < size())
47  return array_[(count_ - 1 - pos) % size()];
48  }
49  T const & operator[](size_type const & pos) const {
50  ASSERT(pos < size())
51  return array_[(count_ - 1 - pos) % size()];
52  }
53  T & back() {
54  ASSERT(count_ > 0)
55  return (*this)[size() - 1];
56  }
57  T const & back() const {
58  ASSERT(count_ > 0)
59  return (*this)[size() - 1];
60  }
61  T & front() {
62  ASSERT(count_ > 0)
63  return (*this)[0];
64  }
65  T const & front() const {
66  ASSERT(count_ > 0)
67  return (*this)[0];
68  }
69  //------------------- size -------------------
70  size_type size() const {
71  return ((count_ < N) ? count_ : N);
72  }
73  //------------------- info -------------------
74  count_type const & count() const {
75  return count;
76  }
77  //------------------- print & serialize-------------------
78  template<typename S>
79  void print(S & os) const {
80  os << "[";
81  for(size_type i = 0; i < size(); ++i) {
82  os << (*this)[i];
83  if(i != size() - 1)
84  os << ", ";
85  }
86  os << "]";
87  }
88  template<typename Archive>
89  void serialize(Archive & ar) {
90  ar & count_;
91  ar & array_;
92  }
93  private:
94  count_type count_;
95  ustd::array<T, N> array_;
96  };
97  template<typename T, uint16_t N, typename S>
98  S & operator<<(S & os, ring_buffer<T, N> const & arg) {
99  arg.print(os);
100  return os;
101  }
102 }//end namespace tool
103 
104 #endif //__RING_BUFFER_HEADER
uint16_t count_type
Definition: ring_buffer.hpp:22
Definition: ring_buffer.hpp:19
T const & operator[](size_type const &pos) const
Definition: ring_buffer.hpp:49
T const & front() const
Definition: ring_buffer.hpp:65
void print(S &os) const
print to any stream-concept
Definition: array.hpp:117
T & operator[](size_type const &pos)
Definition: ring_buffer.hpp:45
T & back()
Definition: ring_buffer.hpp:53
ring_buffer & operator=(ring_buffer const &rhs)
Definition: ring_buffer.hpp:31
void serialize(Archive &ar)
Definition: ring_buffer.hpp:89
T & front()
Definition: ring_buffer.hpp:61
uint16_t size_type
Definition: ring_buffer.hpp:21
basically just a wrap for an cpp-array
Definition: array.hpp:22
ring_buffer(ring_buffer const &arg)
Definition: ring_buffer.hpp:29
count_type const & count() const
Definition: ring_buffer.hpp:74
void print(S &os) const
Definition: ring_buffer.hpp:79
void pop_front()
Definition: ring_buffer.hpp:40
size_type size() const
Definition: ring_buffer.hpp:70
#define ASSERT(exp)
Definition: ard_assert.hpp:33
T const & back() const
Definition: ring_buffer.hpp:57
void push_front(T const &t)
Definition: ring_buffer.hpp:36
ring_buffer()
Definition: ring_buffer.hpp:24