From 1a5f29542ff55be2bf31e164448c0c6e8179ee38 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Sun, 2 Jan 2011 04:10:40 +0100 Subject: Killed the 'commands' namespace. --- commands.cpp | 28 ++++++++++++++-------------- commands.h | 42 ++++++++++++++++++++---------------------- telnet_connection.cpp | 4 ++-- 3 files changed, 36 insertions(+), 38 deletions(-) diff --git a/commands.cpp b/commands.cpp index db693e5..3000f31 100644 --- a/commands.cpp +++ b/commands.cpp @@ -6,14 +6,14 @@ #include -std::vector commands::Commands::ls() { +std::vector Commands::ls() { if(args.size() != 2) { - throw commands::CommandException("usage: ls DIR"); + throw CommandException("usage: ls DIR"); } MusicDirectory::p dir = music::get_directory(args[1]); if(!dir) { - throw commands::CommandException("no such directory"); + throw CommandException("no such directory"); } std::cout << dir->path << std::endl; std::vector result; @@ -29,9 +29,9 @@ std::vector commands::Commands::ls() { return result; } -std::vector commands::Commands::find() { +std::vector Commands::find() { if(args.size() != 3) { - throw commands::CommandException("usage: find TYPE SEARCH"); + throw CommandException("usage: find TYPE SEARCH"); } FindFunction ff; @@ -43,12 +43,12 @@ std::vector commands::Commands::find() { types.push_back(it->first); } std::string s = boost::str(boost::format("unknown search type, must be one of %s") % boost::algorithm::join(types, ", ")); - throw commands::CommandException(s.c_str()); + throw CommandException(s.c_str()); } std::vector ml = ff(args[2]); if(!ml.size()) { - throw commands::CommandException("no results"); + throw CommandException("no results"); } std::vector result; @@ -60,9 +60,9 @@ std::vector commands::Commands::find() { return result; } -std::vector commands::Commands::update() { +std::vector Commands::update() { if(args.size() != 2) { - throw commands::CommandException("usage: update DIR"); + throw CommandException("usage: update DIR"); } io_service.post(boost::bind(music::begin_update, args[1])); @@ -71,14 +71,14 @@ std::vector commands::Commands::update() { return v; } -commands::Commands::Commands(boost::asio::io_service& io_service_, std::vector& args_) : io_service(io_service_), args(args_) { - handlers["ls"] = &commands::Commands::ls; - handlers["find"] = &commands::Commands::find; +Commands::Commands(boost::asio::io_service& io_service_, std::vector& args_) : io_service(io_service_), args(args_) { + handlers["ls"] = &Commands::ls; + handlers["find"] = &Commands::find; find_handlers["artist"] = music::find_artist; - handlers["update"] = &commands::Commands::update; + handlers["update"] = &Commands::update; } -std::vector commands::Commands::operator()() { +std::vector Commands::operator()() { assert(args.size()); Handler h = handlers[args[0]]; if(!h) { diff --git a/commands.h b/commands.h index ae5dc84..084dd02 100644 --- a/commands.h +++ b/commands.h @@ -11,35 +11,33 @@ #include #include -namespace commands { - class Commands; +class Commands; - class CommandException : public std::runtime_error { - public: - CommandException(const char *s) : std::runtime_error(s) {}; - CommandException(std::string s) : std::runtime_error(s.c_str()) {}; - }; +class CommandException : public std::runtime_error { + public: + CommandException(const char *s) : std::runtime_error(s) {}; + CommandException(std::string s) : std::runtime_error(s.c_str()) {}; +}; - class Commands { - private: - typedef boost::function (Commands*)> Handler; - std::map handlers; +class Commands { + private: + typedef boost::function (Commands*)> Handler; + std::map handlers; - typedef boost::function (const std::string artist)> FindFunction; - std::map find_handlers; + typedef boost::function (const std::string artist)> FindFunction; + std::map find_handlers; - boost::asio::io_service& io_service; - std::vector& args; + boost::asio::io_service& io_service; + std::vector& args; - std::vector ls(); - std::vector find(); - std::vector update(); + std::vector ls(); + std::vector find(); + std::vector update(); - public: - Commands(boost::asio::io_service& io_service, std::vector& args); - std::vector operator()(); + public: + Commands(boost::asio::io_service& io_service, std::vector& args); + std::vector operator()(); - }; }; #endif diff --git a/telnet_connection.cpp b/telnet_connection.cpp index c9089a9..2cc723e 100644 --- a/telnet_connection.cpp +++ b/telnet_connection.cpp @@ -34,9 +34,9 @@ void telnet::Connection::handle_read(const boost::system::error_code& error, siz std::vector r; try { - commands::Commands cmd(socket.get_io_service(), args); + Commands cmd(socket.get_io_service(), args); r = cmd(); - } catch(commands::CommandException& ce) { + } catch(CommandException& ce) { std::string s(ce.what()); s += '\n'; boost::asio::write(socket, boost::asio::buffer(s)); -- cgit v1.2.3