| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- //
- // dyn_limit.h
- // pokemon
- //
- // Created by Sam Jaffe on 12/1/23.
- // Copyright © 2023 Sam Jaffe. All rights reserved.
- //
- #pragma once
- #include <math/clamp.h>
- #include <math/limit.h>
- namespace math {
- template <typename T, typename B = T> class DynBound final {
- public:
- using value_type = T;
- using underlying_type = value_type const &;
- using bound_type = B;
- private:
- bound_type lower_bound_{};
- bound_type upper_bound_{};
- value_type value_{};
- public:
- DynBound() = default;
- DynBound(value_type value, bound_type lower_bound, bound_type upper_bound)
- : lower_bound_(lower_bound), upper_bound_(upper_bound),
- value_(clamp(value, lower_bound_, upper_bound_)) {}
- template <bound_type MINIMUM_VALUE, bound_type MAXIMUM_VALUE>
- DynBound(Bound<value_type, MINIMUM_VALUE, MAXIMUM_VALUE> const & other)
- : lower_bound_(MINIMUM_VALUE), upper_bound_(MAXIMUM_VALUE),
- value_(other) {}
- explicit operator value_type const &() const { return value_; }
- value_type const &operator*() const { return value_; }
- value_type const *operator->() const { return &value_; }
-
- auto operator<=>(DynBound const &other) const noexcept = default;
- bool is_min() const { return value_ == lower_bound_; }
- bool is_max() const { return value_ == upper_bound_; }
- DynBound & operator+=(value_type by) {
- using std::min;
- value_ = min(static_cast<value_type>(value_ + by), upper_bound_);
- return *this;
- }
- DynBound & operator-=(value_type by) {
- using std::max;
- value_ = max(static_cast<value_type>(value_ - by), lower_bound_);
- return *this;
- }
- DynBound & operator++() { return operator+=(1); }
- DynBound & operator--() { return operator-=(1); }
- DynBound operator+(value_type by) const { return DynBound{*this} += by; }
- DynBound operator-(value_type by) const { return DynBound{*this} -= by; }
- DynBound operator++(int) {
- DynBound tmp{*this};
- operator++();
- return tmp;
- }
- DynBound operator--(int) {
- DynBound tmp{*this};
- operator--();
- return tmp;
- }
- };
- }
|