commonlib
fifobuffer.h
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include "fifo_buffer_base.h"
14 
15 BEGIN_NAMESPACE_MEMORY
16 
17 template <typename T>
19 {
20  T* bufferPtr;
21  size_t capacity;
22  size_t readPos;
23  size_t writePos;
24  size_t size;
25 };
26 
30 template <typename T>
31 class FifoBuffer : public FifoBufferBase<T>
32 {
33 public:
38  explicit FifoBuffer(size_t aCapacity = 0);
39 
45  void setCapacity(size_t aCapacity) override;
46 
52 
57  T* getData() const override;
58 
63  std::size_t getSize() const;
64 
65 protected:
66  std::unique_ptr<T[]> buffer;
67 };
68 
69 
70 template <typename T>
71 FifoBuffer<T>::FifoBuffer(size_t aCapacity)
72 {
73  FifoBuffer<T>::setCapacity(aCapacity);
74 }
75 
76 template <typename T>
77 void FifoBuffer<T>::setCapacity(size_t aCapacity)
78 {
79  std::lock_guard<std::recursive_mutex> guard(this->getLock());
81  buffer.reset(new T[this->getCapacity()]);
82 }
83 
84 template <typename T>
86 {
87  std::lock_guard<std::recursive_mutex> guard(this->getLock());
88  return {buffer.get(), this->capacity, this->readPos, this->writePos, this->size};
89 }
90 
91 template <typename T>
93 {
94  return buffer.get();
95 }
96 
97 template <typename T>
98 std::size_t FifoBuffer<T>::getSize() const
99 {
100  std::lock_guard<std::recursive_mutex> guard(FifoBufferBase<T>::lock);
102 }
103 
104 END_NAMESPACE_MEMORY
Definition: fifobuffer.h:18
The class FifoBufferBase is used to implement first-in-first-out (FIFO) buffers.
Definition: fifo_buffer_base.h:21
FifoBuffer(size_t aCapacity=0)
The constructor allocates memory at the heap and creates an instance.
Definition: fifobuffer.h:71
The class FifoBuffer implements a first-in-first-out (FIFO) data structure.
Definition: fifobuffer.h:31
virtual void setCapacity(size_t aCapacity)
Function allocates memory at the heap.
Definition: fifo_buffer_base.h:145
T * getData() const override
Function returns pointer to the internal buffer.
Definition: fifobuffer.h:92
FifoBufferStatus< T > getStatus()
Function returns FifoBuffer status which can be used for optimization.
Definition: fifobuffer.h:85
std::size_t getSize() const
Function returns current buffer size in values.
Definition: fifobuffer.h:98
void setCapacity(size_t aCapacity) override
Function allocates memory at the heap.
Definition: fifobuffer.h:77