diff options
| author | Joe Ludwig <[email protected]> | 2013-06-26 15:22:04 -0700 |
|---|---|---|
| committer | Joe Ludwig <[email protected]> | 2013-06-26 15:22:04 -0700 |
| commit | 39ed87570bdb2f86969d4be821c94b722dc71179 (patch) | |
| tree | abc53757f75f40c80278e87650ea92808274aa59 /sp/src/public/tier1/rangecheckedvar.h | |
| download | source-sdk-2013-39ed87570bdb2f86969d4be821c94b722dc71179.tar.xz source-sdk-2013-39ed87570bdb2f86969d4be821c94b722dc71179.zip | |
First version of the SOurce SDK 2013
Diffstat (limited to 'sp/src/public/tier1/rangecheckedvar.h')
| -rw-r--r-- | sp/src/public/tier1/rangecheckedvar.h | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/sp/src/public/tier1/rangecheckedvar.h b/sp/src/public/tier1/rangecheckedvar.h new file mode 100644 index 00000000..8d9eba70 --- /dev/null +++ b/sp/src/public/tier1/rangecheckedvar.h @@ -0,0 +1,118 @@ +//========= Copyright Valve Corporation, All rights reserved. ============//
+//
+// Purpose:
+//
+//=============================================================================//
+
+#ifndef RANGECHECKEDVAR_H
+#define RANGECHECKEDVAR_H
+#ifdef _WIN32
+#pragma once
+#endif
+
+
+#include "tier0/dbg.h"
+#include "tier0/threadtools.h"
+#include "mathlib/vector.h"
+#include <float.h>
+
+
+// Use this to disable range checks within a scope.
+class CDisableRangeChecks
+{
+public:
+ CDisableRangeChecks();
+ ~CDisableRangeChecks();
+};
+
+
+template< class T >
+inline void RangeCheck( const T &value, int minValue, int maxValue )
+{
+#ifdef _DEBUG
+ extern bool g_bDoRangeChecks;
+ if ( ThreadInMainThread() && g_bDoRangeChecks )
+ {
+ // Ignore the min/max stuff for now.. just make sure it's not a NAN.
+ Assert( _finite( value ) );
+ }
+#endif
+}
+
+inline void RangeCheck( const Vector &value, int minValue, int maxValue )
+{
+#ifdef _DEBUG
+ RangeCheck( value.x, minValue, maxValue );
+ RangeCheck( value.y, minValue, maxValue );
+ RangeCheck( value.z, minValue, maxValue );
+#endif
+}
+
+
+template< class T, int minValue, int maxValue, int startValue >
+class CRangeCheckedVar
+{
+public:
+
+ inline CRangeCheckedVar()
+ {
+ m_Val = startValue;
+ }
+
+ inline CRangeCheckedVar( const T &value )
+ {
+ *this = value;
+ }
+
+ T GetRaw() const
+ {
+ return m_Val;
+ }
+
+ // Clamp the value to its limits. Interpolation code uses this after interpolating.
+ inline void Clamp()
+ {
+ if ( m_Val < minValue )
+ m_Val = minValue;
+ else if ( m_Val > maxValue )
+ m_Val = maxValue;
+ }
+
+ inline operator const T&() const
+ {
+ return m_Val;
+ }
+
+ inline CRangeCheckedVar<T, minValue, maxValue, startValue>& operator=( const T &value )
+ {
+ RangeCheck( value, minValue, maxValue );
+ m_Val = value;
+ return *this;
+ }
+
+ inline CRangeCheckedVar<T, minValue, maxValue, startValue>& operator+=( const T &value )
+ {
+ return (*this = m_Val + value);
+ }
+
+ inline CRangeCheckedVar<T, minValue, maxValue, startValue>& operator-=( const T &value )
+ {
+ return (*this = m_Val - value);
+ }
+
+ inline CRangeCheckedVar<T, minValue, maxValue, startValue>& operator*=( const T &value )
+ {
+ return (*this = m_Val * value);
+ }
+
+ inline CRangeCheckedVar<T, minValue, maxValue, startValue>& operator/=( const T &value )
+ {
+ return (*this = m_Val / value);
+ }
+
+private:
+
+ T m_Val;
+};
+
+#endif // RANGECHECKEDVAR_H
|