Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
kcenon::common::utils::CircularBuffer< T, Capacity > Class Template Referenceexport

Thread-safe fixed-size circular buffer. More...

#include <circular_buffer.h>

Collaboration diagram for kcenon::common::utils::CircularBuffer< T, Capacity >:
Collaboration graph

Public Member Functions

 CircularBuffer ()=default
 
bool push (const T &value, bool overwrite=false)
 
bool push (T &&value, bool overwrite=false)
 
std::optional< T > pop ()
 
bool empty () const
 
bool full () const
 
std::size_t size () const
 
constexpr std::size_t capacity () const
 
 CircularBuffer ()=default
 
bool push (const T &value, bool overwrite=false)
 Push a value to the buffer.
 
bool push (T &&value, bool overwrite=false)
 
std::optional< T > pop ()
 Pop a value from the buffer.
 
bool empty () const
 
bool full () const
 
std::size_t size () const
 
constexpr std::size_t capacity () const
 

Private Member Functions

void advance (std::size_t &index) noexcept
 
bool is_full_locked () const noexcept
 
std::optional< T > pop_locked ()
 
void advance (std::size_t &index) noexcept
 
bool is_full_locked () const noexcept
 
std::optional< T > pop_locked ()
 

Private Attributes

std::mutex mutex_
 
std::array< T, Capacity > buffer_ {}
 
std::size_t head_ {0}
 
std::size_t tail_ {0}
 
std::size_t size_ {0}
 

Detailed Description

template<typename T, std::size_t Capacity>
class kcenon::common::utils::CircularBuffer< T, Capacity >

Thread-safe fixed-size circular buffer.

Template Parameters
TElement type
CapacityMaximum number of elements
Examples
utility_containers_example.cpp.

Definition at line 100 of file utils.cppm.

Constructor & Destructor Documentation

◆ CircularBuffer() [1/2]

template<typename T , std::size_t Capacity>
kcenon::common::utils::CircularBuffer< T, Capacity >::CircularBuffer ( )
default

◆ CircularBuffer() [2/2]

template<typename T , std::size_t Capacity>
kcenon::common::utils::CircularBuffer< T, Capacity >::CircularBuffer ( )
exportdefault

Member Function Documentation

◆ advance() [1/2]

template<typename T , std::size_t Capacity>
void kcenon::common::utils::CircularBuffer< T, Capacity >::advance ( std::size_t & index)
inlineprivatenoexcept

Definition at line 84 of file circular_buffer.h.

84 {
85 index = (index + 1) % Capacity;
86 }

Referenced by kcenon::common::utils::CircularBuffer< T, Capacity >::pop_locked(), kcenon::common::utils::CircularBuffer< T, Capacity >::push(), and kcenon::common::utils::CircularBuffer< T, Capacity >::push().

Here is the caller graph for this function:

◆ advance() [2/2]

template<typename T , std::size_t Capacity>
void kcenon::common::utils::CircularBuffer< T, Capacity >::advance ( std::size_t & index)
inlineexportprivatenoexcept

Definition at line 169 of file utils.cppm.

169 {
170 index = (index + 1) % Capacity;
171 }

◆ capacity() [1/2]

template<typename T , std::size_t Capacity>
std::size_t kcenon::common::utils::CircularBuffer< T, Capacity >::capacity ( ) const
inlinenodiscardconstexpr

Definition at line 79 of file circular_buffer.h.

79 {
80 return Capacity;
81 }

◆ capacity() [2/2]

template<typename T , std::size_t Capacity>
std::size_t kcenon::common::utils::CircularBuffer< T, Capacity >::capacity ( ) const
inlinenodiscardconstexprexport

Definition at line 164 of file utils.cppm.

164 {
165 return Capacity;
166 }

◆ empty() [1/2]

template<typename T , std::size_t Capacity>
bool kcenon::common::utils::CircularBuffer< T, Capacity >::empty ( ) const
inlinenodiscard
Examples
utility_containers_example.cpp.

Definition at line 64 of file circular_buffer.h.

64 {
65 std::lock_guard<std::mutex> lock(mutex_);
66 return size_ == 0;
67 }

References kcenon::common::utils::CircularBuffer< T, Capacity >::mutex_, and kcenon::common::utils::CircularBuffer< T, Capacity >::size_.

