Image Component Library (ICL)
ClippedCast.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLUtils/src/ICLUtils/ClippedCast.h **
10 ** Module : ICLUtils **
11 ** Authors: Christof Elbrechter **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <ICLUtils/CompatMacros.h>
34 #include <limits>
35 
36 namespace icl{
37  namespace utils{
39  template <class T>
40  inline T clip(T tX, T tMin, T tMax){
41  return tX < tMin ? tMin : tX > tMax ? tMax : tX;
42  }
43 
44 
45  template<class T>
46  inline bool is_float_type(){
47  return false;
48  }
49 
51  template<> inline bool is_float_type<float>() { return true; }
52  template<> inline bool is_float_type<double>() { return true; }
55  template<class S, class D>
57  inline D clipped_cast(S src){
58  if(is_float_type<D>()){ //hopefully this is const enough for optimize this expresseion out
59  return src < -(std::numeric_limits<D>::max)() ? -(std::numeric_limits<D>::max)() :
60  src > (std::numeric_limits<D>::max)() ? (std::numeric_limits<D>::max)() :
61  static_cast<D>(src);
62  }else{
63  return src < (std::numeric_limits<D>::min)() ? (std::numeric_limits<D>::min)() :
64  src > (std::numeric_limits<D>::max)() ? (std::numeric_limits<D>::max)() :
65  static_cast<D>(src);
66  }
67  }
68 
70 #define SPECIALISE_CLIPPED_CAST(T) template<> inline T clipped_cast<T,T>(T t) { return t; }
72  SPECIALISE_CLIPPED_CAST(int)
73  SPECIALISE_CLIPPED_CAST(unsigned int)
74  SPECIALISE_CLIPPED_CAST(char)
75  SPECIALISE_CLIPPED_CAST(unsigned char)
76  SPECIALISE_CLIPPED_CAST(short)
77  SPECIALISE_CLIPPED_CAST(unsigned short)
78  SPECIALISE_CLIPPED_CAST(long)
79  SPECIALISE_CLIPPED_CAST(unsigned long)
80  SPECIALISE_CLIPPED_CAST(bool)
81  SPECIALISE_CLIPPED_CAST(float)
82  SPECIALISE_CLIPPED_CAST(double)
83  #undef SPECIALISE_CLIPPED_CAST
84 
86  } // namespace utils
87 }
88 
89 
undocument this line if you encounter any issues!
Definition: Any.h:37
D clipped_cast(S src)
utility cast function wrapping the standard lib's numerical_limits template
Definition: ClippedCast.h:57
bool is_float_type()
Definition: ClippedCast.h:46
T clip(T tX, T tMin, T tMax)
clips a value into the range [tMin,tMax]
Definition: ClippedCast.h:40