tools: various improvements in nfdc

* use same underlying type for ParseMode and AvailableIn,
  to avoid potential errors when converting between them
* simplify AvailableIn stream output operator with text::Separator
* allow CommandParser::addCommand to accept bitwise OR'ed modes
* qualify nfdc logging sources with 'nfdc.' prefix

refs #3780

Change-Id: I2eee3d562184daf27305c2d5f2892637b63421c2
This commit is contained in:
Junxiao Shi committed 2016-09-20 13:04:51 +00:00
1 parent c69f9210d9
commit c143afea10
4 files changed
+34 -23

No files matched your search

+1 -1
View File
@@ -33,7 +33,7 @@ namespace nfd {
namespace tools {
namespace nfdc {
NDN_LOG_INIT(CommandDefinition);
NDN_LOG_INIT(nfdc.CommandDefinition);
std::ostream&
operator<<(std::ostream& os, ArgValueType vt)
+17 -17
View File
@@ -24,33 +24,31 @@
*/
#include "command-parser.hpp"
#include "format-helpers.hpp"
#include <ndn-cxx/util/logger.hpp>
namespace nfd {
namespace tools {
namespace nfdc {
NDN_LOG_INIT(CommandParser);
NDN_LOG_INIT(nfdc.CommandParser);
static_assert(std::is_same<std::underlying_type<AvailableIn>::type,
std::underlying_type<ParseMode>::type>::value,
"AvailableIn and ParseMode must be declared with same underlying type");
std::ostream&
operator<<(std::ostream& os, AvailableIn modes)
{
int count = 0;
#define PRINT_BIT(bit, str) \
if ((modes & bit) != 0) { \
if (++count > 1) { \
os << '|'; \
} \
os << str; \
text::Separator sep("|");
if ((modes & AVAILABLE_IN_ONE_SHOT) != 0) {
os << sep << "one-shot";
}
if ((modes & AVAILABLE_IN_BATCH) != 0) {
os << sep << "batch";
}
PRINT_BIT(AVAILABLE_IN_ONE_SHOT, "one-shot")
PRINT_BIT(AVAILABLE_IN_BATCH, "batch")
#undef PRINT_BIT
if (count == 0) {
if (sep.getCount() == 0) {
os << "none";
}
return os;
@@ -69,10 +67,12 @@ operator<<(std::ostream& os, ParseMode mode)
}
CommandParser&
CommandParser::addCommand(const CommandDefinition& def, const ExecuteCommand& execute, AvailableIn modes)
CommandParser::addCommand(const CommandDefinition& def, const ExecuteCommand& execute,
std::underlying_type<AvailableIn>::type modes)
{
BOOST_ASSERT(modes != AVAILABLE_IN_NONE);
m_commands[{def.getNoun(), def.getVerb()}].reset(new Command{def, execute, modes});
m_commands[{def.getNoun(), def.getVerb()}].reset(
new Command{def, execute, static_cast<AvailableIn>(modes)});
return *this;
}
+10 -5
View File
@@ -28,6 +28,7 @@
#include "command-definition.hpp"
#include "execute-command.hpp"
#include <type_traits>
namespace nfd {
namespace tools {
@@ -37,8 +38,8 @@ namespace nfdc {
*/
enum AvailableIn : uint8_t {
AVAILABLE_IN_NONE = 0,
AVAILABLE_IN_ONE_SHOT = 1 << 0,
AVAILABLE_IN_BATCH = 1 << 1,
AVAILABLE_IN_ONE_SHOT = 1 << 0, ///< one-shot mode
AVAILABLE_IN_BATCH = 1 << 1, ///< batch mode
AVAILABLE_IN_ALL = 0xff
};
@@ -47,7 +48,7 @@ operator<<(std::ostream& os, AvailableIn modes);
/** \brief indicates which mode is the parser operated in
*/
enum class ParseMode {
enum class ParseMode : uint8_t {
ONE_SHOT = AVAILABLE_IN_ONE_SHOT, ///< one-shot mode
BATCH = AVAILABLE_IN_BATCH ///< batch mode
};
@@ -76,7 +77,8 @@ public:
* \param modes parse modes this command should be available in, must not be AVAILABLE_IN_NONE
*/
CommandParser&
addCommand(const CommandDefinition& def, const ExecuteCommand& execute, AvailableIn modes = AVAILABLE_IN_ALL);
addCommand(const CommandDefinition& def, const ExecuteCommand& execute,
std::underlying_type<AvailableIn>::type modes = AVAILABLE_IN_ALL);
/** \brief add an alias "noun verb2" to existing command "noun verb"
* \throw std::out_of_range "noun verb" does not exist
@@ -104,7 +106,10 @@ private:
AvailableIn modes;
};
std::map<CommandName, shared_ptr<Command>> m_commands;
/** \brief map from command name or alias to command definition
*/
typedef std::map<CommandName, shared_ptr<Command>> CommandContainer;
CommandContainer m_commands;
};
} // namespace nfdc
+6
View File
@@ -85,6 +85,12 @@ public:
explicit
Separator(const std::string& subsequent);
int
getCount() const
{
return m_count;
}
private:
std::string m_first;
std::string m_subsequent;