constrained_value.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #ifndef CONSTRAINED_VALUE_HPP___
  2. #define CONSTRAINED_VALUE_HPP___
  3. /* Copyright (c) 2002,2003 CrystalClear Software, Inc.
  4. * Use, modification and distribution is subject to the
  5. * Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  7. * Author: Jeff Garland
  8. * $Date$
  9. */
  10. #include <exception>
  11. #include <stdexcept>
  12. #include <boost/config.hpp>
  13. #include <boost/throw_exception.hpp>
  14. #include <boost/type_traits/conditional.hpp>
  15. #include <boost/type_traits/is_base_of.hpp>
  16. namespace boost {
  17. //! Namespace containing constrained_value template and types
  18. namespace CV {
  19. //! Represent a min or max violation type
  20. enum violation_enum {min_violation, max_violation};
  21. //! A template to specify a constrained basic value type
  22. /*! This template provides a quick way to generate
  23. * an integer type with a constrained range. The type
  24. * provides for the ability to specify the min, max, and
  25. * and error handling policy.
  26. *
  27. * <b>value policies</b>
  28. * A class that provides the range limits via the min and
  29. * max functions as well as a function on_error that
  30. * determines how errors are handled. A common strategy
  31. * would be to assert or throw and exception. The on_error
  32. * is passed both the current value and the new value that
  33. * is in error.
  34. *
  35. */
  36. template<class value_policies>
  37. class BOOST_SYMBOL_VISIBLE constrained_value {
  38. public:
  39. typedef typename value_policies::value_type value_type;
  40. // typedef except_type exception_type;
  41. constrained_value(value_type value) : value_((min)())
  42. {
  43. assign(value);
  44. }
  45. constrained_value& operator=(value_type v)
  46. {
  47. assign(v);
  48. return *this;
  49. }
  50. //! Return the max allowed value (traits method)
  51. static value_type max BOOST_PREVENT_MACRO_SUBSTITUTION () {return (value_policies::max)();}
  52. //! Return the min allowed value (traits method)
  53. static value_type min BOOST_PREVENT_MACRO_SUBSTITUTION () {return (value_policies::min)();}
  54. //! Coerce into the representation type
  55. operator value_type() const {return value_;}
  56. protected:
  57. value_type value_;
  58. private:
  59. void assign(value_type value)
  60. {
  61. //adding 1 below gets rid of a compiler warning which occurs when the
  62. //min_value is 0 and the type is unsigned....
  63. if (value+1 < (min)()+1) {
  64. value_policies::on_error(value_, value, min_violation);
  65. return;
  66. }
  67. if (value > (max)()) {
  68. value_policies::on_error(value_, value, max_violation);
  69. return;
  70. }
  71. value_ = value;
  72. }
  73. };
  74. //! Template to shortcut the constrained_value policy creation process
  75. template<typename rep_type, rep_type min_value,
  76. rep_type max_value, class exception_type>
  77. class BOOST_SYMBOL_VISIBLE simple_exception_policy
  78. {
  79. struct BOOST_SYMBOL_VISIBLE exception_wrapper : public exception_type
  80. {
  81. // In order to support throw_exception mechanism in the BOOST_NO_EXCEPTIONS mode,
  82. // we'll have to provide a way to acquire std::exception from the exception being thrown.
  83. // However, we cannot derive from it, since it would make it interceptable by this class,
  84. // which might not be what the user wanted.
  85. operator std::out_of_range () const
  86. {
  87. // TODO: Make the message more descriptive by using arguments to on_error
  88. return std::out_of_range("constrained value boundary has been violated");
  89. }
  90. };
  91. typedef typename conditional<
  92. is_base_of< std::exception, exception_type >::value,
  93. exception_type,
  94. exception_wrapper
  95. >::type actual_exception_type;
  96. public:
  97. typedef rep_type value_type;
  98. static rep_type min BOOST_PREVENT_MACRO_SUBSTITUTION () { return min_value; }
  99. static rep_type max BOOST_PREVENT_MACRO_SUBSTITUTION () { return max_value; }
  100. static void on_error(rep_type, rep_type, violation_enum)
  101. {
  102. boost::throw_exception(actual_exception_type());
  103. }
  104. };
  105. } } //namespace CV
  106. #endif