constrained_value.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. BOOST_CXX14_CONSTEXPR constrained_value(value_type value) : value_((min)())
  42. {
  43. assign(value);
  44. }
  45. BOOST_CXX14_CONSTEXPR constrained_value& operator=(value_type v)
  46. {
  47. assign(v);
  48. return *this;
  49. }
  50. //! Return the max allowed value (traits method)
  51. static BOOST_CONSTEXPR value_type
  52. max BOOST_PREVENT_MACRO_SUBSTITUTION () {return (value_policies::max)();}
  53. //! Return the min allowed value (traits method)
  54. static BOOST_CONSTEXPR value_type
  55. min BOOST_PREVENT_MACRO_SUBSTITUTION () {return (value_policies::min)();}
  56. //! Coerce into the representation type
  57. BOOST_CXX14_CONSTEXPR operator value_type() const {return value_;}
  58. protected:
  59. value_type value_;
  60. private:
  61. BOOST_CXX14_CONSTEXPR void assign(value_type value)
  62. {
  63. //adding 1 below gets rid of a compiler warning which occurs when the
  64. //min_value is 0 and the type is unsigned....
  65. if (value+1 < (min)()+1) {
  66. value_policies::on_error(value_, value, min_violation);
  67. return;
  68. }
  69. if (value > (max)()) {
  70. value_policies::on_error(value_, value, max_violation);
  71. return;
  72. }
  73. value_ = value;
  74. }
  75. };
  76. //! Template to shortcut the constrained_value policy creation process
  77. template<typename rep_type, rep_type min_value,
  78. rep_type max_value, class exception_type>
  79. class BOOST_SYMBOL_VISIBLE simple_exception_policy
  80. {
  81. struct BOOST_SYMBOL_VISIBLE exception_wrapper : public exception_type
  82. {
  83. // In order to support throw_exception mechanism in the BOOST_NO_EXCEPTIONS mode,
  84. // we'll have to provide a way to acquire std::exception from the exception being thrown.
  85. // However, we cannot derive from it, since it would make it interceptable by this class,
  86. // which might not be what the user wanted.
  87. operator std::out_of_range () const
  88. {
  89. // TODO: Make the message more descriptive by using arguments to on_error
  90. return std::out_of_range("constrained value boundary has been violated");
  91. }
  92. };
  93. typedef typename conditional<
  94. is_base_of< std::exception, exception_type >::value,
  95. exception_type,
  96. exception_wrapper
  97. >::type actual_exception_type;
  98. public:
  99. typedef rep_type value_type;
  100. static BOOST_CONSTEXPR rep_type
  101. min BOOST_PREVENT_MACRO_SUBSTITUTION () { return min_value; }
  102. static BOOST_CONSTEXPR rep_type
  103. max BOOST_PREVENT_MACRO_SUBSTITUTION () { return max_value; }
  104. static void on_error(rep_type, rep_type, violation_enum)
  105. {
  106. boost::throw_exception(actual_exception_type());
  107. }
  108. };
  109. } } //namespace CV
  110. #endif