X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fgetopt.h;h=a3b7ae405ec567c4d9b0fb6a8966d89bed4fb689;hp=bb1bd1568f6dff403843e6bd57492bc0832fb21d;hb=736d15fbdee0ea47b38a4d5fcd321e86eb21b465;hpb=8aaeebd4a23bf19682564a3044fb5be4029fe82e diff --git a/source/core/getopt.h b/source/core/getopt.h index bb1bd15..a3b7ae4 100644 --- a/source/core/getopt.h +++ b/source/core/getopt.h @@ -1,10 +1,3 @@ -/* $Id$ - -This file is part of libmspcore -Copyright © 2006-2009, 2011 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - #ifndef MSP_CORE_GETOPT_H_ #define MSP_CORE_GETOPT_H_ @@ -72,6 +65,9 @@ private: template class Option: public OptBase { + private: + T &data; + public: Option(char s, const std::string &l, T &d, ArgType a): OptBase(s, l, a), data(d) { } @@ -88,13 +84,14 @@ private: throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")"); } } - private: - T &data; }; template class ListOption: public OptBase { + private: + T &data; + public: ListOption(char s, const std::string &l, T &d, ArgType a): OptBase(s, l, a), data(d) { if(arg_type!=REQUIRED_ARG) throw std::invalid_argument("ListOption arg_type!=REQUIRED"); } @@ -112,8 +109,6 @@ private: throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")"); } } - private: - T &data; }; bool help; @@ -128,17 +123,19 @@ public: template OptBase &add_option(char s, const std::string &l, T &d, ArgType a = NO_ARG) - { opts.push_back(new Option(s, l, d, a)); return *opts.back(); } + { return add_option(new Option(s, l, d, a)); } template OptBase &add_option(char s, const std::string &l, std::list &d, ArgType a = REQUIRED_ARG) - { opts.push_back(new ListOption >(s, l, d, a)); return *opts.back(); } + { return add_option(new ListOption >(s, l, d, a)); } template OptBase &add_option(const std::string &l, T &d, ArgType a) { return add_option(0, l, d, a); } private: + OptBase &add_option(OptBase *); + OptBase &get_option(char); OptBase &get_option(const std::string &); @@ -163,8 +160,11 @@ public: std::string generate_help() const; }; -template<> inline void GetOpt::Option::store() { data = true; } -template<> inline void GetOpt::Option::store() { ++data; } +template<> inline void GetOpt::Option::store() +{ data = true; } + +template<> inline void GetOpt::Option::store() +{ ++data; } template<> inline void GetOpt::Option::store(const std::string &a) { data = a; }