Jamba C++ API 7.5.0
Loading...
Searching...
No Matches
Misc.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018-2019 pongasoft
3 *
4 * Licensed under the Apache License, Version 2.0 or the MIT license,
5 * at your option. You may not use this file except in compliance with
6 * one of these licenses. You may obtain copies of the licenses at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 * https://opensource.org/licenses/MIT
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 *
17 * @author Yan Pujante
18 */
19#ifndef __PONGASOFT_UTILS_MISC_H__
20#define __PONGASOFT_UTILS_MISC_H__
21
22#include <pongasoft/logging/logging.h>
23
24namespace pongasoft::Utils {
25
33template <typename T, typename U>
34inline static T clamp(const U &iValue, const T &iLower, const T &iUpper)
35{
36 auto v = static_cast<T>(iValue);
37 return v < iLower ? iLower : (v > iUpper ? iUpper : v);
38}
39
44template <typename T, typename U>
45inline static T clampRange(const U &iValue, const T &iFrom, const T &iTo)
46{
47 if(iFrom < iTo)
48 return clamp(iValue, iFrom, iTo);
49 else
50 return clamp(iValue, iTo, iFrom);
51}
52
58template <typename T, typename U>
59inline static T clampE(const U &value, const T &lower, const T &upper)
60{
61 auto v = static_cast<T>(value);
62 DCHECK_F(v >= lower && v <= upper);
63 return v < lower ? lower : (v > upper ? upper : value);
64}
65
66
67}
68
69#endif // __PONGASOFT_UTILS_MISC_H__
Definition CircularBuffer.h:26
static T clamp(const U &iValue, const T &iLower, const T &iUpper)
Make sure that the value remains within its bounds.
Definition Misc.h:34
static T clampE(const U &value, const T &lower, const T &upper)
Same as clamp except it will actually fail/assert in debug mode.
Definition Misc.h:59
static T clampRange(const U &iValue, const T &iFrom, const T &iTo)
Make sure that the value remains within its bounds.
Definition Misc.h:45