dyn_limit.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //
  2. // dyn_limit.h
  3. // pokemon
  4. //
  5. // Created by Sam Jaffe on 12/1/23.
  6. // Copyright © 2023 Sam Jaffe. All rights reserved.
  7. //
  8. #pragma once
  9. #include <math/clamp.h>
  10. namespace math {
  11. template <typename T, T, T> class Bound;
  12. template <typename T> class DynBound final {
  13. public:
  14. using value_type = T;
  15. using underlying_type = value_type;
  16. private:
  17. value_type lower_bound_{};
  18. value_type upper_bound_{};
  19. value_type value_{};
  20. public:
  21. DynBound() = default;
  22. DynBound(value_type value, value_type lower_bound, value_type upper_bound)
  23. : lower_bound_(lower_bound), upper_bound_(upper_bound),
  24. value_(clamp(value, lower_bound_, upper_bound_)) {}
  25. template <value_type MINIMUM_VALUE, value_type MAXIMUM_VALUE>
  26. DynBound(Bound<value_type, MINIMUM_VALUE, MAXIMUM_VALUE> const & other)
  27. : lower_bound_(MINIMUM_VALUE), upper_bound_(MAXIMUM_VALUE),
  28. value_(other) {}
  29. operator value_type() const { return value_; }
  30. bool is_min() const { return value_ == lower_bound_; }
  31. bool is_max() const { return value_ == upper_bound_; }
  32. DynBound & operator+=(value_type by) {
  33. value_ = std::min<value_type>(upper_bound_, value_ + by);
  34. return *this;
  35. }
  36. DynBound & operator-=(value_type by) {
  37. value_ = std::max<value_type>(lower_bound_, value_ - by);
  38. return *this;
  39. }
  40. DynBound & operator++() { return operator+=(1); }
  41. DynBound & operator--() { return operator-=(1); }
  42. DynBound operator+(value_type by) const { return DynBound{*this} += by; }
  43. DynBound operator-(value_type by) const { return DynBound{*this} -= by; }
  44. DynBound operator++(int) {
  45. DynBound tmp{*this};
  46. operator++();
  47. return tmp;
  48. }
  49. DynBound operator--(int) {
  50. DynBound tmp{*this};
  51. operator--();
  52. return tmp;
  53. }
  54. };
  55. }