daily pastebin goal
58%
SHARE
TWEET

Untitled

a guest Feb 22nd, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Length len = read_from_keyboard(); // or some means of initialization
  2. if( len.is_valid() )   { ...   }
  3.    
  4. struct Length
  5. {
  6.   QString m;
  7.  
  8.   Length() {}
  9.  
  10.   Length( QString s )   {   if( is_valid_string(s) )  {  m = s; }      }
  11.  
  12.   QString operator() () {   return m;      }
  13.  
  14.   bool is_valid()       {   return is_valid_string(m);      }
  15.  
  16.   static bool is_valid_string( QString s ) {
  17.     return s == "mm" || s=="m" || s=="ft" || s=="in";
  18.   }
  19. };
  20.    
  21. struct Angle{
  22.   QString m;
  23.  
  24.   Angle() {}
  25.  
  26.   Angle( QString s )    {   if( is_valid_string(s) )  {  m = s; }      }
  27.  
  28.   QString operator() () {   return m;      }
  29.  
  30.   bool is_valid()       {   return is_valid_string(m);      }
  31.  
  32.   static bool is_valid_string( QString s ) {
  33.     return s == "deg" || s=="rad";
  34.   }
  35. };
  36.    
  37. template <class T>
  38. struct ConstrainedText {
  39.   QString m;
  40.  
  41.   ConstrainedText() {}
  42.  
  43.   ConstrainedText( QString s ) {   if( T::is_valid_string(s) )   {  m = s;  }    }
  44.  
  45.   QString operator() ()        {   return m;      }
  46.  
  47.   bool is_valid()              {   return T::is_valid_string(m);    }
  48.  
  49. };
  50.  
  51.  
  52. struct Angle : public ConstrainedText<Angle> {
  53.    static bool is_valid_string( QString s ) {
  54.       return s == "deg" || s="rad";
  55.    }  
  56. };
  57.  
  58.  
  59. struct Length : public ConstrainedText<Angle> {
  60.    static bool is_valid_string( QString s ) {
  61.       return s == "mm" || s="m" || s=="ft" || s=="in";
  62.    }  
  63. };
  64.    
  65. #include <string>
  66. #include <stdexcept>
  67. #include <iostream>
  68.  
  69. template <class T>        
  70. class ConstrainedText {        
  71.   std::string m;                
  72.  
  73. protected:
  74.   ConstrainedText() {}
  75.   ~ConstrainedText() {}
  76. public:
  77.   bool is_valid() {        
  78.     return T::is_valid_string(m);        
  79.   }        
  80.  
  81.   static T Create(std::string const & s)
  82.   {
  83.       if (T::is_valid_string(s)) {
  84.           T t;
  85.           static_cast<ConstrainedText<T>&>(t).m = s;
  86.           return t;
  87.       }
  88.  
  89.       throw std::runtime_error("invalid input!");
  90.   }
  91. };        
  92.  
  93. struct Angle : public ConstrainedText<Angle> {        
  94.    static bool is_valid_string( std::string s ) {        
  95.       return s == "deg" || s=="rad";        
  96.    }          
  97. };        
  98.  
  99.  
  100. struct Length : public ConstrainedText<Length> {        
  101.    static bool is_valid_string( std::string s ) {        
  102.       return s == "mm" || s == "m" || s == "ft" || s == "in";        
  103.    }          
  104. };        
  105.  
  106. int main()
  107. {
  108.    auto a = Angle::Create("deg");
  109.    auto l = Length::Create("mm");
  110.  
  111.    try {
  112.        Angle::Create("bimbo");
  113.    } catch (std::runtime_error & pEx) {
  114.        std::cout << "exception as expected" << std::endl;
  115.    }
  116.  
  117.    try {
  118.        Length::Create("bimbo");
  119.    } catch (std::runtime_error & pEx) {
  120.        std::cout << "exception as expected" << std::endl;
  121.    }
  122. }
  123.    
  124. template<typename... Args> Derived(Args &&...args):
  125.     Base(std::forward<Args>(args)...) {}
  126.    
  127. template <class Policy>
  128. struct ConstrainedText {
  129.   QString m;
  130.  
  131.   ConstrainedText() {}
  132.  
  133.   ConstrainedText( QString s ) {   if( Policy::is_valid_string(s) )   {  m = s;  }    }
  134.  
  135.   QString operator() ()        {   return m;      }
  136.  
  137.   bool is_valid()              {   return Policy::is_valid_string(m);    }
  138.  
  139. };
  140.  
  141.  
  142. struct Angle_policy {
  143.    static bool is_valid_string( QString s ) {
  144.       return s == "deg" || s="rad";
  145.    }  
  146. };
  147.  
  148. struct Length_policy {
  149.    static bool is_valid_string( QString s ) {
  150.       return s == "mm" || s="m" || s=="ft" || s=="in";
  151.    }  
  152. };
  153.  
  154. typedef ConstrainedText<Length_policy> Length;
  155. typedef ConstrainedText<Angle_policy>  Angle;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top