File indexing completed on 2025-01-18 09:30:36
0001 #ifndef CONSTRAINED_VALUE_HPP___
0002 #define CONSTRAINED_VALUE_HPP___
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <exception>
0013 #include <stdexcept>
0014 #include <boost/config.hpp>
0015 #include <boost/throw_exception.hpp>
0016 #include <boost/type_traits/conditional.hpp>
0017 #include <boost/type_traits/is_base_of.hpp>
0018
0019 namespace boost {
0020
0021
0022 namespace CV {
0023
0024 enum violation_enum {min_violation, max_violation};
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041 template<class value_policies>
0042 class BOOST_SYMBOL_VISIBLE constrained_value {
0043 public:
0044 typedef typename value_policies::value_type value_type;
0045
0046 BOOST_CXX14_CONSTEXPR constrained_value(value_type value) : value_((min)())
0047 {
0048 assign(value);
0049 }
0050 BOOST_CXX14_CONSTEXPR constrained_value& operator=(value_type v)
0051 {
0052 assign(v);
0053 return *this;
0054 }
0055
0056 static BOOST_CONSTEXPR value_type
0057 max BOOST_PREVENT_MACRO_SUBSTITUTION () {return (value_policies::max)();}
0058
0059
0060 static BOOST_CONSTEXPR value_type
0061 min BOOST_PREVENT_MACRO_SUBSTITUTION () {return (value_policies::min)();}
0062
0063
0064 BOOST_CXX14_CONSTEXPR operator value_type() const {return value_;}
0065 protected:
0066 value_type value_;
0067 private:
0068 BOOST_CXX14_CONSTEXPR void assign(value_type value)
0069 {
0070
0071
0072 if (value+1 < (min)()+1) {
0073 value_policies::on_error(value_, value, min_violation);
0074 return;
0075 }
0076 if (value > (max)()) {
0077 value_policies::on_error(value_, value, max_violation);
0078 return;
0079 }
0080 value_ = value;
0081 }
0082 };
0083
0084
0085 template<typename rep_type, rep_type min_value,
0086 rep_type max_value, class exception_type>
0087 class BOOST_SYMBOL_VISIBLE simple_exception_policy
0088 {
0089 struct BOOST_SYMBOL_VISIBLE exception_wrapper : public exception_type
0090 {
0091
0092
0093
0094
0095 operator std::out_of_range () const
0096 {
0097
0098 return std::out_of_range("constrained value boundary has been violated");
0099 }
0100 };
0101
0102 typedef typename conditional<
0103 is_base_of< std::exception, exception_type >::value,
0104 exception_type,
0105 exception_wrapper
0106 >::type actual_exception_type;
0107
0108 public:
0109 typedef rep_type value_type;
0110 static BOOST_CONSTEXPR rep_type
0111 min BOOST_PREVENT_MACRO_SUBSTITUTION () { return min_value; }
0112
0113 static BOOST_CONSTEXPR rep_type
0114 max BOOST_PREVENT_MACRO_SUBSTITUTION () { return max_value; }
0115
0116 static void on_error(rep_type, rep_type, violation_enum)
0117 {
0118 boost::throw_exception(actual_exception_type());
0119 }
0120 };
0121
0122
0123
0124 } }
0125
0126
0127
0128
0129 #endif