Chris@16: #ifndef CONSTRAINED_VALUE_HPP___ Chris@16: #define CONSTRAINED_VALUE_HPP___ Chris@16: Chris@16: /* Copyright (c) 2002,2003 CrystalClear Software, Inc. Chris@16: * Use, modification and distribution is subject to the Chris@16: * Boost Software License, Version 1.0. (See accompanying Chris@16: * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Author: Jeff Garland Chris@101: * $Date$ Chris@16: */ Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: //! Namespace containing constrained_value template and types Chris@16: namespace CV { Chris@16: //! Represent a min or max violation type Chris@16: enum violation_enum {min_violation, max_violation}; Chris@16: Chris@16: //! A template to specify a constrained basic value type Chris@16: /*! This template provides a quick way to generate Chris@16: * an integer type with a constrained range. The type Chris@16: * provides for the ability to specify the min, max, and Chris@16: * and error handling policy. Chris@16: * Chris@16: * value policies Chris@16: * A class that provides the range limits via the min and Chris@16: * max functions as well as a function on_error that Chris@16: * determines how errors are handled. A common strategy Chris@16: * would be to assert or throw and exception. The on_error Chris@16: * is passed both the current value and the new value that Chris@16: * is in error. Chris@16: * Chris@16: */ Chris@16: template Chris@16: class constrained_value { Chris@16: public: Chris@16: typedef typename value_policies::value_type value_type; Chris@16: // typedef except_type exception_type; Chris@16: constrained_value(value_type value) : value_((min)()) Chris@16: { Chris@16: assign(value); Chris@16: } Chris@16: constrained_value& operator=(value_type v) Chris@16: { Chris@16: assign(v); Chris@16: return *this; Chris@16: } Chris@16: //! Return the max allowed value (traits method) Chris@16: static value_type max BOOST_PREVENT_MACRO_SUBSTITUTION () {return (value_policies::max)();} Chris@16: //! Return the min allowed value (traits method) Chris@16: static value_type min BOOST_PREVENT_MACRO_SUBSTITUTION () {return (value_policies::min)();} Chris@16: //! Coerce into the representation type Chris@16: operator value_type() const {return value_;} Chris@16: protected: Chris@16: value_type value_; Chris@16: private: Chris@16: void assign(value_type value) Chris@16: { Chris@16: //adding 1 below gets rid of a compiler warning which occurs when the Chris@16: //min_value is 0 and the type is unsigned.... Chris@16: if (value+1 < (min)()+1) { Chris@16: value_policies::on_error(value_, value, min_violation); Chris@16: return; Chris@16: } Chris@16: if (value > (max)()) { Chris@16: value_policies::on_error(value_, value, max_violation); Chris@16: return; Chris@16: } Chris@16: value_ = value; Chris@16: } Chris@16: }; Chris@16: Chris@16: //! Template to shortcut the constrained_value policy creation process Chris@16: template Chris@16: class simple_exception_policy Chris@16: { Chris@16: struct exception_wrapper : public exception_type Chris@16: { Chris@16: // In order to support throw_exception mechanism in the BOOST_NO_EXCEPTIONS mode, Chris@16: // we'll have to provide a way to acquire std::exception from the exception being thrown. Chris@16: // However, we cannot derive from it, since it would make it interceptable by this class, Chris@16: // which might not be what the user wanted. Chris@16: operator std::out_of_range () const Chris@16: { Chris@16: // TODO: Make the message more descriptive by using arguments to on_error Chris@16: return std::out_of_range("constrained value boundary has been violated"); Chris@16: } Chris@16: }; Chris@16: Chris@16: typedef typename mpl::if_< Chris@16: is_base_of< std::exception, exception_type >, Chris@16: exception_type, Chris@16: exception_wrapper Chris@16: >::type actual_exception_type; Chris@16: Chris@16: public: Chris@16: typedef rep_type value_type; Chris@16: static rep_type min BOOST_PREVENT_MACRO_SUBSTITUTION () { return min_value; } Chris@16: static rep_type max BOOST_PREVENT_MACRO_SUBSTITUTION () { return max_value; } Chris@16: static void on_error(rep_type, rep_type, violation_enum) Chris@16: { Chris@16: boost::throw_exception(actual_exception_type()); Chris@16: } Chris@16: }; Chris@16: Chris@16: Chris@16: Chris@16: } } //namespace CV Chris@16: Chris@16: Chris@16: Chris@16: Chris@16: #endif