Advertisement
Guest User

program options

a guest
Jun 25th, 2013
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <list>
  4. #include <sstream>
  5. #include <exception>
  6. #include <map>
  7. using namespace std;
  8.  
  9. class OptBase
  10. {
  11. private:
  12.   string n;
  13.  
  14. public:
  15.   OptBase(string name) : n(name) {}
  16.   virtual string to_string() = 0;
  17.   virtual void set_from_string(const string &) = 0;
  18.   virtual string name()
  19.   {
  20.     return n;
  21.   }
  22. };
  23.  
  24. template <class T>
  25. class Option : public OptBase
  26. {
  27. protected:
  28.   T val;
  29.  
  30. public:
  31.   Option(T t, string name) : OptBase(name), val(t) {}
  32.  
  33.   virtual string to_string() override
  34.   {
  35.     ostringstream str;
  36.     str << val;
  37.     return str.str();
  38.   }
  39.  
  40.   virtual void set_from_string(const string &s) override
  41.   {
  42.     istringstream str;
  43.     if(!str >> val)
  44.       throw exception();
  45.   }
  46.  
  47.   T get()
  48.   {
  49.     return val;
  50.   }
  51.  
  52.   void set(T &t)
  53.   {
  54.     val = t;
  55.   }
  56. };
  57.  
  58.  
  59. template <>
  60. class Option<bool> : public OptBase
  61. {
  62. protected:
  63.   bool val;
  64.  
  65. public:
  66.  
  67.   Option(bool t, string name)
  68.     : OptBase(name), val(t)
  69.   {
  70.   }
  71.  
  72.   virtual string to_string() override
  73.   {
  74.     return val ? "true" : "false";
  75.   }
  76.  
  77.   virtual void set_from_string(const string &s) override
  78.   {
  79.     if(s == "true")
  80.       val = true;
  81.     else if(s == "false")
  82.       val = false;
  83.     else
  84.       throw exception();
  85.   }
  86.  
  87.   operator bool()
  88.   {
  89.     return val;
  90.   }
  91.  
  92.   bool get()
  93.   {
  94.     return val;
  95.   }
  96.  
  97.   void set(bool &t)
  98.   {
  99.     val = t;
  100.   }
  101. };
  102.  
  103. template <class T>
  104. class ChoiceOption : public OptBase
  105. {
  106. public:
  107.   typedef map<T, string> ChoiceNames;
  108.  
  109. protected:
  110.   ChoiceNames names;
  111.   T val;
  112.  
  113. public:
  114.   ChoiceOption(T t, string name, ChoiceNames nms)
  115.     : OptBase(name), val(t), names(nms) {}
  116.  
  117.   virtual string to_string() override
  118.   {
  119.     for(pair<T, string> it : names)
  120.       if(val == it.first)
  121.         return it.second;
  122.    
  123.     throw exception();
  124.   }
  125.  
  126.   virtual void set_from_string(const string &s) override
  127.   {
  128.     for(pair<T, string> it : names)
  129.       if(s == it.second)
  130.       {
  131.         val = it.first;
  132.         return;
  133.       }
  134.    
  135.     throw exception();
  136.   }
  137.  
  138.   T get()
  139.   {
  140.     return val;
  141.   }
  142.  
  143.   void set(T &t)
  144.   {
  145.     val = t;
  146.   }
  147. };
  148.  
  149. class AlgoBase
  150. {
  151. public:
  152.   virtual list<OptBase*> get_options() = 0;
  153.  
  154. };
  155.  
  156.  
  157. class Algo1 : public AlgoBase
  158. {
  159. protected:
  160.   enum class Mode{LIN, LOG};
  161.  
  162.   Option<bool> do_extra_work;
  163.   ChoiceOption<Mode> my_mode;
  164.  
  165. public:
  166.   Algo1()
  167.     : do_extra_work(true, "DoThis"),
  168.       my_mode(Mode::LIN, "Mode", {{Mode::LIN, "lin"}, {Mode::LOG, "log"}})
  169.     {}
  170.  
  171.   list<OptBase*> get_options() override
  172.   {
  173.     return list<OptBase*>{&do_extra_work, &my_mode};
  174.   }
  175.  
  176.   void iterate()
  177.   {
  178.     for(int i = 0; i < 10000; ++i)
  179.     {
  180.       // lookup needs to be fast
  181.       if(do_extra_work)
  182.       {
  183.       }
  184.       else
  185.       {
  186.       }
  187.      
  188.       switch(my_mode.get())
  189.       {
  190.         case Mode::LIN:
  191.           break;
  192.         case Mode::LOG:
  193.           break;
  194.         default:
  195.           break;
  196.       }
  197.     }
  198.   }
  199. };
  200.  
  201. int main(int argc, char **argv)
  202. {
  203.   Option<bool> bool_opt(true, "Option");
  204.  
  205.   Algo1 alg;
  206.  
  207.   for(OptBase *opt : alg.get_options())
  208.   {
  209.     // string based functions are not required to be fast
  210.     cout << opt->name() << ": " << opt->to_string() << endl;
  211.    
  212.     if(opt->name() == "Mode")
  213.     {
  214.       opt->set_from_string(string("log"));
  215.       cout << opt->name() << ": " << opt->to_string() << endl;
  216.     }
  217.   }
  218.  
  219.   alg.iterate();
  220.  
  221.   return 0;
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement