diff options
Diffstat (limited to 'queue.h')
| -rw-r--r-- | queue.h | 29 |
1 files changed, 29 insertions, 0 deletions
| @@ -0,0 +1,29 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <cassert> | ||
| 4 | #include <deque> | ||
| 5 | #include <optional> | ||
| 6 | #include <utility> | ||
| 7 | |||
| 8 | #include "infra/port.h" | ||
| 9 | #include "infra/sim.h" | ||
| 10 | |||
| 11 | namespace infra { | ||
| 12 | template<typename T, unsigned int size> struct queue : public sim { | ||
| 13 | port<T> input; | ||
| 14 | port<T> *output = nullptr; | ||
| 15 | std::deque<T> elements; | ||
| 16 | |||
| 17 | void clock() { | ||
| 18 | if (input.can_read() && elements.size() < size) { | ||
| 19 | auto x = input.read(); | ||
| 20 | elements.emplace_back(std::move(x)); | ||
| 21 | } | ||
| 22 | if (output->can_write() && !elements.empty()) { | ||
| 23 | auto &x = elements.front(); | ||
| 24 | output->write(std::move(x)); | ||
| 25 | elements.pop_front(); | ||
| 26 | } | ||
| 27 | } | ||
| 28 | }; | ||
| 29 | } | ||
