BufferedValue.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #ifndef CORE_BUFFERED_VALUE_HPP
  2. #define CORE_BUFFERED_VALUE_HPP
  3. #include "math/Math.hpp"
  4. namespace Core {
  5. template<typename T>
  6. class BufferedValue final {
  7. T last;
  8. T current;
  9. public:
  10. BufferedValue(const T& t) : last(t), current(t) {
  11. }
  12. void update() {
  13. last = current;
  14. }
  15. T get(float lag) const {
  16. return Math::interpolate(last, current, lag);
  17. }
  18. template<typename O>
  19. BufferedValue& operator=(const O& o) {
  20. current = o;
  21. return *this;
  22. }
  23. template<typename O>
  24. BufferedValue& operator+=(const O& o) {
  25. current += o;
  26. return *this;
  27. }
  28. template<typename O>
  29. BufferedValue& operator-=(const O& o) {
  30. current -= o;
  31. return *this;
  32. }
  33. template<typename O>
  34. BufferedValue& operator*=(const O& o) {
  35. current *= o;
  36. return *this;
  37. }
  38. template<typename O>
  39. BufferedValue& operator/=(const O& o) {
  40. current /= o;
  41. return *this;
  42. }
  43. template<typename O>
  44. auto operator+(const O& o) const {
  45. return current + o;
  46. }
  47. template<typename O>
  48. auto operator-(const O& o) const {
  49. return current - o;
  50. }
  51. template<typename O>
  52. auto operator*(const O& o) const {
  53. return current * o;
  54. }
  55. template<typename O>
  56. auto operator/(const O& o) const {
  57. return current / o;
  58. }
  59. auto operator-() const {
  60. return -current;
  61. }
  62. auto& operator[](int index) {
  63. return current[index];
  64. }
  65. const auto& operator[](int index) const {
  66. return current[index];
  67. }
  68. operator T&() {
  69. return current;
  70. }
  71. operator const T&() const {
  72. return current;
  73. }
  74. };
  75. }
  76. #endif