I have written a constrained_value type for things like range checking
integers, non-negative doubles, etc. and I have made the source code freely
available for all at http://www.cdiggins.com/constrained_value.hpp . Below
is a small example of the type in action:
#include "constrained_value.hpp"
#include <iostream>
typedef cv::constrained_value < cv::policies::ranged_integer < 0, 59 > >
Base60_A;
typedef cv::constrained_value < cv::policies::ranged_integer < 0, 59,
cv::policies::saturating > > Base60_B;
int main()
{
Base60_A a = 10;
a = a + 20;
try {
std::cout << "exception should be thrown" << std::endl;
a = a + 40;
std::cout << "no exception thrown" << std::endl;
}
catch(...) {
std::cout << "exception caught" << std::endl;
}
Base60_B b = 10;
std::cout << "Output should be 10" << std::endl << b << std::endl;
b = b + 70;
std::cout << "Output should be 59" << std::endl << b << std::endl;
b = b - 100;
std::cout << "Output should be 0" << std::endl << b << std::endl;
std::cin.get();
return 0;
};
There is a more complete demo of the type at
http://www.cdiggins.com/cv_test.cpp .
Enjoy!
--
Christopher Diggins
http://www.heron-centric.com