]> granicus.if.org Git - icinga2/blob - lib/base/ringbuffer.hpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / ringbuffer.hpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #ifndef RINGBUFFER_H
4 #define RINGBUFFER_H
5
6 #include "base/i2-base.hpp"
7 #include "base/object.hpp"
8 #include <boost/thread/mutex.hpp>
9 #include <vector>
10
11 namespace icinga
12 {
13
14 /**
15  * A ring buffer that holds a pre-defined number of integers.
16  *
17  * @ingroup base
18  */
19 class RingBuffer final
20 {
21 public:
22         DECLARE_PTR_TYPEDEFS(RingBuffer);
23
24         typedef std::vector<int>::size_type SizeType;
25
26         RingBuffer(SizeType slots);
27
28         SizeType GetLength() const;
29         void InsertValue(SizeType tv, int num);
30         int UpdateAndGetValues(SizeType tv, SizeType span);
31         double CalculateRate(SizeType tv, SizeType span);
32
33 private:
34         mutable boost::mutex m_Mutex;
35         std::vector<int> m_Slots;
36         SizeType m_TimeValue;
37         SizeType m_InsertedValues;
38
39         void InsertValueUnlocked(SizeType tv, int num);
40         int UpdateAndGetValuesUnlocked(SizeType tv, SizeType span);
41 };
42
43 }
44
45 #endif /* RINGBUFFER_H */