1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
|