Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:30:36

0001 #ifndef CONSTRAINED_VALUE_HPP___
0002 #define CONSTRAINED_VALUE_HPP___
0003 
0004 /* Copyright (c) 2002,2003 CrystalClear Software, Inc.
0005  * Use, modification and distribution is subject to the 
0006  * Boost Software License, Version 1.0. (See accompanying
0007  * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
0008  * Author: Jeff Garland 
0009  * $Date$
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 //! Namespace containing constrained_value template and types
0022 namespace CV {
0023   //! Represent a min or max violation type
0024   enum violation_enum {min_violation, max_violation};
0025   
0026   //! A template to specify a constrained basic value type
0027   /*! This template provides a quick way to generate
0028    *  an integer type with a constrained range.  The type
0029    *  provides for the ability to specify the min, max, and
0030    *  and error handling policy.
0031    *  
0032    *  <b>value policies</b>
0033    *  A class that provides the range limits via the min and
0034    *  max functions as well as a function on_error that 
0035    *  determines how errors are handled.  A common strategy
0036    *  would be to assert or throw and exception.  The on_error
0037    *  is passed both the current value and the new value that
0038    *  is in error.
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     //    typedef except_type exception_type;
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     //! Return the max allowed value (traits method)
0056     static BOOST_CONSTEXPR value_type
0057     max BOOST_PREVENT_MACRO_SUBSTITUTION () {return (value_policies::max)();}
0058 
0059     //! Return the min allowed value (traits method)
0060     static BOOST_CONSTEXPR value_type
0061     min BOOST_PREVENT_MACRO_SUBSTITUTION () {return (value_policies::min)();}
0062 
0063     //! Coerce into the representation type
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       //adding 1 below gets rid of a compiler warning which occurs when the 
0071       //min_value is 0 and the type is unsigned....
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   //! Template to shortcut the constrained_value policy creation process
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       // In order to support throw_exception mechanism in the BOOST_NO_EXCEPTIONS mode,
0092       // we'll have to provide a way to acquire std::exception from the exception being thrown.
0093       // However, we cannot derive from it, since it would make it interceptable by this class,
0094       // which might not be what the user wanted.
0095       operator std::out_of_range () const
0096       {
0097         // TODO: Make the message more descriptive by using arguments to on_error
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 } } //namespace CV
0125 
0126 
0127 
0128 
0129 #endif