Referenced by main().

Here is the caller graph for this function:

◆ empty() [2/2]

template<typename T , std::size_t Capacity>
bool kcenon::common::utils::CircularBuffer< T, Capacity >::empty ( ) const
inlinenodiscardexport

Definition at line 149 of file utils.cppm.

149 {
150 std::lock_guard<std::mutex> lock(mutex_);
151 return size_ == 0;
152 }

References kcenon::common::utils::CircularBuffer< T, Capacity >::mutex_, and kcenon::common::utils::CircularBuffer< T, Capacity >::size_.

◆ full() [1/2]

template<typename T , std::size_t Capacity>
bool kcenon::common::utils::CircularBuffer< T, Capacity >::full ( ) const
inlinenodiscard
Examples
utility_containers_example.cpp.

Definition at line 69 of file circular_buffer.h.

69 {
70 std::lock_guard<std::mutex> lock(mutex_);
71 return size_ == Capacity;
72 }

References kcenon::common::utils::CircularBuffer< T, Capacity >::mutex_, and kcenon::common::utils::CircularBuffer< T, Capacity >::size_.

Referenced by main().

Here is the caller graph for this function:

◆ full() [2/2]

template<typename T , std::size_t Capacity>
bool kcenon::common::utils::CircularBuffer< T, Capacity >::full ( ) const
inlinenodiscardexport

Definition at line 154 of file utils.cppm.

154 {
155 std::lock_guard<std::mutex> lock(mutex_);
156 return size_ == Capacity;
157 }

References kcenon::common::utils::CircularBuffer< T, Capacity >::mutex_, and kcenon::common::utils::CircularBuffer< T, Capacity >::size_.

◆ is_full_locked() [1/2]

template<typename T , std::size_t Capacity>
bool kcenon::common::utils::CircularBuffer< T, Capacity >::is_full_locked ( ) const
inlineprivatenoexcept

Definition at line 88 of file circular_buffer.h.

88 {
89 return size_ == Capacity;
90 }

References kcenon::common::utils::CircularBuffer< T, Capacity >::size_.

Referenced by kcenon::common::utils::CircularBuffer< T, Capacity >::push(), and kcenon::common::utils::CircularBuffer< T, Capacity >::push().

Here is the caller graph for this function:

◆ is_full_locked() [2/2]

template<typename T , std::size_t Capacity>
bool kcenon::common::utils::CircularBuffer< T, Capacity >::is_full_locked ( ) const
inlineexportprivatenoexcept

Definition at line 173 of file utils.cppm.

173 {
174 return size_ == Capacity;
175 }

References kcenon::common::utils::CircularBuffer< T, Capacity >::size_.

◆ pop() [1/2]

template<typename T , std::size_t Capacity>
std::optional< T > kcenon::common::utils::CircularBuffer< T, Capacity >::pop ( )
inlinenodiscard
Examples
utility_containers_example.cpp.

Definition at line 59 of file circular_buffer.h.

59 {
60 std::lock_guard<std::mutex> lock(mutex_);
61 return pop_locked();
62 }

References kcenon::common::utils::CircularBuffer< T, Capacity >::mutex_, and kcenon::common::utils::CircularBuffer< T, Capacity >::pop_locked().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pop() [2/2]

template<typename T , std::size_t Capacity>
std::optional< T > kcenon::common::utils::CircularBuffer< T, Capacity >::pop ( )
inlinenodiscardexport

Pop a value from the buffer.

Returns
The oldest value, or nullopt if empty

Definition at line 144 of file utils.cppm.

144 {
145 std::lock_guard<std::mutex> lock(mutex_);
146 return pop_locked();
147 }

References kcenon::common::utils::CircularBuffer< T, Capacity >::mutex_, and kcenon::common::utils::CircularBuffer< T, Capacity >::pop_locked().

Here is the call graph for this function:

◆ pop_locked() [1/2]

template<typename T , std::size_t Capacity>
std::optional< T > kcenon::common::utils::CircularBuffer< T, Capacity >::pop_locked ( )
inlineprivate

◆ pop_locked() [2/2]

template<typename T , std::size_t Capacity>
std::optional< T > kcenon::common::utils::CircularBuffer< T, Capacity >::pop_locked ( )
inlineexportprivate

Definition at line 177 of file utils.cppm.

177 {
178 if (size_ == 0) {
179 return std::nullopt;
180 }
181 auto value = std::move(buffer_[head_]);
182 advance(head_);
183 --size_;
184 return value;
185 }

References kcenon::common::utils::CircularBuffer< T, Capacity >::advance(), kcenon::common::utils::CircularBuffer< T, Capacity >::buffer_, kcenon::common::utils::CircularBuffer< T, Capacity >::head_, and kcenon::common::utils::CircularBuffer< T, Capacity >::size_.

Here is the call graph for this function:

◆ push() [1/4]

template<typename T , std::size_t Capacity>
bool kcenon::common::utils::CircularBuffer< T, Capacity >::push ( const T & value,
bool overwrite = false )
inline

◆ push() [2/4]

template<typename T , std::size_t Capacity>
bool kcenon::common::utils::CircularBuffer< T, Capacity >::push ( const T & value,
bool overwrite = false )
inlineexport

Push a value to the buffer.

Parameters
valueValue to push
overwriteIf true, overwrite oldest value when full
Returns
true if pushed successfully, false if full and overwrite is false

Definition at line 112 of file utils.cppm.

112 {
113 std::lock_guard<std::mutex> lock(mutex_);
114 if (is_full_locked() && !overwrite) {
115 return false;
116 }
117 if (is_full_locked()) {
118 pop_locked();
119 }
120 buffer_[tail_] = value;
121 advance(tail_);
122 ++size_;
123 return true;
124 }

References kcenon::common::utils::CircularBuffer< T, Capacity >::advance(), kcenon::common::utils::CircularBuffer< T, Capacity >::buffer_, kcenon::common::utils::CircularBuffer< T, Capacity >::is_full_locked(), kcenon::common::utils::CircularBuffer< T, Capacity >::mutex_, kcenon::common::utils::CircularBuffer< T, Capacity >::pop_locked(), kcenon::common::utils::CircularBuffer< T, Capacity >::size_, and kcenon::common::utils::CircularBuffer< T, Capacity >::tail_.

Here is the call graph for this function:

◆ push() [3/4]

template<typename T , std::size_t Capacity>
bool kcenon::common::utils::CircularBuffer< T, Capacity >::push ( T && value,
bool overwrite = false )
inline

◆ push() [4/4]

template<typename T , std::size_t Capacity>
bool kcenon::common::utils::CircularBuffer< T, Capacity >::push ( T && value,
bool overwrite = false )
inlineexport

◆ size() [1/2]

template<typename T , std::size_t Capacity>
std::size_t kcenon::common::utils::CircularBuffer< T, Capacity >::size ( ) const
inlinenodiscard
Examples
utility_containers_example.cpp.

Definition at line 74 of file circular_buffer.h.

74 {
75 std::lock_guard<std::mutex> lock(mutex_);
76 return size_;
77 }

References kcenon::common::utils::CircularBuffer< T, Capacity >::mutex_, and kcenon::common::utils::CircularBuffer< T, Capacity >::size_.

Referenced by main().

Here is the caller graph for this function:

◆ size() [2/2]

template<typename T , std::size_t Capacity>
std::size_t kcenon::common::utils::CircularBuffer< T, Capacity >::size ( ) const
inlinenodiscardexport

Definition at line 159 of file utils.cppm.

159 {
160 std::lock_guard<std::mutex> lock(mutex_);
161 return size_;
162 }

References kcenon::common::utils::CircularBuffer< T, Capacity >::mutex_, and kcenon::common::utils::CircularBuffer< T, Capacity >::size_.

Member Data Documentation

◆ buffer_

template<typename T , std::size_t Capacity>
std::array< T, Capacity > kcenon::common::utils::CircularBuffer< T, Capacity >::buffer_ {}
exportprivate

◆ head_

template<typename T , std::size_t Capacity>
std::size_t kcenon::common::utils::CircularBuffer< T, Capacity >::head_ {0}
exportprivate

◆ mutex_

◆ size_

◆ tail_

template<typename T , std::size_t Capacity>
std::size_t kcenon::common::utils::CircularBuffer< T, Capacity >::tail_ {0}
exportprivate

The documentation for this class was generated from the following files: