Memory allocated in _constructor is now deleted when the object is destroyed

This commit is contained in:
macbishop 2004-09-19 18:32:13 +00:00
parent 6cd0c3db78
commit 6c06e31bb2

View File

@ -46,26 +46,50 @@ CmdLine::CmdLine(const string& m, char delim, const string& v )
_constructor(); _constructor();
} }
CmdLine::~CmdLine()
{
list<Arg*>::iterator argIter;
list<Visitor*>::iterator visIter;
for(argIter = _argDeleteOnExitList.begin(); argIter != _argDeleteOnExitList.end(); ++argIter) {
delete *argIter;
}
for(visIter = _visitorDeleteOnExitList.begin(); visIter != _visitorDeleteOnExitList.end(); ++visIter) {
delete *visIter;
}
}
void CmdLine::_constructor() void CmdLine::_constructor()
{ {
Visitor *v;
Arg::setDelimiter( _delimiter ); Arg::setDelimiter( _delimiter );
SwitchArg* help = new SwitchArg("h","help", v = new HelpVisitor( this );
"Displays usage information and exits.", SwitchArg* help = new SwitchArg("h","help",
false, new HelpVisitor( this ) ); "Displays usage information and exits.",
false, v);
add( *help ); add( *help );
deleteOnExit(help);
deleteOnExit(v);
v = new VersionVisitor( this );
SwitchArg* vers = new SwitchArg("v","version", SwitchArg* vers = new SwitchArg("v","version",
"Displays version information and exits.", "Displays version information and exits.",
false, new VersionVisitor( this ) ); false, v);
add( *vers ); add( *vers );
deleteOnExit(vers);
deleteOnExit(v);
v = new IgnoreRestVisitor();
SwitchArg* ignore = new SwitchArg(Arg::flagStartString, SwitchArg* ignore = new SwitchArg(Arg::flagStartString,
Arg::ignoreNameString, Arg::ignoreNameString,
"Ignores the rest of the labeled arguments following this flag.", "Ignores the rest of the labeled arguments following this flag.",
false, new IgnoreRestVisitor() ); false, v);
add( *ignore ); add( *ignore );
deleteOnExit(ignore);
deleteOnExit(v);
} }
void CmdLine::xorAdd( vector<Arg*>& ors ) void CmdLine::xorAdd( vector<Arg*>& ors )
@ -223,4 +247,14 @@ bool CmdLine::_emptyCombined(const string& s)
return true; return true;
} }
void CmdLine::deleteOnExit(Arg* ptr)
{
_argDeleteOnExitList.push_back(ptr);
}
void CmdLine::deleteOnExit(Visitor* ptr)
{
_visitorDeleteOnExitList.push_back(ptr);
}
} }