Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <list>
- #include <sstream>
- #include <exception>
- #include <map>
- using namespace std;
- class OptBase
- {
- private:
- string n;
- public:
- OptBase(string name) : n(name) {}
- virtual string to_string() = 0;
- virtual void set_from_string(const string &) = 0;
- virtual string name()
- {
- return n;
- }
- };
- template <class T>
- class Option : public OptBase
- {
- protected:
- T val;
- public:
- Option(T t, string name) : OptBase(name), val(t) {}
- virtual string to_string() override
- {
- ostringstream str;
- str << val;
- return str.str();
- }
- virtual void set_from_string(const string &s) override
- {
- istringstream str;
- if(!str >> val)
- throw exception();
- }
- T get()
- {
- return val;
- }
- void set(T &t)
- {
- val = t;
- }
- };
- template <>
- class Option<bool> : public OptBase
- {
- protected:
- bool val;
- public:
- Option(bool t, string name)
- : OptBase(name), val(t)
- {
- }
- virtual string to_string() override
- {
- return val ? "true" : "false";
- }
- virtual void set_from_string(const string &s) override
- {
- if(s == "true")
- val = true;
- else if(s == "false")
- val = false;
- else
- throw exception();
- }
- operator bool()
- {
- return val;
- }
- bool get()
- {
- return val;
- }
- void set(bool &t)
- {
- val = t;
- }
- };
- template <class T>
- class ChoiceOption : public OptBase
- {
- public:
- typedef map<T, string> ChoiceNames;
- protected:
- ChoiceNames names;
- T val;
- public:
- ChoiceOption(T t, string name, ChoiceNames nms)
- : OptBase(name), val(t), names(nms) {}
- virtual string to_string() override
- {
- for(pair<T, string> it : names)
- if(val == it.first)
- return it.second;
- throw exception();
- }
- virtual void set_from_string(const string &s) override
- {
- for(pair<T, string> it : names)
- if(s == it.second)
- {
- val = it.first;
- return;
- }
- throw exception();
- }
- T get()
- {
- return val;
- }
- void set(T &t)
- {
- val = t;
- }
- };
- class AlgoBase
- {
- public:
- virtual list<OptBase*> get_options() = 0;
- };
- class Algo1 : public AlgoBase
- {
- protected:
- enum class Mode{LIN, LOG};
- Option<bool> do_extra_work;
- ChoiceOption<Mode> my_mode;
- public:
- Algo1()
- : do_extra_work(true, "DoThis"),
- my_mode(Mode::LIN, "Mode", {{Mode::LIN, "lin"}, {Mode::LOG, "log"}})
- {}
- list<OptBase*> get_options() override
- {
- return list<OptBase*>{&do_extra_work, &my_mode};
- }
- void iterate()
- {
- for(int i = 0; i < 10000; ++i)
- {
- // lookup needs to be fast
- if(do_extra_work)
- {
- }
- else
- {
- }
- switch(my_mode.get())
- {
- case Mode::LIN:
- break;
- case Mode::LOG:
- break;
- default:
- break;
- }
- }
- }
- };
- int main(int argc, char **argv)
- {
- Option<bool> bool_opt(true, "Option");
- Algo1 alg;
- for(OptBase *opt : alg.get_options())
- {
- // string based functions are not required to be fast
- cout << opt->name() << ": " << opt->to_string() << endl;
- if(opt->name() == "Mode")
- {
- opt->set_from_string(string("log"));
- cout << opt->name() << ": " << opt->to_string() << endl;
- }
- }
- alg.iterate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement