* Cleanup of old command code.

git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@1156 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2010-04-04 04:44:45 +00:00
parent 4e4540e60f
commit 90da65f8a1
15 changed files with 334 additions and 386 deletions
+1
View File
@@ -2,6 +2,7 @@ noinst_LIBRARIES = libsub_rpc.a
libsub_rpc_a_SOURCES = \
command.h \
command_impl.h \
command_function.cc \
command_function.h \
command_map.cc \
+24 -42
View File
@@ -34,8 +34,8 @@
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#ifndef RTORRENT_RPC_VARIABLE_H
#define RTORRENT_RPC_VARIABLE_H
#ifndef RTORRENT_RPC_COMMAND_H
#define RTORRENT_RPC_COMMAND_H
#include <torrent/object.h>
#include <torrent/data/file_list_iterator.h>
@@ -128,14 +128,30 @@ public:
static const int target_download_pair = 7;
static const unsigned int max_arguments = 10;
struct stack_type {
torrent::Object* begin() { return reinterpret_cast<torrent::Object*>(buffer); }
torrent::Object* end() { return reinterpret_cast<torrent::Object*>(buffer) + max_arguments; }
static stack_type* from_data(char* data) { return reinterpret_cast<stack_type*>(data); }
char buffer[sizeof(torrent::Object) * max_arguments];
};
Command() {}
virtual ~Command() {}
static torrent::Object* argument(unsigned int index) { return m_arguments + index; }
static torrent::Object& argument_ref(unsigned int index) { return *(m_arguments + index); }
static torrent::Object* argument(unsigned int index) { return m_arguments.begin() + index; }
static torrent::Object& argument_ref(unsigned int index) { return *(m_arguments.begin() + index); }
static const unsigned int max_arguments = 10;
static torrent::Object m_arguments[max_arguments];
static stack_type m_arguments;
static torrent::Object* stack_begin() { return m_arguments.begin(); }
static torrent::Object* stack_end() { return m_arguments.end(); }
static torrent::Object* push_stack(const torrent::Object::list_type& args, torrent::Object* tmp_stack);
static void pop_stack(torrent::Object* first_stack, torrent::Object* last_stack);
protected:
Command(const Command&);
@@ -151,54 +167,20 @@ struct target_type_id {
// Nothing here, so we cause an error.
};
//template <> struct target_type_id<Command::generic_slot> { static const int value = Command::target_generic; };
template <> struct target_type_id<Command::cleaned_slot> { static const int value = Command::target_generic; };
template <> struct target_type_id<Command::any_slot> { static const int value = Command::target_any; };
template <> struct target_type_id<Command::download_slot> { static const int value = Command::target_download; };
template <> struct target_type_id<Command::peer_slot> { static const int value = Command::target_peer; };
template <> struct target_type_id<Command::tracker_slot> { static const int value = Command::target_tracker; };
template <> struct target_type_id<Command::file_slot> { static const int value = Command::target_file; };
template <> struct target_type_id<Command::file_itr_slot> { static const int value = Command::target_file_itr; };
template <> struct target_type_id<Command::download_pair_slot> { static const int value = Command::target_download_pair; };
template <> struct target_type_id<> { static const int value = Command::target_generic; };
template <> struct target_type_id<target_type> { static const int value = Command::target_any; static const int proper_type = 1; };
template <> struct target_type_id<core::Download*> { static const int value = Command::target_download; static const int proper_type = 1; };
template <> struct target_type_id<torrent::Peer*> { static const int value = Command::target_peer; static const int proper_type = 1; };
template <> struct target_type_id<torrent::Tracker*> { static const int value = Command::target_tracker; static const int proper_type = 1; };
template <> struct target_type_id<torrent::File*> { static const int value = Command::target_file; static const int proper_type = 1; };
template <> struct target_type_id<torrent::FileListIterator*> { static const int value = Command::target_file_itr; static const int proper_type = 1; };
template <> struct target_type_id<core::Download*, core::Download*> { static const int value = Command::target_download_pair; };
template <typename T> inline bool
is_target_compatible(const target_type& target) { return target.first == target_type_id<T>::value; }
template <> inline bool
is_target_compatible<target_type>(const target_type& target) { return true; }
template <> inline bool
is_target_compatible<torrent::File*>(const target_type& target) { return target.first == Command::target_file || Command::target_file_itr; }
// Splitting pairs into separate targets.
inline bool is_target_pair(const target_type& target) { return target.first >= Command::target_download_pair; }
template <typename T> inline T
get_target_cast(target_type target, int type = target_type_id<T>::value) { return (T)target.second; }
template <> inline target_type
get_target_cast<target_type>(target_type target, int type) { return target; }
template <> inline torrent::File*
get_target_cast<torrent::File*>(target_type target, int type) {
if (target.first == Command::target_file_itr)
return static_cast<torrent::FileListIterator*>(target.second)->file();
else
return static_cast<torrent::File*>(target.second);
}
inline target_type get_target_left(const target_type& target) { return target_type(target.first - 5, target.second); }
inline target_type get_target_right(const target_type& target) { return target_type(target.first - 5, target.third); }
}
#include "command_impl.h"
#endif
-7
View File
@@ -129,13 +129,6 @@ CommandFunction::call(Command* rawCommand, target_type target, const torrent::Ob
}
}
const torrent::Object
CommandFunction::call_redirect(Command* rawCommand, target_type target, const torrent::Object& args) {
CommandFunction* command = reinterpret_cast<CommandFunction*>(rawCommand);
return commands.call_command(command->m_command.c_str(), args, target);
}
const torrent::Object
CommandFunctionList::call(Command* rawCommand, target_type target, const torrent::Object& args) {
char* buffer[sizeof(torrent::Object) * Command::max_arguments];
-1
View File
@@ -55,7 +55,6 @@ public:
void set_command(const std::string& cmd) { m_command = cmd; }
static const torrent::Object call(Command* rawCommand, target_type target, const torrent::Object& args);
static const torrent::Object call_redirect(Command* rawCommand, target_type target, const torrent::Object& args);
private:
// TODO: Replace with a delete-me flag and const char*.
+38 -58
View File
@@ -55,7 +55,7 @@
namespace rpc {
torrent::Object Command::m_arguments[Command::max_arguments];
Command::stack_type Command::m_arguments;
CommandMap::~CommandMap() {
std::vector<const char*> keys;
@@ -95,8 +95,7 @@ CommandMap::insert(key_type key, const command_map_data_type src) {
itr = base_type::insert(itr, value_type(key, command_map_data_type(src.m_variable, src.m_flags | flag_dont_delete, src.m_parm, src.m_doc)));
// We can assume all the slots are the same size.
itr->second.m_target = src.m_target;
itr->second.m_genericSlot = src.m_genericSlot;
itr->second.m_anySlot = src.m_anySlot;
}
void
@@ -144,8 +143,7 @@ CommandMap::create_redirect(key_type key_new, key_type key_dest, int flags) {
dest_itr->second.m_doc)));
// We can assume all the slots are the same size.
itr->second.m_target = dest_itr->second.m_target;
itr->second.m_genericSlot = dest_itr->second.m_genericSlot;
itr->second.m_anySlot = dest_itr->second.m_anySlot;
}
const CommandMap::mapped_type
@@ -165,72 +163,54 @@ CommandMap::call_command(key_type key, const mapped_type& arg, target_type targe
if (itr == base_type::end())
throw torrent::input_error("Command \"" + std::string(key) + "\" does not exist.");
if (target.first != Command::target_generic && target.second == NULL) {
// We received a target that is NULL, so throw an exception unless
// we can convert it to a void target.
if (itr->second.m_target > Command::target_any)
throw torrent::input_error("Command type mis-match.");
// if (target.first != Command::target_generic && target.second == NULL) {
// // We received a target that is NULL, so throw an exception unless
// // we can convert it to a void target.
// if (itr->second.m_target > Command::target_any)
// throw torrent::input_error("Command type mis-match.");
target.first = Command::target_generic;
}
// target.first = Command::target_generic;
// }
if (itr->second.m_target != target.first && itr->second.m_target > Command::target_any) {
// Mismatch between the target and command type. If it is not
// possible to convert, then throw an input error.
if (target.first == Command::target_file_itr && itr->second.m_target == Command::target_file)
target = target_type((int)Command::target_file, static_cast<torrent::FileListIterator*>(target.second)->file());
else
throw torrent::input_error("Command type mis-match.");
}
// if (itr->second.m_target != target.first && itr->second.m_target > Command::target_any) {
// // Mismatch between the target and command type. If it is not
// // possible to convert, then throw an input error.
// if (target.first == Command::target_file_itr && itr->second.m_target == Command::target_file)
// target = target_type((int)Command::target_file, static_cast<torrent::FileListIterator*>(target.second)->file());
// else
// throw torrent::input_error("Command type mis-match.");
// }
// This _should_ be optimized int just two calls.
switch (itr->second.m_target) {
case Command::target_any: return itr->second.m_anySlot(itr->second.m_variable, target, arg);
// switch (itr->second.m_target) {
// case Command::target_any: return itr->second.m_anySlot(itr->second.m_variable, target, arg);
// default: throw torrent::internal_error("CommandMap::call_command(...) Invalid target.");
// }
case Command::target_generic:
case Command::target_download:
case Command::target_peer:
case Command::target_tracker:
case Command::target_file:
case Command::target_file_itr: return itr->second.m_genericSlot(itr->second.m_variable, (target_wrapper<void>::cleaned_type)target.second, arg);
// This should only allow target_type to be passed or something, in
// order to optimize this away.
case Command::target_download_pair: return itr->second.m_downloadPairSlot(itr->second.m_variable, (core::Download*)target.second, (core::Download*)target.third, arg);
default: throw torrent::internal_error("CommandMap::call_command(...) Invalid target.");
}
return itr->second.m_anySlot(itr->second.m_variable, target, arg);
}
const CommandMap::mapped_type
CommandMap::call_command(const_iterator itr, const mapped_type& arg, target_type target) {
if (target.first != Command::target_generic && target.second == NULL) {
// We received a target that is NULL, so throw an exception unless
// we can convert it to a void target.
if (itr->second.m_target > Command::target_any)
throw torrent::input_error("Command type mis-match.");
// if (target.first != Command::target_generic && target.second == NULL) {
// // We received a target that is NULL, so throw an exception unless
// // we can convert it to a void target.
// if (itr->second.m_target > Command::target_any)
// throw torrent::input_error("Command type mis-match.");
target.first = Command::target_generic;
}
// target.first = Command::target_generic;
// }
if (itr->second.m_target != target.first && itr->second.m_target > Command::target_any)
throw torrent::input_error("Command type mis-match.");
// if (itr->second.m_target != target.first && itr->second.m_target > Command::target_any)
// throw torrent::input_error("Command type mis-match.");
// This _should_ be optimized int just two calls.
switch (itr->second.m_target) {
case Command::target_any: return itr->second.m_anySlot(itr->second.m_variable, target, arg);
// // This _should_ be optimized int just two calls.
// switch (itr->second.m_target) {
// case Command::target_any: return itr->second.m_anySlot(itr->second.m_variable, target, arg);
// default: throw torrent::internal_error("CommandMap::call_command(...) Invalid target.");
// }
case Command::target_generic:
case Command::target_download:
case Command::target_peer:
case Command::target_tracker:
case Command::target_file:
case Command::target_file_itr: return itr->second.m_genericSlot(itr->second.m_variable, (target_wrapper<void>::cleaned_type)target.second, arg);
case Command::target_download_pair: return itr->second.m_downloadPairSlot(itr->second.m_variable, (core::Download*)target.second, (core::Download*)target.third, arg);
default: throw torrent::internal_error("CommandMap::call_command(...) Invalid target.");
}
return itr->second.m_anySlot(itr->second.m_variable, target, arg);
}
}
+3 -20
View File
@@ -60,24 +60,10 @@ struct command_map_data_type {
command_map_data_type(Command* variable, int flags, const char* parm, const char* doc) :
m_variable(variable), m_flags(flags), m_parm(parm), m_doc(doc) {}
int target() const { return m_target; }
Command* m_variable;
union {
Command::cleaned_slot m_genericSlot;
Command::any_slot m_anySlot;
Command::download_slot m_downloadSlot;
Command::file_slot m_fileSlot;
Command::file_itr_slot m_fileItrSlot;
Command::peer_slot m_peerSlot;
Command::tracker_slot m_trackerSlot;
Command::download_pair_slot m_downloadPairSlot;
};
Command::any_slot m_anySlot;
int m_flags;
int m_target;
const char* m_parm;
const char* m_doc;
@@ -118,12 +104,9 @@ public:
iterator insert(key_type key, Command* variable, int flags, const char* parm, const char* doc);
// Make this a wrapper call to insert without extra fluff.
template <typename T>
void insert_type(key_type key, Command* variable, T targetSlot, int flags, const char* parm, const char* doc) {
void insert_type(key_type key, Command* variable, Command::any_slot targetSlot, int flags, const char* parm, const char* doc) {
iterator itr = insert(key, variable, flags, parm, doc);
itr->second.m_target = target_type_id<T>::value;
itr->second.m_genericSlot = (Command::cleaned_slot)targetSlot;
itr->second.m_anySlot = targetSlot;
}
void insert(key_type key, const command_map_data_type src);
+8 -8
View File
@@ -42,7 +42,7 @@
namespace rpc {
const torrent::Object
CommandVariable::set_bool(Command* rawCommand, cleaned_type target, const torrent::Object& rawArgs) {
CommandVariable::set_bool(Command* rawCommand, target_type target, const torrent::Object& rawArgs) {
CommandVariable* variable = static_cast<CommandVariable*>(rawCommand);
const torrent::Object& arg = convert_to_single_argument(rawArgs);
@@ -73,14 +73,14 @@ CommandVariable::set_bool(Command* rawCommand, cleaned_type target, const torren
}
const torrent::Object
CommandVariable::get_bool(Command* rawCommand, cleaned_type target, const torrent::Object& args) {
CommandVariable::get_bool(Command* rawCommand, target_type target, const torrent::Object& args) {
CommandVariable* variable = static_cast<CommandVariable*>(rawCommand);
return variable->m_variable;
}
const torrent::Object
CommandVariable::set_value(Command* rawCommand, cleaned_type target, const torrent::Object& rawArgs) {
CommandVariable::set_value(Command* rawCommand, target_type target, const torrent::Object& rawArgs) {
CommandVariable* variable = static_cast<CommandVariable*>(rawCommand);
const torrent::Object& arg = convert_to_single_argument(rawArgs);
@@ -109,14 +109,14 @@ CommandVariable::set_value(Command* rawCommand, cleaned_type target, const torre
}
const torrent::Object
CommandVariable::get_value(Command* rawCommand, cleaned_type target, const torrent::Object& args) {
CommandVariable::get_value(Command* rawCommand, target_type target, const torrent::Object& args) {
CommandVariable* variable = static_cast<CommandVariable*>(rawCommand);
return variable->m_variable;
}
const torrent::Object
CommandVariable::set_string(Command* rawCommand, cleaned_type target, const torrent::Object& rawArgs) {
CommandVariable::set_string(Command* rawCommand, target_type target, const torrent::Object& rawArgs) {
CommandVariable* variable = static_cast<CommandVariable*>(rawCommand);
const torrent::Object& arg = convert_to_single_argument(rawArgs);
@@ -142,7 +142,7 @@ CommandVariable::set_string(Command* rawCommand, cleaned_type target, const torr
}
const torrent::Object
CommandVariable::get_string(Command* rawCommand, cleaned_type target, const torrent::Object& args) {
CommandVariable::get_string(Command* rawCommand, target_type target, const torrent::Object& args) {
CommandVariable* variable = static_cast<CommandVariable*>(rawCommand);
return variable->m_variable;
@@ -153,14 +153,14 @@ CommandVariable::get_string(Command* rawCommand, cleaned_type target, const torr
//
const torrent::Object
CommandObjectPtr::set_generic(Command* rawCommand, cleaned_type target, const torrent::Object& rawArgs) {
CommandObjectPtr::set_generic(Command* rawCommand, target_type target, const torrent::Object& rawArgs) {
CommandObjectPtr* command = static_cast<CommandObjectPtr*>(rawCommand);
return (*command->m_object = rawArgs);
}
const torrent::Object
CommandObjectPtr::get_generic(Command* rawCommand, cleaned_type target, const torrent::Object& args) {
CommandObjectPtr::get_generic(Command* rawCommand, target_type target, const torrent::Object& args) {
CommandObjectPtr* command = static_cast<CommandObjectPtr*>(rawCommand);
return *command->m_object;
+14 -18
View File
@@ -48,21 +48,19 @@ namespace rpc {
class CommandVariable : public Command {
public:
typedef target_wrapper<void>::cleaned_type cleaned_type;
CommandVariable(const torrent::Object& v = torrent::Object()) : m_variable(v) {}
const torrent::Object variable() const { return m_variable; }
void set_variable(const torrent::Object& var) { m_variable = var; }
static const torrent::Object set_bool(Command* rawCommand, cleaned_type target, const torrent::Object& args);
static const torrent::Object get_bool(Command* rawCommand, cleaned_type target, const torrent::Object& args);
static const torrent::Object set_bool(Command* rawCommand, target_type target, const torrent::Object& args);
static const torrent::Object get_bool(Command* rawCommand, target_type target, const torrent::Object& args);
static const torrent::Object set_value(Command* rawCommand, cleaned_type target, const torrent::Object& args);
static const torrent::Object get_value(Command* rawCommand, cleaned_type target, const torrent::Object& args);
static const torrent::Object set_value(Command* rawCommand, target_type target, const torrent::Object& args);
static const torrent::Object get_value(Command* rawCommand, target_type target, const torrent::Object& args);
static const torrent::Object set_string(Command* rawCommand, cleaned_type target, const torrent::Object& args);
static const torrent::Object get_string(Command* rawCommand, cleaned_type target, const torrent::Object& args);
static const torrent::Object set_string(Command* rawCommand, target_type target, const torrent::Object& args);
static const torrent::Object get_string(Command* rawCommand, target_type target, const torrent::Object& args);
private:
torrent::Object m_variable;
@@ -70,24 +68,22 @@ private:
class CommandObjectPtr : public Command {
public:
typedef target_wrapper<void>::cleaned_type cleaned_type;
CommandObjectPtr(torrent::Object* obj = NULL) : m_object(obj) {}
const torrent::Object* object() const { return m_object; }
void set_object(torrent::Object* obj) { m_object = obj; }
static const torrent::Object set_generic(Command* rawCommand, cleaned_type target, const torrent::Object& args);
static const torrent::Object get_generic(Command* rawCommand, cleaned_type target, const torrent::Object& args);
static const torrent::Object set_generic(Command* rawCommand, target_type target, const torrent::Object& args);
static const torrent::Object get_generic(Command* rawCommand, target_type target, const torrent::Object& args);
// static const torrent::Object set_bool(Command* rawCommand, cleaned_type target, const torrent::Object& args);
// static const torrent::Object get_bool(Command* rawCommand, cleaned_type target, const torrent::Object& args);
// static const torrent::Object set_bool(Command* rawCommand, target_type target, const torrent::Object& args);
// static const torrent::Object get_bool(Command* rawCommand, target_type target, const torrent::Object& args);
// static const torrent::Object set_value(Command* rawCommand, cleaned_type target, const torrent::Object& args);
// static const torrent::Object get_value(Command* rawCommand, cleaned_type target, const torrent::Object& args);
// static const torrent::Object set_value(Command* rawCommand, target_type target, const torrent::Object& args);
// static const torrent::Object get_value(Command* rawCommand, target_type target, const torrent::Object& args);
// static const torrent::Object set_string(Command* rawCommand, cleaned_type target, const torrent::Object& args);
// static const torrent::Object get_string(Command* rawCommand, cleaned_type target, const torrent::Object& args);
// static const torrent::Object set_string(Command* rawCommand, target_type target, const torrent::Object& args);
// static const torrent::Object get_string(Command* rawCommand, target_type target, const torrent::Object& args);
private:
torrent::Object* m_object;
+1 -1
View File
@@ -446,7 +446,7 @@ xmlrpc_call_command(xmlrpc_env* env, xmlrpc_value* args, void* voidServerInfo) {
if (itr->second.m_flags & CommandMap::flag_no_target)
xmlrpc_to_object(env, args, XmlRpc::call_generic, &target).swap(object);
else
xmlrpc_to_object(env, args, itr->second.target(), &target).swap(object);
xmlrpc_to_object(env, args, XmlRpc::call_any, &target).swap(object);
if (env->fault_occurred)
return NULL;