Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
2.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef RANGECHECKEDVAR_H
  7. #define RANGECHECKEDVAR_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "tier0/dbg.h"
  12. #include "tier0/threadtools.h"
  13. #include "mathlib/vector.h"
  14. #include <float.h>
  15. // Use this to disable range checks within a scope.
  16. class CDisableRangeChecks
  17. {
  18. public:
  19. CDisableRangeChecks();
  20. ~CDisableRangeChecks();
  21. };
  22. template< class T >
  23. inline void RangeCheck( const T &value, int minValue, int maxValue )
  24. {
  25. #ifdef _DEBUG
  26. extern bool g_bDoRangeChecks;
  27. if ( ThreadInMainThread() && g_bDoRangeChecks )
  28. {
  29. // Ignore the min/max stuff for now.. just make sure it's not a NAN.
  30. Assert( _finite( value ) );
  31. }
  32. #endif
  33. }
  34. inline void RangeCheck( const Vector &value, int minValue, int maxValue )
  35. {
  36. #ifdef _DEBUG
  37. RangeCheck( value.x, minValue, maxValue );
  38. RangeCheck( value.y, minValue, maxValue );
  39. RangeCheck( value.z, minValue, maxValue );
  40. #endif
  41. }
  42. template< class T, int minValue, int maxValue, int startValue >
  43. class CRangeCheckedVar
  44. {
  45. public:
  46. inline CRangeCheckedVar()
  47. {
  48. m_Val = startValue;
  49. }
  50. inline CRangeCheckedVar( const T &value )
  51. {
  52. *this = value;
  53. }
  54. T GetRaw() const
  55. {
  56. return m_Val;
  57. }
  58. // Clamp the value to its limits. Interpolation code uses this after interpolating.
  59. inline void Clamp()
  60. {
  61. if ( m_Val < minValue )
  62. m_Val = minValue;
  63. else if ( m_Val > maxValue )
  64. m_Val = maxValue;
  65. }
  66. inline operator const T&() const
  67. {
  68. return m_Val;
  69. }
  70. inline CRangeCheckedVar<T, minValue, maxValue, startValue>& operator=( const T &value )
  71. {
  72. RangeCheck( value, minValue, maxValue );
  73. m_Val = value;
  74. return *this;
  75. }
  76. inline CRangeCheckedVar<T, minValue, maxValue, startValue>& operator+=( const T &value )
  77. {
  78. return (*this = m_Val + value);
  79. }
  80. inline CRangeCheckedVar<T, minValue, maxValue, startValue>& operator-=( const T &value )
  81. {
  82. return (*this = m_Val - value);
  83. }
  84. inline CRangeCheckedVar<T, minValue, maxValue, startValue>& operator*=( const T &value )
  85. {
  86. return (*this = m_Val * value);
  87. }
  88. inline CRangeCheckedVar<T, minValue, maxValue, startValue>& operator/=( const T &value )
  89. {
  90. return (*this = m_Val / value);
  91. }
  92. private:
  93. T m_Val;
  94. };
  95. #endif // RANGECHECKEDVAR_H