command.hpp

Go to the documentation of this file.
00001 /* libobby - Network text editing library
00002  * Copyright (C) 2005, 2006 0x539 dev group
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public
00015  * License along with this program; if not, write to the Free
00016  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 #ifndef _OBBY_COMMAND_HPP_
00020 #define _OBBY_COMMAND_HPP_
00021 
00022 #include <map>
00023 #include <list>
00024 #include <queue>
00025 #include <memory>
00026 
00027 #include <net6/packet.hpp>
00028 
00029 #include "user.hpp"
00030 
00031 namespace obby
00032 {
00033 
00034 // TODO: Put all this in a separate namespace?
00035 
00036 template<typename data_type>
00037 class command_context_from: public ::serialise::default_context_from<data_type>
00038 {
00039 };
00040 
00043 class command_query
00044 {
00045 public:
00049         command_query(const std::string& command,
00050                       const std::string& paramlist);
00051 
00054         command_query(const net6::packet& pack,
00055                       unsigned int& index);
00056 
00059         const std::string& get_command() const;
00060 
00063         const std::string& get_paramlist() const;
00064 
00067         void append_packet(net6::packet& pack) const;
00068 protected:
00069         std::string m_command;
00070         std::string m_paramlist;
00071 };
00072 
00075 class command_result
00076 {
00077 public:
00080         enum type {
00083                 NOT_FOUND,
00084 
00087                 NO_REPLY,
00088 
00091                 REPLY
00092         };
00093 
00096         command_result();
00097 
00100         command_result(type type, const std::string& reply = "");
00101 
00104         command_result(const net6::packet& pack, unsigned int& index);
00105 
00108         type get_type() const;
00109 
00112         const std::string& get_reply() const;
00113 
00116         void append_packet(net6::packet& pack) const;
00117 protected:
00118         type m_type;
00119         std::string m_reply;
00120 };
00121 
00124 class command_paramlist
00125 {
00126 public:
00127         typedef std::vector<std::string>::size_type size_type;
00128 
00131         command_paramlist(const std::string& list);
00132 
00135         size_type count() const;
00136 
00140         template<typename data_type>
00141         data_type get(unsigned int index,
00142                       const ::serialise::context_base_from<data_type>& context =
00143                       command_context_from<data_type>() )
00144         {
00145                 return context.from_string(m_params.at(index) );
00146         }
00147 
00150         const std::string& value(unsigned int index) const;
00151 protected:
00152         std::vector<std::string> m_params;
00153 };
00154 
00157 class command_map
00158 {
00159 public:
00160         typedef sigc::slot<command_result, const user&, const std::string&>
00161                 slot_type;
00162 
00163         command_map();
00164 
00167         void add_command(const std::string& name,
00168                          const std::string& desc,
00169                          const slot_type& func);
00170 
00174         command_result exec_command(const user& from,
00175                                     const command_query& query) const;
00176 protected:
00177         command_result on_help(const user& from, const std::string& paramlist);
00178 
00179         struct command
00180         {
00181                 std::string name;
00182                 std::string desc;
00183                 slot_type func;
00184         };
00185 
00186         typedef std::map<std::string, command> map_type;
00187 
00188         std::auto_ptr<map_type> m_map;
00189 };
00190 
00195 class command_queue
00196 {
00197 public:
00198         typedef sigc::signal<void, const command_query&, const command_result&>
00199                 signal_result_type;
00200 
00201         typedef sigc::signal<void, const command_query&>
00202                 signal_query_failed_type;
00203 
00204         typedef sigc::signal<void, const std::string&, const std::string&>
00205                 signal_help_type;
00206 
00207         command_queue();
00208 
00211         void query(const command_query& query);
00212 
00215         void result(const command_result& result);
00216 
00219         void clear();
00220 
00224         signal_result_type result_event(const std::string& command) const;
00225 
00229         signal_query_failed_type query_failed_event() const;
00230 
00234         signal_help_type help_event() const;
00235 
00236 protected:
00237         void on_help(const command_query& query, const command_result& result);
00238 
00239         typedef std::map<std::string, signal_result_type> map_type;
00240 
00241         std::auto_ptr<map_type> m_map;
00242         std::queue<command_query> m_commands;
00243         signal_query_failed_type m_signal_query_failed;
00244         signal_help_type m_signal_help;
00245 };
00246 
00247 } // namespace obby
00248 
00249 #endif // _OBBY_COMMAND_HPP_

Generated on Fri Jan 11 10:01:32 2008 for obby by  doxygen 1.5.1