Modified source engine (2017) developed by valve and leaked in 2020. Not for commercial purporses
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.

119 lines
2.3 KiB

5 years ago
//========= 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( IsFinite( value ) );
5 years ago
}
#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