|
Boost : |
From: christopher diggins (cdiggins_at_[hidden])
Date: 2004-05-16 10:09:07
/*
constrained_value.hpp
Boost constrained_value type mini-library
original authour: Jeff Garland, http://www.crystalclearsoftware.com/
modified by: Christopher Diggins http://www.cdiggins.com
license available at http://www.boost.org/LICENSE_1_0.txt
last modified: May 16, 2004
*/
#ifndef BOOST_CV_HPP
#define BOOST_CV_HPP
// you can explicitly turn off constrained_value_checking
#ifdef BOOST_CV_CHECKING_OFF
#define BOOST_CV_CHECKING_ON 0
#else
#define BOOST_CV_CHECKING_ON 1
#endif
#include <boost/mpl/if.hpp>
namespace boost
{
namespace cv
{
enum enum_violation { min_violation, max_violation };
template<typename T, T min_T, T max_T>
struct integer_range_policy
{
typedef typename T value;
static const value min() { return min_T; };
static const value max() { return max_T; };
};
struct default_error_policy {
template<typename T>
static void on_error(T value, T* field, T min, T max, enum_violation
v) {
throw;
}
};
template
<
class constraints_policy,
class error_policy = default_error_policy,
bool implicit_conversion_policy = true
>
class constrained_value {
private:
struct unused_struct { };
public:
typedef constrained_value<constraints_policy, error_policy,
implicit_conversion_policy> self;
typedef typename constraints_policy::value value;
typedef typename mpl::if_c<implicit_conversion_policy, value,
unused_struct> value_parameter;
constrained_value() : m(min()) { }
constrained_value(const self& x) { m = x.get_value(); }
constrained_value(value_parameter x) { assign(x); }
static const value min() { return constraints_policy::min(); }
static const value max() { return constraints_policy::max(); }
const value get_value() const { return m; }
operator value() { return m; }
self& operator=(const self& x) { m = x; return *this; }
self& operator=(value_parameter x) { assign(x); return *this; }
void assign(unused_struct x) { }
void assign(const value& x)
{
#ifdef BOOST_CV_CHECKING_ON
{
// +1 removes unsigned warnings
if (x+1 < min()+1) {
error_policy::on_error(x, &m, min(), max(),
policies::min_violation);
return;
}
if (x > max()) {
error_policy::on_error(x, &m, min(), max(),
policies::max_violation);
return;
}
}
#endif
m = x;
}
private:
value m;
};
};
};
#endif // BOOST_CV_HPP
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk