Created
December 12, 2016 21:04
-
-
Save leha-bot/37807eb1a76efd452749f67449a5ebfa to your computer and use it in GitHub Desktop.
clamp T
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename T> | |
class ConstraintUnit { | |
T minv, maxv, v; | |
typedef const T &crefT; | |
ConstraintUnit(const T &minval, const T &maxval) | |
: minv(minval), maxv (maxval) | |
{ | |
} | |
void lower(const T& val) | |
{ | |
minv = val; | |
} | |
void upper(const T& val) | |
{ | |
maxv = val; | |
} | |
const T &clamped() const | |
{ | |
return v > maxv ? maxv : | |
v < minv ? minv : v; | |
} | |
const T &value() const | |
{ | |
return v; | |
} | |
const T &clamp() | |
{ | |
v = clamped(); | |
return v; | |
} | |
const T &setval(const T &newv) | |
{ | |
v = newv; | |
return clamp(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment