// // dyn_limit.h // pokemon // // Created by Sam Jaffe on 12/1/23. // Copyright © 2023 Sam Jaffe. All rights reserved. // #pragma once #include #include namespace math { template 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 DynBound(Bound 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_ + by), upper_bound_); return *this; } DynBound & operator-=(value_type by) { using std::max; value_ = max(static_cast(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; } }; }