* Use user-modifiable commands instead of slots for the 'on_*' events.

git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@1070 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2008-10-01 14:19:51 +00:00
parent d7c2960d1e
commit 07b9616ef6
13 changed files with 332 additions and 139 deletions
+42
View File
@@ -36,6 +36,10 @@
#include "config.h"
#include <algorithm>
#include <functional>
#include <rak/functional.h>
#include "parse.h"
#include "parse_commands.h"
#include "command_function.h"
@@ -51,4 +55,42 @@ CommandFunction::call(Command* rawCommand, target_type target, const torrent::Ob
return torrent::Object();
}
const torrent::Object
CommandFunctionList::call(Command* rawCommand, target_type target, const torrent::Object& args) {
CommandFunctionList* command = reinterpret_cast<CommandFunctionList*>(rawCommand);
for (base_type::const_iterator itr = command->begin(), last = command->end(); itr != last; itr++)
parse_command_multiple(target, itr->second.c_str(), itr->second.c_str() + itr->second.size());
return torrent::Object();
}
CommandFunctionList::const_iterator
CommandFunctionList::find(const char* key) {
base_type::iterator itr = std::find_if(begin(), end(), rak::greater_equal(key, rak::mem_ref(&base_type::value_type::first)));
if (itr == end() || itr->first != key)
return end();
else
return itr;
}
void
CommandFunctionList::insert(const std::string& key, const std::string& cmd) {
base_type::iterator itr = std::find_if(begin(), end(), rak::greater_equal(key, rak::mem_ref(&base_type::value_type::first)));
if (itr != end() && itr->first == key)
itr->second = cmd;
else
base_type::insert(itr, base_type::value_type(key, cmd));
}
void
CommandFunctionList::erase(const std::string& key) {
base_type::iterator itr = std::find_if(begin(), end(), rak::greater_equal(key, rak::mem_ref(&base_type::value_type::first)));
if (itr != end() && itr->first == key)
base_type::erase(itr);
}
}
+25 -3
View File
@@ -38,6 +38,7 @@
#define RTORRENT_RPC_COMMAND_FUNCTION_H
#include <string>
#include <vector>
#include <limits>
#include <inttypes.h>
#include <torrent/object.h>
@@ -50,14 +51,35 @@ class CommandFunction : public Command {
public:
CommandFunction(const std::string& cmd = std::string()) : m_command(cmd) {}
const std::string& command() const { return m_command; }
void set_command(const std::string& cmd) { m_command = cmd; }
const std::string& command() const { return m_command; }
void set_command(const std::string& cmd) { m_command = cmd; }
static const torrent::Object call(Command* rawCommand, target_type target, const torrent::Object& args);
private:
// TODO: Replace with a delete-me flag and const char*.
std::string m_command;
std::string m_command;
};
class CommandFunctionList : public Command,
private std::vector<std::pair<std::string, std::string> > {
public:
typedef std::vector<std::pair<std::string, std::string> > base_type;
using Command::value_type;
using base_type::iterator;
using base_type::const_iterator;
using base_type::begin;
using base_type::end;
CommandFunctionList() {}
const_iterator find(const char* key);
void insert(const std::string& key, const std::string& cmd);
void erase(const std::string& key);
static const torrent::Object call(Command* rawCommand, target_type target, const torrent::Object& args);
};
}
+15
View File
@@ -41,6 +41,11 @@
#include <torrent/object.h>
#include <torrent/data/file_list_iterator.h>
// Get better logging...
#include "globals.h"
#include "control.h"
#include "core/manager.h"
#include "command.h"
#include "command_map.h"
@@ -99,6 +104,16 @@ CommandMap::erase(iterator itr) {
delete [] key;
}
const CommandMap::mapped_type
CommandMap::call_catch(key_type key, target_type target, const mapped_type& args, const char* err) {
try {
return call_command(key, args, target);
} catch (torrent::input_error& e) {
control->core()->push_log((err + std::string(e.what())).c_str());
return torrent::Object();
}
}
const CommandMap::mapped_type
CommandMap::call_command(key_type key, const mapped_type& arg, target_type target) {
const_iterator itr = base_type::find(key);
+24 -1
View File
@@ -62,7 +62,7 @@ struct command_map_data_type {
int target() const { return m_target; }
Command* m_variable;
Command* m_variable;
union {
Command::cleaned_slot m_genericSlot;
@@ -124,6 +124,9 @@ public:
void insert(key_type key, const command_map_data_type src);
void erase(iterator itr);
const mapped_type call(key_type key, target_type target, const mapped_type& args = mapped_type()) { return call_command(key, args, target); }
const mapped_type call_catch(key_type key, target_type target, const mapped_type& args = mapped_type(), const char* err = "Command failed: ");
const mapped_type call_command (key_type key, const mapped_type& arg, target_type target = target_type((int)Command::target_generic, NULL));
const mapped_type call_command (const_iterator itr, const mapped_type& arg, target_type target = target_type((int)Command::target_generic, NULL));
@@ -151,6 +154,26 @@ inline target_type make_target_pair(T target1, T target2) {
return target_type((int)target_type_id<T, T>::value, target1, target2);
}
// TODO: Helper-functions that really should be in the
// torrent/object.h header.
inline torrent::Object
create_object_list(const torrent::Object& o1, const torrent::Object& o2) {
torrent::Object tmp = torrent::Object::create_list();
tmp.as_list().push_back(o1);
tmp.as_list().push_back(o2);
return tmp;
}
inline torrent::Object
create_object_list(const torrent::Object& o1, const torrent::Object& o2, const torrent::Object& o3) {
torrent::Object tmp = torrent::Object::create_list();
tmp.as_list().push_back(o1);
tmp.as_list().push_back(o2);
tmp.as_list().push_back(o3);
return tmp;
}
}
#endif