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:
@@ -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_{};
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user