Skip to content

Instantly share code, notes, and snippets.

@leha-bot
Created December 12, 2016 21:04
Show Gist options
  • Save leha-bot/37807eb1a76efd452749f67449a5ebfa to your computer and use it in GitHub Desktop.
Save leha-bot/37807eb1a76efd452749f67449a5ebfa to your computer and use it in GitHub Desktop.
clamp T
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