123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- #ifndef CONSTRAINED_VALUE_HPP___
- #define CONSTRAINED_VALUE_HPP___
- #include <exception>
- #include <stdexcept>
- #include <boost/config.hpp>
- #include <boost/throw_exception.hpp>
- #include <boost/mpl/if.hpp>
- #include <boost/type_traits/is_base_of.hpp>
- namespace boost {
- namespace CV {
-
- enum violation_enum {min_violation, max_violation};
-
-
-
- template<class value_policies>
- class constrained_value {
- public:
- typedef typename value_policies::value_type value_type;
-
- constrained_value(value_type value) : value_((min)())
- {
- assign(value);
- }
- constrained_value& operator=(value_type v)
- {
- assign(v);
- return *this;
- }
-
- static value_type max BOOST_PREVENT_MACRO_SUBSTITUTION () {return (value_policies::max)();}
-
- static value_type min BOOST_PREVENT_MACRO_SUBSTITUTION () {return (value_policies::min)();}
-
- operator value_type() const {return value_;}
- protected:
- value_type value_;
- private:
- void assign(value_type value)
- {
-
-
- if (value+1 < (min)()+1) {
- value_policies::on_error(value_, value, min_violation);
- return;
- }
- if (value > (max)()) {
- value_policies::on_error(value_, value, max_violation);
- return;
- }
- value_ = value;
- }
- };
-
- template<typename rep_type, rep_type min_value,
- rep_type max_value, class exception_type>
- class simple_exception_policy
- {
- struct exception_wrapper : public exception_type
- {
-
-
-
-
- operator std::out_of_range () const
- {
-
- return std::out_of_range("constrained value boundary has been violated");
- }
- };
- typedef typename mpl::if_<
- is_base_of< std::exception, exception_type >,
- exception_type,
- exception_wrapper
- >::type actual_exception_type;
- public:
- typedef rep_type value_type;
- static rep_type min BOOST_PREVENT_MACRO_SUBSTITUTION () { return min_value; }
- static rep_type max BOOST_PREVENT_MACRO_SUBSTITUTION () { return max_value; }
- static void on_error(rep_type, rep_type, violation_enum)
- {
- boost::throw_exception(actual_exception_type());
- }
- };
- } }
- #endif
|