BufferedValue.h 1.7 KB

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