fix static management of CommandParser, sf.net 1474444

This commit is contained in:
simonb 2006-04-22 12:16:38 +00:00
parent 1028f4cb51
commit de4dfdad16
3 changed files with 24 additions and 3 deletions

View file

@ -1,6 +1,9 @@
(Format: Year/Month/Day)
Changes for 0.9.16:
*06/04/22:
* static CommandParser could be used after destruction (Simon)
Fixes sf.net #1474444, segfault when DISPLAY not set
CommmandParser.hh/cc
* Minor cosmetic changes, thanks Semushin Slava (php-coder AT ngs ru)
main.cc Screen.cc util/startfluxbox.in
* First draft of new docs in docs/asciidoc (Mathias)

View file

@ -32,6 +32,9 @@ using std::vector;
using FbTk::StringUtil::removeFirstWhitespace;
using FbTk::StringUtil::toLower;
CommandParser *CommandParser::s_singleton = 0;
CommandFactory::CommandFactory() {
}
@ -39,15 +42,24 @@ CommandFactory::CommandFactory() {
CommandFactory::~CommandFactory() {
// remove all associations with this factory
CommandParser::instance().removeAssociation(*this);
}
void CommandFactory::addCommand(const string &command_name) {
CommandParser::instance().associateCommand(command_name, *this);
}
// ensure it is singleton
CommandParser::CommandParser() {
if (s_singleton != 0)
throw std::string("CommandParser currently meant ot be singleton");
}
CommandParser &CommandParser::instance() {
static CommandParser singleton;
return singleton;
if (s_singleton == 0)
s_singleton = new CommandParser();
return *s_singleton;
}
FbTk::Command *CommandParser::parseLine(const string &line) {
@ -105,4 +117,7 @@ void CommandParser::removeAssociation(CommandFactory &factory) {
m_commandfactorys.erase(commands.back());
commands.pop_back();
}
if (m_commandfactorys.empty())
delete s_singleton;
}

View file

@ -54,6 +54,8 @@ public:
/// @return parses and returns a command matching the line
FbTk::Command *parseLine(const std::string &line);
CommandParser();
/// @return instance of command parser
static CommandParser &instance();
/// @return map of factorys
@ -71,7 +73,8 @@ private:
const std::string &arguments);
CommandFactoryMap m_commandfactorys; ///< a string to factory map
static CommandParser *s_singleton;
};
#endif // COMMANDPARSER_HH