1 | #ifndef CONSTRAINED_VALUE_HPP___ |
---|
2 | #define CONSTRAINED_VALUE_HPP___ |
---|
3 | |
---|
4 | /* Copyright (c) 2002,2003 CrystalClear Software, Inc. |
---|
5 | * Use, modification and distribution is subject to the |
---|
6 | * Boost Software License, Version 1.0. (See accompanying |
---|
7 | * file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0) |
---|
8 | * Author: Jeff Garland |
---|
9 | * $Date: 2004/02/26 18:26:47 $ |
---|
10 | */ |
---|
11 | |
---|
12 | #include <boost/config.hpp> |
---|
13 | |
---|
14 | namespace boost { |
---|
15 | |
---|
16 | //! Namespace containing constrained_value template and types |
---|
17 | namespace CV { |
---|
18 | //! Represent a min or max violation type |
---|
19 | enum violation_enum {min_violation, max_violation}; |
---|
20 | |
---|
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 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) |
---|
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 | }; |
---|
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 simple_exception_policy |
---|
80 | { |
---|
81 | public: |
---|
82 | typedef rep_type value_type; |
---|
83 | static rep_type min BOOST_PREVENT_MACRO_SUBSTITUTION () { return min_value; }; |
---|
84 | static rep_type max BOOST_PREVENT_MACRO_SUBSTITUTION () { return max_value;}; |
---|
85 | static void on_error(rep_type, rep_type, violation_enum) |
---|
86 | { |
---|
87 | throw exception_type(); |
---|
88 | } |
---|
89 | }; |
---|
90 | |
---|
91 | |
---|
92 | |
---|
93 | } } //namespace CV |
---|
94 | |
---|
95 | |
---|
96 | |
---|
97 | |
---|
98 | #endif |
---|