1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-07-05 17:48:38 +00:00

use unique_ptr in ArgParser

This commit is contained in:
Lu Wang 2013-04-06 00:00:03 +08:00
parent 86961b2a80
commit eb632c529e
2 changed files with 5 additions and 14 deletions

View File

@ -41,14 +41,6 @@ void dump_value(std::ostream & out, const std::string & v)
out << '"' << 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) ArgParser & ArgParser::add(const char * optname, const char * description, ArgParserCallBack callback)
{ {
return add<char>(optname, nullptr, 0, description, callback, true); return add<char>(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) for(auto iter = arg_entries.begin(); iter != arg_entries.end(); ++iter)
{ {
const ArgEntryBase * p = *iter; const auto * p = iter->get();
if(p->shortname != 0) if(p->shortname != 0)
{ {
optstring.push_back(p->shortname); optstring.push_back(p->shortname);

View File

@ -13,6 +13,7 @@
#include <vector> #include <vector>
#include <ostream> #include <ostream>
#include <sstream> #include <sstream>
#include <memory>
#ifndef nullptr #ifndef nullptr
#define nullptr (NULL) #define nullptr (NULL)
@ -42,8 +43,6 @@ extern void dump_value(std::ostream & out, const std::string & v);
class ArgParser class ArgParser
{ {
public: public:
~ArgParser(void);
typedef void (*ArgParserCallBack) (const char * arg); typedef void (*ArgParserCallBack) (const char * arg);
/* /*
@ -100,7 +99,7 @@ class ArgParser
bool dont_show_default; bool dont_show_default;
}; };
std::vector<ArgEntryBase *> arg_entries, optional_arg_entries; std::vector<std::unique_ptr<ArgEntryBase>> arg_entries, optional_arg_entries;
static const int arg_col_width; 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])) if((!optname) || (!optname[0]))
{ {
// when optname is nullptr or "", it's optional, and description is dropped // when optname is nullptr or "", it's optional, and description is dropped
optional_arg_entries.push_back(new ArgEntry<T, Tv>("", location, default_value, callback, "", dont_show_default)); optional_arg_entries.emplace_back(new ArgEntry<T, Tv>("", location, default_value, callback, "", dont_show_default));
} }
else else
{ {
arg_entries.push_back(new ArgEntry<T, Tv>(optname, location, default_value, callback, (description ? description : ""), dont_show_default)); arg_entries.emplace_back(new ArgEntry<T, Tv>(optname, location, default_value, callback, (description ? description : ""), dont_show_default));
} }
return *this; return *this;