Counter Strike : Global Offensive Source Code
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.

113 lines
2.2 KiB

  1. //========= Copyright � 1996-2005, 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( IsFinite( 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. // Clamp the value to its limits. Interpolation code uses this after interpolating.
  55. inline void Clamp()
  56. {
  57. if ( m_Val < minValue )
  58. m_Val = minValue;
  59. else if ( m_Val > maxValue )
  60. m_Val = maxValue;
  61. }
  62. inline operator const T&() const
  63. {
  64. return m_Val;
  65. }
  66. inline CRangeCheckedVar<T, minValue, maxValue, startValue>& operator=( const T &value )
  67. {
  68. RangeCheck( value, minValue, maxValue );
  69. m_Val = value;
  70. return *this;
  71. }
  72. inline CRangeCheckedVar<T, minValue, maxValue, startValue>& operator+=( const T &value )
  73. {
  74. return (*this = m_Val + value);
  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. private:
  89. T m_Val;
  90. };
  91. #endif // RANGECHECKEDVAR_H