1
0
mirror of https://github.com/TomHarte/CLK.git synced 2026-04-20 10:17:05 +00:00

Factor out the stuff of being a circular counter.

This commit is contained in:
Thomas Harte
2026-02-15 13:10:24 -05:00
parent a8761bdd43
commit 5abff02d56
4 changed files with 57 additions and 5 deletions
+48
View File
@@ -0,0 +1,48 @@
//
// CircularCounter.hpp
// Clock Signal
//
// Created by Thomas Harte on 15/02/2026.
// Copyright © 2026 Thomas Harte. All rights reserved.
//
#include <cassert>
namespace Numeric {
template <typename IntT, IntT limit>
class CircularCounter {
public:
constexpr CircularCounter() noexcept = default;
constexpr CircularCounter(const IntT value) noexcept : value_(value) {
assert(value < limit);
}
CircularCounter &operator ++() {
++value_;
if(value_ == limit) {
value_ = 0;
}
return *this;
}
CircularCounter operator ++(int) {
const auto result = *this;
++*this;
return result;
}
operator IntT() const {
return value_;
}
CircularCounter &operator = (const IntT rhs) {
value_ = rhs;
return *this;
}
private:
IntT value_{};
};
}