-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib::egress_buffer.hpp
More file actions
72 lines (55 loc) · 1.75 KB
/
lib::egress_buffer.hpp
File metadata and controls
72 lines (55 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#pragma once
#include<new>
#include<type_traits>
#include<vector>
#include<utility>
#include<cassert>
#include "lib::type_trait.hpp"
namespace lib{
template<typename T,std::size_t capacity_ = 1026,typename Policy = no_overflow, typename Container = std::array<T,capacity_>>
class egress_buffer{
private:
std::size_t head_; // pointer index to index that will be removed
std::size_t tail_; // pointer index to free slot
std::size_t size_; // current size of the array we will use
Container storage_; // default is set to std::array
public:
constexpr egress_buffer()noexcept : head_(0),tail_(0),size_(0){}
void clear(){
if(this->size_ != 0){
ptr().~T();
}
}
~egress_buffer(){
this->clear();
}
constexpr std::size_t size()const noexcept{return this->size_;}
constexpr std::size_t capacity() const noexcept{return this->capacity_;}
constexpr bool empty() const noexcept{return this->size_ == 0;}
constexpr bool full() const noexcept{return this->size_ == this->capacity_;}
/* 0 <= size <= capacity
* tail_ = (head_ + size_) % capacityi_;
*/
bool push(T&& value) {
if(this->size_ == this->capacity_){
return false;
}
storage_[tail_] = std::move(value);
this->tail_ = (tail_+1)%capacity_;
this->size_++;
return true;
}
bool pop(T& out) {
if(this->size_ == 0){
return false;
}
out = std::move(storage_[head_]);
this->head_ = (head_+ 1)%capacity_;
--this->size_;
return true;
}
};
struct no_overflow(){};
struct tail_drop(){};
struct overwrite(){};
};