mirror of
https://github.com/rakshasa/rtorrent.git
synced 2026-08-15 14:42:30 +00:00
* Return torrent::Object instead of reference from
utils::VariableMap::call_command(...). * More cleanup of the command calls. git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@895 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
@@ -40,7 +40,7 @@
|
||||
|
||||
namespace utils {
|
||||
|
||||
const torrent::Object&
|
||||
const torrent::Object
|
||||
CommandSlot::call_list(Variable* rawVariable, const torrent::Object& rawArgs) {
|
||||
CommandSlot* command = static_cast<CommandSlot*>(rawVariable);
|
||||
|
||||
@@ -74,13 +74,29 @@ CommandSlot::call_list(Variable* rawVariable, const torrent::Object& rawArgs) {
|
||||
// case torrent::Object::TYPE_STRING:
|
||||
// break;
|
||||
case torrent::Object::TYPE_LIST:
|
||||
command->m_slot(rawArgs);
|
||||
break;
|
||||
return command->m_slot(rawArgs);
|
||||
default:
|
||||
throw torrent::input_error("Not a list.");
|
||||
}
|
||||
}
|
||||
|
||||
return m_emptyObject;
|
||||
const torrent::Object
|
||||
CommandSlot::call_string(Variable* rawVariable, const torrent::Object& rawArgs) {
|
||||
CommandSlot* command = static_cast<CommandSlot*>(rawVariable);
|
||||
|
||||
const torrent::Object& arg = to_single_argument(rawArgs);
|
||||
|
||||
switch (arg.type()) {
|
||||
// case torrent::Object::TYPE_VALUE:
|
||||
// break;
|
||||
|
||||
case torrent::Object::TYPE_STRING:
|
||||
return command->m_slot(arg);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw torrent::input_error("Not a string.");
|
||||
}
|
||||
}
|
||||
|
||||
// const torrent::Object&
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
#ifndef RTORRENT_UTILS_COMMAND_SLOT_H
|
||||
#define RTORRENT_UTILS_COMMAND_SLOT_H
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <limits>
|
||||
#include <inttypes.h>
|
||||
@@ -53,8 +54,7 @@ namespace utils {
|
||||
|
||||
class CommandSlot : public Variable {
|
||||
public:
|
||||
// For now, only return void.
|
||||
typedef rak::function1<void, const torrent::Object&> slot_type;
|
||||
typedef rak::function1<torrent::Object, const torrent::Object&> slot_type;
|
||||
|
||||
// template <typename SlotSet>
|
||||
// CommandSlot(SlotSet* slotSet) {
|
||||
@@ -69,7 +69,8 @@ public:
|
||||
|
||||
void set_slot(slot_type::base_type* s) { m_slot.set(s); }
|
||||
|
||||
static const torrent::Object& call_list(Variable* rawVariable, const torrent::Object& args);
|
||||
static const torrent::Object call_list(Variable* rawVariable, const torrent::Object& args);
|
||||
static const torrent::Object call_string(Variable* rawVariable, const torrent::Object& args);
|
||||
|
||||
// static const torrent::Object& get_list(Variable* rawVariable, const torrent::Object& args);
|
||||
|
||||
@@ -77,6 +78,64 @@ private:
|
||||
slot_type m_slot;
|
||||
};
|
||||
|
||||
// Some slots that convert torrent::Object arguments to proper
|
||||
// function calls.
|
||||
|
||||
template <typename Func, typename Result = typename Func::result_type>
|
||||
class object_void_fn_t : public rak::function_base1<torrent::Object, const torrent::Object&> {
|
||||
public:
|
||||
object_void_fn_t(Func func) : m_func(func) {}
|
||||
|
||||
virtual torrent::Object operator () (const torrent::Object& arg1) { return torrent::Object(m_func()); }
|
||||
|
||||
private:
|
||||
Func m_func;
|
||||
};
|
||||
|
||||
template <typename Func>
|
||||
class object_void_fn_t<Func, void> : public rak::function_base1<torrent::Object, const torrent::Object&> {
|
||||
public:
|
||||
object_void_fn_t(Func func) : m_func(func) {}
|
||||
|
||||
virtual torrent::Object operator () (const torrent::Object& arg1) {
|
||||
m_func();
|
||||
return torrent::Object();
|
||||
}
|
||||
|
||||
private:
|
||||
Func m_func;
|
||||
};
|
||||
|
||||
template <typename Func, typename Result = typename Func::result_type>
|
||||
class object_string_fn1_t : public rak::function_base1<torrent::Object, const torrent::Object&> {
|
||||
public:
|
||||
object_string_fn1_t(Func func) : m_func(func) {}
|
||||
|
||||
virtual torrent::Object operator () (const torrent::Object& arg1) { return torrent::Object(m_func(arg1.as_string())); }
|
||||
|
||||
private:
|
||||
Func m_func;
|
||||
};
|
||||
|
||||
template <typename Func>
|
||||
class object_string_fn1_t<Func, void> : public rak::function_base1<torrent::Object, const torrent::Object&> {
|
||||
public:
|
||||
object_string_fn1_t(Func func) : m_func(func) {}
|
||||
|
||||
virtual torrent::Object operator () (const torrent::Object& arg1) {
|
||||
m_func(arg1.as_string());
|
||||
|
||||
return torrent::Object();
|
||||
}
|
||||
|
||||
private:
|
||||
Func m_func;
|
||||
};
|
||||
|
||||
template <typename Return> object_void_fn_t<Return (*)(void), Return>* object_fn(Return (*func)(void)) { return new object_void_fn_t<Return (*)(void), Return>(func); }
|
||||
|
||||
template <typename Func> object_string_fn1_t<Func>* object_string_fn(Func func) { return new object_string_fn1_t<Func>(func); }
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -40,7 +40,10 @@
|
||||
|
||||
namespace utils {
|
||||
|
||||
const torrent::Object&
|
||||
extern CommandVariable commandVariables[10];
|
||||
extern CommandVariable* commandVariablesItr;
|
||||
|
||||
const torrent::Object
|
||||
CommandVariable::set_bool(Variable* rawVariable, const torrent::Object& rawArgs) {
|
||||
CommandVariable* variable = static_cast<CommandVariable*>(rawVariable);
|
||||
|
||||
@@ -71,14 +74,14 @@ CommandVariable::set_bool(Variable* rawVariable, const torrent::Object& rawArgs)
|
||||
return variable->m_variable;
|
||||
}
|
||||
|
||||
const torrent::Object&
|
||||
const torrent::Object
|
||||
CommandVariable::get_bool(Variable* rawVariable, const torrent::Object& args) {
|
||||
CommandVariable* variable = static_cast<CommandVariable*>(rawVariable);
|
||||
|
||||
return variable->m_variable;
|
||||
}
|
||||
|
||||
const torrent::Object&
|
||||
const torrent::Object
|
||||
CommandVariable::set_value(Variable* rawVariable, const torrent::Object& rawArgs) {
|
||||
CommandVariable* variable = static_cast<CommandVariable*>(rawVariable);
|
||||
|
||||
@@ -107,14 +110,14 @@ CommandVariable::set_value(Variable* rawVariable, const torrent::Object& rawArgs
|
||||
return variable->m_variable;
|
||||
}
|
||||
|
||||
const torrent::Object&
|
||||
const torrent::Object
|
||||
CommandVariable::get_value(Variable* rawVariable, const torrent::Object& args) {
|
||||
CommandVariable* variable = static_cast<CommandVariable*>(rawVariable);
|
||||
|
||||
return variable->m_variable;
|
||||
}
|
||||
|
||||
const torrent::Object&
|
||||
const torrent::Object
|
||||
CommandVariable::set_string(Variable* rawVariable, const torrent::Object& rawArgs) {
|
||||
CommandVariable* variable = static_cast<CommandVariable*>(rawVariable);
|
||||
|
||||
@@ -140,7 +143,7 @@ CommandVariable::set_string(Variable* rawVariable, const torrent::Object& rawArg
|
||||
return variable->m_variable;
|
||||
}
|
||||
|
||||
const torrent::Object&
|
||||
const torrent::Object
|
||||
CommandVariable::get_string(Variable* rawVariable, const torrent::Object& args) {
|
||||
CommandVariable* variable = static_cast<CommandVariable*>(rawVariable);
|
||||
|
||||
|
||||
@@ -50,17 +50,17 @@ class CommandVariable : public Variable {
|
||||
public:
|
||||
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; }
|
||||
const torrent::Object variable() const { return m_variable; }
|
||||
void set_variable(const torrent::Object& var) { m_variable = var; }
|
||||
|
||||
static const torrent::Object& set_bool(Variable* rawVariable, const torrent::Object& args);
|
||||
static const torrent::Object& get_bool(Variable* rawVariable, const torrent::Object& args);
|
||||
static const torrent::Object set_bool(Variable* rawVariable, const torrent::Object& args);
|
||||
static const torrent::Object get_bool(Variable* rawVariable, const torrent::Object& args);
|
||||
|
||||
static const torrent::Object& set_value(Variable* rawVariable, const torrent::Object& args);
|
||||
static const torrent::Object& get_value(Variable* rawVariable, const torrent::Object& args);
|
||||
static const torrent::Object set_value(Variable* rawVariable, const torrent::Object& args);
|
||||
static const torrent::Object get_value(Variable* rawVariable, const torrent::Object& args);
|
||||
|
||||
static const torrent::Object& set_string(Variable* rawVariable, const torrent::Object& args);
|
||||
static const torrent::Object& get_string(Variable* rawVariable, const torrent::Object& args);
|
||||
static const torrent::Object set_string(Variable* rawVariable, const torrent::Object& args);
|
||||
static const torrent::Object get_string(Variable* rawVariable, const torrent::Object& args);
|
||||
|
||||
private:
|
||||
torrent::Object m_variable;
|
||||
|
||||
@@ -71,7 +71,7 @@ VariableMap::insert(key_type key, Variable* variable, generic_slot genericSlot,
|
||||
base_type::insert(itr, value_type(key, variable_map_data_type(variable, genericSlot, NULL, flags)));
|
||||
}
|
||||
|
||||
const VariableMap::mapped_type&
|
||||
const VariableMap::mapped_type
|
||||
VariableMap::get(key_type key) const {
|
||||
const_iterator itr = base_type::find(key);
|
||||
|
||||
@@ -84,7 +84,7 @@ VariableMap::get(key_type key) const {
|
||||
return itr->second.m_variable->get();
|
||||
}
|
||||
|
||||
const VariableMap::mapped_type&
|
||||
const VariableMap::mapped_type
|
||||
VariableMap::get_d(core::Download* download, key_type key) const {
|
||||
const_iterator itr = base_type::find(key);
|
||||
|
||||
@@ -266,7 +266,7 @@ VariableMap::process_file(key_type path) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const VariableMap::mapped_type&
|
||||
const VariableMap::mapped_type
|
||||
VariableMap::call_command(key_type key, const mapped_type& arg) {
|
||||
const_iterator itr = base_type::find(key);
|
||||
|
||||
@@ -279,7 +279,7 @@ VariableMap::call_command(key_type key, const mapped_type& arg) {
|
||||
return itr->second.m_genericSlot(itr->second.m_variable, arg);
|
||||
}
|
||||
|
||||
const VariableMap::mapped_type&
|
||||
const VariableMap::mapped_type
|
||||
VariableMap::call_command_get(key_type key, const mapped_type& arg) {
|
||||
const_iterator itr = base_type::find(key);
|
||||
|
||||
@@ -292,7 +292,7 @@ VariableMap::call_command_get(key_type key, const mapped_type& arg) {
|
||||
return get(key);
|
||||
}
|
||||
|
||||
const VariableMap::mapped_type&
|
||||
const VariableMap::mapped_type
|
||||
VariableMap::call_command_set(key_type key, const mapped_type& arg) {
|
||||
const_iterator itr = base_type::find(key);
|
||||
|
||||
|
||||
@@ -63,8 +63,8 @@ struct variable_map_data_type {
|
||||
// Some commands will need to share data, like get/set a variable. So
|
||||
// instead of using a single virtual member function, each command
|
||||
// will register a member function pointer to be used instead.
|
||||
typedef const torrent::Object& (*generic_slot)(Variable*, const torrent::Object&);
|
||||
typedef const torrent::Object& (*download_slot)(Variable*, core::Download*, const torrent::Object&);
|
||||
typedef const torrent::Object (*generic_slot)(Variable*, const torrent::Object&);
|
||||
typedef const torrent::Object (*download_slot)(Variable*, core::Download*, const torrent::Object&);
|
||||
|
||||
variable_map_data_type(Variable* variable, generic_slot genericSlot, download_slot downloadSlot, int flags) :
|
||||
m_variable(variable), m_genericSlot(genericSlot), m_downloadSlot(downloadSlot), m_flags(flags) {}
|
||||
@@ -108,11 +108,11 @@ public:
|
||||
|
||||
// Consider uninlining the helper functions.
|
||||
|
||||
const mapped_type& get(key_type key) const;
|
||||
const mapped_type& get_d(core::Download* download, key_type key) const;
|
||||
const mapped_type get(key_type key) const;
|
||||
const mapped_type get_d(core::Download* download, key_type key) const;
|
||||
|
||||
const std::string& get_string(key_type key) const { return get(key).as_string(); }
|
||||
const std::string& get_d_string(core::Download* download, key_type key) const { return get_d(download, key).as_string(); }
|
||||
const std::string get_string(key_type key) const { return get(key).as_string(); }
|
||||
const std::string get_d_string(core::Download* download, key_type key) const { return get_d(download, key).as_string(); }
|
||||
|
||||
mapped_value_type get_value(key_type key) const { return get(key).as_value(); }
|
||||
mapped_value_type get_d_value(core::Download* download, key_type key) const { return get_d(download, key).as_value(); }
|
||||
@@ -143,10 +143,10 @@ public:
|
||||
|
||||
// The new API, which is atm just a wrapper over the old and
|
||||
// requires seperate calls to get and set. These will be merged.
|
||||
const mapped_type& call_command(key_type key, const mapped_type& arg);
|
||||
const mapped_type call_command(key_type key, const mapped_type& arg);
|
||||
|
||||
const mapped_type& call_command_get(key_type key, const mapped_type& arg);
|
||||
const mapped_type& call_command_set(key_type key, const mapped_type& arg);
|
||||
const mapped_type call_command_get(key_type key, const mapped_type& arg);
|
||||
const mapped_type call_command_set(key_type key, const mapped_type& arg);
|
||||
|
||||
private:
|
||||
VariableMap(const VariableMap&);
|
||||
|
||||
Reference in New Issue
Block a user