From bc08f0a03c75627427b19e3f821642931ba60b33 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 14 Jul 2012 18:07:25 +0300 Subject: [PATCH] Rearrange GetOpt internals to provide a nicer external interface --- source/core/getopt.h | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/source/core/getopt.h b/source/core/getopt.h index a3b7ae4..d3369c6 100644 --- a/source/core/getopt.h +++ b/source/core/getopt.h @@ -32,7 +32,20 @@ public: REQUIRED_ARG }; - class OptBase + class Option + { + protected: + Option() { } + public: + virtual ~Option() { } + + virtual Option &set_help(const std::string &) = 0; + virtual Option &set_help(const std::string &, const std::string &) = 0; + virtual unsigned get_seen_count() const = 0; + }; + +private: + class OptBase: public Option { protected: char shrt; @@ -46,14 +59,14 @@ public: public: virtual ~OptBase() { } - OptBase &set_help(const std::string &); - OptBase &set_help(const std::string &, const std::string &); + virtual OptBase &set_help(const std::string &); + virtual OptBase &set_help(const std::string &, const std::string &); char get_short() const { return shrt; } const std::string &get_long() const { return lng; } ArgType get_arg_type() const { return arg_type; } const std::string &get_help() const { return help; } const std::string &get_metavar() const { return metavar; } - unsigned get_seen_count() const { return seen_count; } + virtual unsigned get_seen_count() const { return seen_count; } void process(); void process(const std::string &); protected: @@ -61,15 +74,14 @@ public: virtual void store(const std::string &) = 0; }; -private: template - class Option: public OptBase + class SimpleOption: public OptBase { private: T &data; public: - Option(char s, const std::string &l, T &d, ArgType a): OptBase(s, l, a), data(d) { } + SimpleOption(char s, const std::string &l, T &d, ArgType a): OptBase(s, l, a), data(d) { } virtual void store() { } @@ -122,15 +134,15 @@ public: const std::vector &get_args() const { return args; } template - OptBase &add_option(char s, const std::string &l, T &d, ArgType a = NO_ARG) - { return add_option(new Option(s, l, d, a)); } + Option &add_option(char s, const std::string &l, T &d, ArgType a = NO_ARG) + { return add_option(new SimpleOption(s, l, d, a)); } template - OptBase &add_option(char s, const std::string &l, std::list &d, ArgType a = REQUIRED_ARG) + Option &add_option(char s, const std::string &l, std::list &d, ArgType a = REQUIRED_ARG) { return add_option(new ListOption >(s, l, d, a)); } template - OptBase &add_option(const std::string &l, T &d, ArgType a) + Option &add_option(const std::string &l, T &d, ArgType a) { return add_option(0, l, d, a); } private: @@ -160,13 +172,13 @@ public: std::string generate_help() const; }; -template<> inline void GetOpt::Option::store() +template<> inline void GetOpt::SimpleOption::store() { data = true; } -template<> inline void GetOpt::Option::store() +template<> inline void GetOpt::SimpleOption::store() { ++data; } -template<> inline void GetOpt::Option::store(const std::string &a) +template<> inline void GetOpt::SimpleOption::store(const std::string &a) { data = a; } template<> inline void GetOpt::ListOption >::store(const std::string &a) -- 2.45.2