AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
CircularBuffer< T > Class Template Reference

#include "CircularBuffer.h"

Public Member Functions

 CircularBuffer (std::size_t size)
 
void put (T item)
 
bool empty () const
 
bool full () const
 
std::size_t capacity () const
 
std::size_t size () const
 
std::vector< T > content ()
 
peak_back ()
 

Private Attributes

std::mutex mutex_
 
std::unique_ptr< T[]> buf_
 
std::size_t head_ = 0
 
std::size_t tail_ = 0
 
const std::size_t max_size_
 
bool full_ = false
 

Detailed Description

template<typename T>
class CircularBuffer< T >

Constructor & Destructor Documentation

◆ CircularBuffer()

template<typename T >
CircularBuffer< T >::CircularBuffer ( std::size_t  size)
inlineexplicit
20 :
21 buf_(std::unique_ptr<T[]>(new T[size])),
23 {
24
25 }
const std::size_t max_size_
Definition: CircularBuffer.h:100
std::unique_ptr< T[]> buf_
Definition: CircularBuffer.h:97
std::size_t size() const
Definition: CircularBuffer.h:60

Member Function Documentation

◆ capacity()

template<typename T >
std::size_t CircularBuffer< T >::capacity ( ) const
inline
56 {
57 return max_size_;
58 }

References CircularBuffer< T >::max_size_.

◆ content()

template<typename T >
std::vector< T > CircularBuffer< T >::content ( )
inline
82 {
83 std::lock_guard<std::mutex> lock(mutex_);
84
85 return std::vector<T>(buf_.get(), buf_.get() + size());
86 }
std::mutex mutex_
Definition: CircularBuffer.h:96

References CircularBuffer< T >::buf_, CircularBuffer< T >::mutex_, and CircularBuffer< T >::size().

Referenced by WorldSession::ComputeNewClockDelta().

◆ empty()

template<typename T >
bool CircularBuffer< T >::empty ( ) const
inline
44 {
45 //if head and tail are equal, we are empty
46 return (!full_ && (head_ == tail_));
47 }
std::size_t tail_
Definition: CircularBuffer.h:99
bool full_
Definition: CircularBuffer.h:101
std::size_t head_
Definition: CircularBuffer.h:98

References CircularBuffer< T >::full_, CircularBuffer< T >::head_, and CircularBuffer< T >::tail_.

Referenced by CircularBuffer< T >::peak_back().

◆ full()

template<typename T >
bool CircularBuffer< T >::full ( ) const
inline
50 {
51 //If tail is ahead the head by 1, we are full
52 return full_;
53 }

References CircularBuffer< T >::full_.

◆ peak_back()

template<typename T >
T CircularBuffer< T >::peak_back ( )
inline
89 {
90 std::lock_guard<std::mutex> lock(mutex_);
91
92 return empty() ? T() : buf_[tail_];
93 }
bool empty() const
Definition: CircularBuffer.h:43

References CircularBuffer< T >::buf_, CircularBuffer< T >::empty(), CircularBuffer< T >::mutex_, and CircularBuffer< T >::tail_.

Referenced by WorldSession::ComputeNewClockDelta().

◆ put()

template<typename T >
void CircularBuffer< T >::put ( item)
inline
28 {
29 std::lock_guard<std::mutex> lock(mutex_);
30
31 buf_[head_] = item;
32
33 if (full_)
34 {
35 tail_ = (tail_ + 1) % max_size_;
36 }
37
38 head_ = (head_ + 1) % max_size_;
39
40 full_ = head_ == tail_;
41 }

References CircularBuffer< T >::buf_, CircularBuffer< T >::full_, CircularBuffer< T >::head_, CircularBuffer< T >::max_size_, CircularBuffer< T >::mutex_, and CircularBuffer< T >::tail_.

Referenced by WorldSession::HandleTimeSyncResp().

◆ size()

template<typename T >
std::size_t CircularBuffer< T >::size ( ) const
inline
61 {
62 std::size_t size = max_size_;
63
64 if (!full_)
65 {
66 if (head_ >= tail_)
67 {
68 size = head_ - tail_;
69 }
70 else
71 {
72 size += head_ - tail_;
73 }
74 }
75
76 return size;
77 }

References CircularBuffer< T >::full_, CircularBuffer< T >::head_, CircularBuffer< T >::max_size_, CircularBuffer< T >::size(), and CircularBuffer< T >::tail_.

Referenced by CircularBuffer< T >::content(), and CircularBuffer< T >::size().

Member Data Documentation

◆ buf_

template<typename T >
std::unique_ptr<T[]> CircularBuffer< T >::buf_
private

◆ full_

template<typename T >
bool CircularBuffer< T >::full_ = false
private

◆ head_

template<typename T >
std::size_t CircularBuffer< T >::head_ = 0
private

◆ max_size_

template<typename T >
const std::size_t CircularBuffer< T >::max_size_
private

◆ mutex_

template<typename T >
std::mutex CircularBuffer< T >::mutex_
private

◆ tail_

template<typename T >
std::size_t CircularBuffer< T >::tail_ = 0
private