|
Boost : |
From: Robert Kawulak (kawulak_at_[hidden])
Date: 2007-08-21 16:47:56
Hi all,
I've just written a draft of constraint policy, error policy and aliases for
constrained types with a predefined error value. When the constraint is broken,
the error policy changes the value to the selected value indicating error.
Examples are attached below. Does it look useful enough to be worth adding to
the library?
Best regards,
Robert
P.S.
Note, that when the error value is set, all the operations are still allowed.
Making a constrained type that ignores all subsequent operations once the error
value has been set is not easy given the current design and would require a
serious redesign of the policies, making them more complicated.
// typical bounded_errval_int usage
// parameters: value type, error value, lower bound, upper bound (default used
for the rest)
// note: error value is NOT in the allowed range (but could be)
bounded_errval_int<int, -1, 0, 100>::type b;
BOOST_CHECK(b == 0);
b = 1;
BOOST_CHECK(b == 1);
b = 1000;
BOOST_CHECK(b == -1);
b--;
BOOST_CHECK(b == -1);
b++; // mind this - error value can be changed this way too
BOOST_CHECK(b == 0);
b = -1;
BOOST_CHECK(b == -1);
// initialized with invalid value of 0
BOOST_CHECK((bounded_errval_int<int, -1, 10, 15>::type() == -1));
// initialized with the error value itself
BOOST_CHECK((bounded_errval_int<int, 0, 10, 15>::type() == 0));
// custom constraint
// parameters: value type, error value, constraint (default used for the rest)
// note: error value is a value allowed by the predicate
constrained_errval_int<int, 0, is_even>::type ei0;
BOOST_CHECK(ei0 == 0);
ei0 = 2;
BOOST_CHECK(ei0 == 2);
ei0 = 3;
BOOST_CHECK(ei0 == 0);
ei0 += 4;
BOOST_CHECK(ei0 == 4);
ei0 -= 3;
BOOST_CHECK(ei0 == 0);
ei0++;
BOOST_CHECK(ei0 == 0);
// note: error value is a value NOT allowed by the predicate
constrained_errval_int<int, 1, is_even>::type ei1(3);
BOOST_CHECK(ei1 == 1);
ei1 = 2;
BOOST_CHECK(ei1 == 2);
ei1 = 3;
BOOST_CHECK(ei1 == 1);
ei1 += 4;
BOOST_CHECK(ei1 == 1);
ei1 -= 3;
BOOST_CHECK(ei1 == -2);
ei1++;
BOOST_CHECK(ei1 == 1);
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk