Jamba C++ API  5.0.0
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 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  *
16  * @author Yan Pujante
17  */
18 #ifndef __PONGASOFT_UTILS_MISC_H__
19 #define __PONGASOFT_UTILS_MISC_H__
20 
21 #include <pongasoft/logging/logging.h>
22 
23 namespace pongasoft::Utils {
24 
32 template <typename T, typename U>
33 inline static T clamp(const U &iValue, const T &iLower, const T &iUpper)
34 {
35  auto v = static_cast<T>(iValue);
36  return v < iLower ? iLower : (v > iUpper ? iUpper : v);
37 }
38 
43 template <typename T, typename U>
44 inline static T clampRange(const U &iValue, const T &iFrom, const T &iTo)
45 {
46  if(iFrom < iTo)
47  return clamp(iValue, iFrom, iTo);
48  else
49  return clamp(iValue, iTo, iFrom);
50 }
51 
57 template <typename T, typename U>
58 inline static T clampE(const U &value, const T &lower, const T &upper)
59 {
60  auto v = static_cast<T>(value);
61  DCHECK_F(v >= lower && v <= upper);
62  return v < lower ? lower : (v > upper ? upper : value);
63 }
64 
65 
66 }
67 
68 #endif // __PONGASOFT_UTILS_MISC_H__
static T clamp(const U &iValue, const T &iLower, const T &iUpper)
Make sure that the value remains within its bounds.
Definition: Misc.h:33
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:58
Definition: CircularBuffer.h:25
static T clampRange(const U &iValue, const T &iFrom, const T &iTo)
Make sure that the value remains within its bounds.
Definition: Misc.h:44