Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Length len = read_from_keyboard(); // or some means of initialization
- if( len.is_valid() ) { ... }
- struct Length
- {
- QString m;
- Length() {}
- Length( QString s ) { if( is_valid_string(s) ) { m = s; } }
- QString operator() () { return m; }
- bool is_valid() { return is_valid_string(m); }
- static bool is_valid_string( QString s ) {
- return s == "mm" || s=="m" || s=="ft" || s=="in";
- }
- };
- struct Angle{
- QString m;
- Angle() {}
- Angle( QString s ) { if( is_valid_string(s) ) { m = s; } }
- QString operator() () { return m; }
- bool is_valid() { return is_valid_string(m); }
- static bool is_valid_string( QString s ) {
- return s == "deg" || s=="rad";
- }
- };
- template <class T>
- struct ConstrainedText {
- QString m;
- ConstrainedText() {}
- ConstrainedText( QString s ) { if( T::is_valid_string(s) ) { m = s; } }
- QString operator() () { return m; }
- bool is_valid() { return T::is_valid_string(m); }
- };
- struct Angle : public ConstrainedText<Angle> {
- static bool is_valid_string( QString s ) {
- return s == "deg" || s="rad";
- }
- };
- struct Length : public ConstrainedText<Angle> {
- static bool is_valid_string( QString s ) {
- return s == "mm" || s="m" || s=="ft" || s=="in";
- }
- };
- #include <string>
- #include <stdexcept>
- #include <iostream>
- template <class T>
- class ConstrainedText {
- std::string m;
- protected:
- ConstrainedText() {}
- ~ConstrainedText() {}
- public:
- bool is_valid() {
- return T::is_valid_string(m);
- }
- static T Create(std::string const & s)
- {
- if (T::is_valid_string(s)) {
- T t;
- static_cast<ConstrainedText<T>&>(t).m = s;
- return t;
- }
- throw std::runtime_error("invalid input!");
- }
- };
- struct Angle : public ConstrainedText<Angle> {
- static bool is_valid_string( std::string s ) {
- return s == "deg" || s=="rad";
- }
- };
- struct Length : public ConstrainedText<Length> {
- static bool is_valid_string( std::string s ) {
- return s == "mm" || s == "m" || s == "ft" || s == "in";
- }
- };
- int main()
- {
- auto a = Angle::Create("deg");
- auto l = Length::Create("mm");
- try {
- Angle::Create("bimbo");
- } catch (std::runtime_error & pEx) {
- std::cout << "exception as expected" << std::endl;
- }
- try {
- Length::Create("bimbo");
- } catch (std::runtime_error & pEx) {
- std::cout << "exception as expected" << std::endl;
- }
- }
- template<typename... Args> Derived(Args &&...args):
- Base(std::forward<Args>(args)...) {}
- template <class Policy>
- struct ConstrainedText {
- QString m;
- ConstrainedText() {}
- ConstrainedText( QString s ) { if( Policy::is_valid_string(s) ) { m = s; } }
- QString operator() () { return m; }
- bool is_valid() { return Policy::is_valid_string(m); }
- };
- struct Angle_policy {
- static bool is_valid_string( QString s ) {
- return s == "deg" || s="rad";
- }
- };
- struct Length_policy {
- static bool is_valid_string( QString s ) {
- return s == "mm" || s="m" || s=="ft" || s=="in";
- }
- };
- typedef ConstrainedText<Length_policy> Length;
- typedef ConstrainedText<Angle_policy> Angle;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement