doxyYoda 0.2.2
A modern Doxygen theme — CSS Grid, fog-over-fen, dark mode
☾
Toggle main menu visibility
Loading...
Searching...
No Matches
containers.hpp
Go to the documentation of this file.
1
11
12
#pragma once
13
14
#include <array>
15
#include <cstddef>
16
#include <stdexcept>
17
#include <string>
18
#include <variant>
19
#include <vector>
20
33
34
namespace
containers
{
35
36
// ─── Exceptions ──────────────────────────────────────────────────────────────
37
46
class
ContainerError
:
public
std::runtime_error {
47
public
:
48
using
std::runtime_error::runtime_error;
49
};
50
56
class
BufferOverflow
:
public
ContainerError
{
57
public
:
58
BufferOverflow
() :
ContainerError
(
"buffer overflow: container is full"
) {}
59
};
60
66
class
BufferUnderflow
:
public
ContainerError
{
67
public
:
68
BufferUnderflow
() :
ContainerError
(
"buffer underflow: container is empty"
) {}
69
};
70
71
// ─── Stack ───────────────────────────────────────────────────────────────────
72
84
template
<
typename
T>
85
class
Stack
{
86
public
:
92
void
push
(
const
T& value) {
93
data_.push_back(value);
94
}
95
102
T
pop
() {
103
if
(data_.empty())
throw
BufferUnderflow
();
104
T val = data_.back();
105
data_.pop_back();
106
return
val;
107
}
108
114
[[nodiscard]]
const
T&
top
()
const
{
115
if
(data_.empty())
throw
BufferUnderflow
();
116
return
data_.back();
117
}
118
120
[[nodiscard]]
bool
empty
()
const
{
return
data_.empty(); }
121
123
[[nodiscard]] std::size_t
size
()
const
{
return
data_.size(); }
124
126
void
clear
() { data_.clear(); }
127
128
private
:
129
std::vector<T> data_;
130
};
131
132
// ─── RingBuffer ──────────────────────────────────────────────────────────────
133
150
template
<
typename
T, std::
size_t
N>
151
class
RingBuffer
{
152
public
:
153
RingBuffer
() =
default
;
154
160
void
write
(
const
T& value) {
161
if
(
full
())
throw
BufferOverflow
();
162
buf_[tail_] = value;
163
tail_ = (tail_ + 1) % N;
164
++size_;
165
}
166
172
T
read
() {
173
if
(
empty
())
throw
BufferUnderflow
();
174
T val = buf_[head_];
175
head_ = (head_ + 1) % N;
176
--size_;
177
return
val;
178
}
179
181
[[nodiscard]]
bool
empty
()
const
{
return
size_ == 0; }
182
184
[[nodiscard]]
bool
full
()
const
{
return
size_ == N; }
185
187
[[nodiscard]] std::size_t
size
()
const
{
return
size_; }
188
190
[[nodiscard]]
constexpr
std::size_t
capacity
()
const
{
return
N; }
191
192
private
:
193
std::array<T, N> buf_{};
194
std::size_t head_ = 0;
195
std::size_t tail_ = 0;
196
std::size_t size_ = 0;
197
};
198
199
// ─── Result ──────────────────────────────────────────────────────────────────
200
219
template
<
typename
T,
typename
E = std::
string
>
220
class
Result {
221
public
:
227
static
Result
ok
(T
value
) {
return
Result(std::move(
value
)); }
228
234
static
Result
err
(E
error
) {
return
Result(ErrTag{}, std::move(
error
)); }
235
237
[[nodiscard]]
bool
is_ok
()
const
{
238
return
std::holds_alternative<T>(data_);
239
}
240
242
[[nodiscard]]
bool
is_err
()
const
{
return
!
is_ok
(); }
243
249
[[nodiscard]]
const
T&
value
()
const
{
return
std::get<T>(data_); }
250
256
[[nodiscard]]
const
E&
error
()
const
{
return
std::get<E>(data_); }
257
258
private
:
259
struct
ErrTag {};
260
explicit
Result(T
value
) : data_(std::move(
value
)) {}
261
Result(ErrTag, E
error
) : data_(std::move(
error
)) {}
262
263
std::variant<T, E> data_;
264
};
265
266
}
// namespace containers
267
// end of containers group
containers::BufferOverflow
Thrown when a fixed-capacity container is full.
Definition
containers.hpp:56
containers::BufferOverflow::BufferOverflow
BufferOverflow()
Definition
containers.hpp:58
containers::BufferUnderflow
Thrown when popping from an empty container.
Definition
containers.hpp:66
containers::BufferUnderflow::BufferUnderflow
BufferUnderflow()
Definition
containers.hpp:68
containers::ContainerError
Base exception for container errors.
Definition
containers.hpp:46
containers::Result::is_err
bool is_err() const
Check if this result is an error.
Definition
containers.hpp:242
containers::Result::is_ok
bool is_ok() const
Check if this result is a success.
Definition
containers.hpp:237
containers::Result::ok
static Result ok(T value)
Create a success result.
Definition
containers.hpp:227
containers::Result::error
const E & error() const
Unwrap the error.
Definition
containers.hpp:256
containers::Result::value
const T & value() const
Unwrap the success value.
Definition
containers.hpp:249
containers::Result::err
static Result err(E error)
Create an error result.
Definition
containers.hpp:234
containers::RingBuffer::capacity
constexpr std::size_t capacity() const
Get the maximum capacity.
Definition
containers.hpp:190
containers::RingBuffer::read
T read()
Read and remove the oldest value.
Definition
containers.hpp:172
containers::RingBuffer::size
std::size_t size() const
Get the number of elements currently stored.
Definition
containers.hpp:187
containers::RingBuffer::empty
bool empty() const
Check if the buffer is empty.
Definition
containers.hpp:181
containers::RingBuffer::RingBuffer
RingBuffer()=default
containers::RingBuffer::full
bool full() const
Check if the buffer is at capacity.
Definition
containers.hpp:184
containers::RingBuffer::write
void write(const T &value)
Write a value to the buffer.
Definition
containers.hpp:160
containers::Stack
A simple LIFO stack backed by std::vector.
Definition
containers.hpp:85
containers::Stack::clear
void clear()
Remove all elements.
Definition
containers.hpp:126
containers::Stack::push
void push(const T &value)
Push a value onto the top of the stack.
Definition
containers.hpp:92
containers::Stack::top
const T & top() const
Peek at the top element without removing it.
Definition
containers.hpp:114
containers::Stack::empty
bool empty() const
Check if the stack is empty.
Definition
containers.hpp:120
containers::Stack::pop
T pop()
Remove and return the top element.
Definition
containers.hpp:102
containers::Stack::size
std::size_t size() const
Get the number of elements.
Definition
containers.hpp:123
containers
Definition
containers.hpp:34
demo
src
containers.hpp
Generated by
1.17.0
Generated by
Doxygen 1.17.0
Analytics by
Antics
provided by
TurtleTech ehf