From eb632c529ec13771466970f47e8716b4bccdc5f5 Mon Sep 17 00:00:00 2001 From: Lu Wang Date: Sat, 6 Apr 2013 00:00:03 +0800 Subject: [PATCH] use unique_ptr in ArgParser --- src/util/ArgParser.cc | 10 +--------- src/util/ArgParser.h | 9 ++++----- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/src/util/ArgParser.cc b/src/util/ArgParser.cc index 0edc25f..691c27d 100644 --- a/src/util/ArgParser.cc +++ b/src/util/ArgParser.cc @@ -41,14 +41,6 @@ void dump_value(std::ostream & out, const std::string & v) out << '"' << v << '"'; } -ArgParser::~ArgParser(void) -{ - for(auto iter = arg_entries.begin(); iter != arg_entries.end(); ++iter) - delete (*iter); - for(auto iter = optional_arg_entries.begin(); iter != optional_arg_entries.end(); ++iter) - delete (*iter); -} - ArgParser & ArgParser::add(const char * optname, const char * description, ArgParserCallBack callback) { return add(optname, nullptr, 0, description, callback, true); @@ -66,7 +58,7 @@ void ArgParser::parse(int argc, char ** argv) const for(auto iter = arg_entries.begin(); iter != arg_entries.end(); ++iter) { - const ArgEntryBase * p = *iter; + const auto * p = iter->get(); if(p->shortname != 0) { optstring.push_back(p->shortname); diff --git a/src/util/ArgParser.h b/src/util/ArgParser.h index a6c58c4..e560bc3 100644 --- a/src/util/ArgParser.h +++ b/src/util/ArgParser.h @@ -13,6 +13,7 @@ #include #include #include +#include #ifndef nullptr #define nullptr (NULL) @@ -42,8 +43,6 @@ extern void dump_value(std::ostream & out, const std::string & v); class ArgParser { public: - ~ArgParser(void); - typedef void (*ArgParserCallBack) (const char * arg); /* @@ -100,7 +99,7 @@ class ArgParser bool dont_show_default; }; - std::vector arg_entries, optional_arg_entries; + std::vector> arg_entries, optional_arg_entries; static const int arg_col_width; }; @@ -111,11 +110,11 @@ ArgParser & ArgParser::add(const char * optname, T * location, const Tv & defaul if((!optname) || (!optname[0])) { // when optname is nullptr or "", it's optional, and description is dropped - optional_arg_entries.push_back(new ArgEntry("", location, default_value, callback, "", dont_show_default)); + optional_arg_entries.emplace_back(new ArgEntry("", location, default_value, callback, "", dont_show_default)); } else { - arg_entries.push_back(new ArgEntry(optname, location, default_value, callback, (description ? description : ""), dont_show_default)); + arg_entries.emplace_back(new ArgEntry(optname, location, default_value, callback, (description ? description : ""), dont_show_default)); } return *this;