Command: Code cleanup, added ::category accessor

- The Command object failed to initialize all member on construction and
  initialized them out of order (is this still a thing in C++11?).
- The _category was the only member with no accessor, which is not right.
This commit is contained in:
Paul Beckingham 2015-09-06 14:07:21 -04:00
parent 77b17379d6
commit d1fe1279ed
3 changed files with 32 additions and 23 deletions

View file

@ -88,7 +88,7 @@ int CmdCommands::execute (std::string& output)
{
int row = view.addRow ();
view.set (row, 0, command.first);
view.set (row, 1, Command::categoryNames.at (command.second->_category));
view.set (row, 1, Command::categoryNames.at (command.second->category ()));
if (command.second->read_only ())
view.set (row, 2, "RO");
@ -213,7 +213,7 @@ int CmdZshCommands::execute (std::string& output)
std::vector <ZshCommand> commands;
for (auto& command : context.commands)
{
ZshCommand zshCommand {command.second->_category,
ZshCommand zshCommand {command.second->category (),
command.first,
command.second->description ()};
commands.push_back (zshCommand);

View file

@ -213,7 +213,7 @@ const std::map <Command::Category, std::string> Command::categoryNames =
////////////////////////////////////////////////////////////////////////////////
Command::Command ()
: _category(Category::unassigned)
: _keyword ("")
, _usage ("")
, _description ("")
, _read_only (true)
@ -221,8 +221,10 @@ Command::Command ()
, _needs_confirm (false)
, _needs_gc (true)
, _uses_context (false)
, _accepts_filter (true)
, _accepts_modifications (true)
, _accepts_filter (false)
, _accepts_modifications (false)
, _accepts_miscellaneous (false)
, _category(Category::unassigned)
, _permission_quit (false)
, _permission_all (false)
, _first_iteration (true)
@ -294,6 +296,12 @@ bool Command::accepts_miscellaneous () const
return _accepts_miscellaneous;
}
////////////////////////////////////////////////////////////////////////////////
Command::Category Command::category () const
{
return _category;
}
////////////////////////////////////////////////////////////////////////////////
// Returns true or false indicating whether to proceed with a write command, on
// a per-task basis, after (potentially) asking for permission.

View file

@ -35,23 +35,6 @@
class Command
{
public:
Command ();
virtual ~Command ();
static void factory (std::map <std::string, Command*>&);
std::string keyword () const;
std::string usage () const;
std::string description () const;
bool read_only () const;
bool displays_id () const;
bool needs_gc () const;
bool uses_context () const;
bool accepts_filter () const;
bool accepts_modifications () const;
bool accepts_miscellaneous () const;
virtual int execute (std::string&) = 0;
enum class Category
{
unassigned,
@ -68,7 +51,24 @@ public:
UNDOCUMENTED,
// Whenever you extend this enum, update categoryNames.
};
Category _category;
Command ();
virtual ~Command ();
static void factory (std::map <std::string, Command*>&);
std::string keyword () const;
std::string usage () const;
std::string description () const;
bool read_only () const;
bool displays_id () const;
bool needs_gc () const;
bool uses_context () const;
bool accepts_filter () const;
bool accepts_modifications () const;
bool accepts_miscellaneous () const;
Category category () const;
virtual int execute (std::string&) = 0;
protected:
bool permission (const Task&, const std::string&, unsigned int);
@ -86,6 +86,7 @@ protected:
bool _accepts_filter;
bool _accepts_modifications;
bool _accepts_miscellaneous;
Category _category;
// Permission support
bool _permission_quit;