Listing 1

/* constrained_value.hpp
   constrained_value type mini-library 
   author: Christopher Diggins, http://www.cdiggins.com  
   based on the constrained_value type by: Jeff Garland, 
   http://www.crystalclearsoftware.com/    
   license available at http://www.boost.org/LICENSE_1_0.txt    
   last modified: August 19, 2004 
*/
#ifndef CONSTRAINED_VALUE_HPP
#define CONSTRAINED_VALUE_HPP

namespace cv
{            
  template < typename constraints_policy >
  class constrained_value {
    public:
      typedef constrained_value < constraints_policy > self;
      typedef typename constraints_policy::value value;         
      constrained_value() { }
      constrained_value(const self& x) 
                          { constraints_policy::assign(x.get_value(), m); } 
      constrained_value(const value& x) 
                          { constraints_policy::assign(x, m); }
      const value get_value() const { return m; }
      operator value() { return m; }
      self& operator=(const self& x) 
             { constraints_policy::assign(x.get_value(), m); return *this; }
      self& operator=(const value& x) 
             { constraints_policy::assign(x, m); return *this; }        
    private:
      value m;     
  };          
  namespace policies 
  {
    struct throwing {
      template <typename T> 
      static void on_below(const T& rvalue, T& lvalue, const T& min) 
                                                            { throw 0; }
      template <typename T> 
      static void on_above(const T& rvalue, T& lvalue, const T& max) 
                                                            { throw 0; }
    };
    struct saturating {
      template <typename T> 
      static void on_below(const T& rvalue, T& lvalue, const T& min) 
                                                         { lvalue = min; }
      template <typename T> 
      static void on_above(const T& rvalue, T& lvalue, const T& max) 
                                                         { lvalue = max; }
    };
    template<int min, int max, typename invalid_range = throwing>
    struct ranged_integer {
      typedef int value;
      static const int get_min() { return min; }
      static const int get_max() { return max; }
      static void assign(const value& rvalue, value& lvalue) {  
        if (rvalue < min) 
              { invalid_range::on_below(rvalue, lvalue, min); return; }  
        if (rvalue > max) 
              { invalid_range::on_above(rvalue, lvalue, max); return; }  
        lvalue = rvalue; 
      }
    };                  
  };
};
#endif // CONSTRAINED_VALUE_HPP