mirror of
https://github.com/rakshasa/rtorrent.git
synced 2026-08-14 14:12:31 +00:00
* Starting work on merging get/set variable. Moved the bool and value
variables, still might have missed some prior uses that might cause exceptions to be thrown. git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@890 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
noinst_LIBRARIES = libsub_utils.a
|
||||
|
||||
libsub_utils_a_SOURCES = \
|
||||
command_variable.cc \
|
||||
command_variable.h \
|
||||
directory.cc \
|
||||
directory.h \
|
||||
list_focus.h \
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
// rTorrent - BitTorrent client
|
||||
// Copyright (C) 2006, Jari Sundell
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation; either version 2 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations
|
||||
// including the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for
|
||||
// all of the code used other than OpenSSL. If you modify file(s)
|
||||
// with this exception, you may extend this exception to your version
|
||||
// of the file(s), but you are not obligated to do so. If you do not
|
||||
// wish to do so, delete this exception statement from your version.
|
||||
// If you delete this exception statement from all source files in the
|
||||
// program, then also delete it here.
|
||||
//
|
||||
// Contact: Jari Sundell <jaris@ifi.uio.no>
|
||||
//
|
||||
// Skomakerveien 33
|
||||
// 3185 Skoppum, NORWAY
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "command_variable.h"
|
||||
|
||||
namespace utils {
|
||||
|
||||
const torrent::Object&
|
||||
CommandVariable::set_bool(Variable* rawVariable, const torrent::Object& rawArgs) {
|
||||
CommandVariable* variable = static_cast<CommandVariable*>(rawVariable);
|
||||
|
||||
const torrent::Object& arg = to_single_argument(rawArgs);
|
||||
|
||||
switch (arg.type()) {
|
||||
case torrent::Object::TYPE_VALUE:
|
||||
variable->m_variable = arg.as_value() ? (int64_t)1 : (int64_t)0;
|
||||
break;
|
||||
|
||||
case torrent::Object::TYPE_STRING:
|
||||
// Move the checks into some is_true, is_false think in Variable.
|
||||
if (arg.as_string() == "yes" || arg.as_string() == "true")
|
||||
variable->m_variable = (int64_t)1;
|
||||
|
||||
else if (arg.as_string() == "no" || arg.as_string() == "false")
|
||||
variable->m_variable = (int64_t)0;
|
||||
|
||||
else
|
||||
throw torrent::input_error("String does not parse as a boolean.");
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
throw torrent::input_error("Input is not a boolean.");
|
||||
}
|
||||
|
||||
return variable->m_variable;
|
||||
}
|
||||
|
||||
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&
|
||||
CommandVariable::set_value(Variable* rawVariable, const torrent::Object& rawArgs) {
|
||||
CommandVariable* variable = static_cast<CommandVariable*>(rawVariable);
|
||||
|
||||
const torrent::Object& arg = to_single_argument(rawArgs);
|
||||
|
||||
switch (arg.type()) {
|
||||
case torrent::Object::TYPE_NONE:
|
||||
variable->m_variable = (int64_t)0;
|
||||
break;
|
||||
|
||||
case torrent::Object::TYPE_VALUE:
|
||||
variable->m_variable = arg;
|
||||
break;
|
||||
|
||||
case torrent::Object::TYPE_STRING:
|
||||
int64_t value;
|
||||
string_to_value_unit(arg.as_string().c_str(), &value, 0, 1);
|
||||
|
||||
variable->m_variable = value;
|
||||
break;
|
||||
|
||||
default:
|
||||
throw torrent::input_error("VariableValue unsupported type restriction.");
|
||||
}
|
||||
|
||||
return variable->m_variable;
|
||||
}
|
||||
|
||||
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&
|
||||
CommandVariable::set_string(Variable* rawVariable, const torrent::Object& rawArgs) {
|
||||
CommandVariable* variable = static_cast<CommandVariable*>(rawVariable);
|
||||
|
||||
const torrent::Object& arg = to_single_argument(rawArgs);
|
||||
|
||||
switch (arg.type()) {
|
||||
case torrent::Object::TYPE_NONE:
|
||||
variable->m_variable = std::string("");
|
||||
break;
|
||||
|
||||
// case torrent::Object::TYPE_VALUE:
|
||||
// variable->m_variable = arg;
|
||||
// break;
|
||||
|
||||
case torrent::Object::TYPE_STRING:
|
||||
variable->m_variable = arg;
|
||||
break;
|
||||
|
||||
default:
|
||||
throw torrent::input_error("Not a string.");
|
||||
}
|
||||
|
||||
return variable->m_variable;
|
||||
}
|
||||
|
||||
const torrent::Object&
|
||||
CommandVariable::get_string(Variable* rawVariable, const torrent::Object& args) {
|
||||
CommandVariable* variable = static_cast<CommandVariable*>(rawVariable);
|
||||
|
||||
return variable->m_variable;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
// rTorrent - BitTorrent client
|
||||
// Copyright (C) 2006, Jari Sundell
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation; either version 2 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations
|
||||
// including the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for
|
||||
// all of the code used other than OpenSSL. If you modify file(s)
|
||||
// with this exception, you may extend this exception to your version
|
||||
// of the file(s), but you are not obligated to do so. If you do not
|
||||
// wish to do so, delete this exception statement from your version.
|
||||
// If you delete this exception statement from all source files in the
|
||||
// program, then also delete it here.
|
||||
//
|
||||
// Contact: Jari Sundell <jaris@ifi.uio.no>
|
||||
//
|
||||
// Skomakerveien 33
|
||||
// 3185 Skoppum, NORWAY
|
||||
|
||||
#ifndef RTORRENT_UTILS_COMMAND_VARIABLES_H
|
||||
#define RTORRENT_UTILS_COMMAND_VARIABLES_H
|
||||
|
||||
#include <string>
|
||||
#include <limits>
|
||||
#include <inttypes.h>
|
||||
#include <torrent/object.h>
|
||||
|
||||
#include "variable.h"
|
||||
|
||||
namespace utils {
|
||||
|
||||
class CommandVariable : public Variable {
|
||||
public:
|
||||
CommandVariable(const torrent::Object& v = torrent::Object()) : m_variable(v) {}
|
||||
|
||||
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_string(Variable* rawVariable, const torrent::Object& args);
|
||||
static const torrent::Object& get_string(Variable* rawVariable, const torrent::Object& args);
|
||||
|
||||
private:
|
||||
torrent::Object m_variable;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -47,11 +47,13 @@ const torrent::Object Variable::m_emptyObject;
|
||||
// Consider throwing an exception.
|
||||
const torrent::Object&
|
||||
Variable::get() {
|
||||
return m_emptyObject;
|
||||
// return m_emptyObject;
|
||||
throw torrent::internal_error("Variable::get() called.");
|
||||
}
|
||||
|
||||
void
|
||||
Variable::set(const torrent::Object& arg) {
|
||||
throw torrent::internal_error("Variable::set() called.");
|
||||
}
|
||||
|
||||
const torrent::Object&
|
||||
|
||||
@@ -66,6 +66,8 @@ public:
|
||||
static const char* string_to_value_unit(const char* pos, value_type* value, int base, int unit);
|
||||
static bool string_to_value_unit_nothrow(const char* pos, value_type* value, int base, int unit);
|
||||
|
||||
static const torrent::Object& to_single_argument(const torrent::Object& args);
|
||||
|
||||
// Temporary hack, until torrent::Object is extended to allow
|
||||
// references so we can return a copy, not a const reference.
|
||||
static const torrent::Object m_emptyObject;
|
||||
@@ -75,6 +77,14 @@ protected:
|
||||
void operator = (const Variable&);
|
||||
};
|
||||
|
||||
inline const torrent::Object&
|
||||
Variable::to_single_argument(const torrent::Object& args) {
|
||||
if (args.type() == torrent::Object::TYPE_LIST && args.as_list().size() == 1)
|
||||
return args.as_list().front();
|
||||
else
|
||||
return args;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -51,18 +51,24 @@
|
||||
|
||||
namespace utils {
|
||||
|
||||
struct variable_map_get_ptr : std::unary_function<VariableMap::value_type&, Variable*> {
|
||||
Variable* operator () (VariableMap::value_type& value) { return value.second.m_variable; }
|
||||
};
|
||||
|
||||
VariableMap::~VariableMap() {
|
||||
std::for_each(base_type::begin(), base_type::end(), rak::on(rak::mem_ref(&value_type::second), rak::call_delete<Variable>()));
|
||||
for (iterator itr = base_type::begin(), last = base_type::end(); itr != last; itr++)
|
||||
if (!(itr->second.m_flags & flag_dont_delete))
|
||||
delete itr->second.m_variable;
|
||||
}
|
||||
|
||||
void
|
||||
VariableMap::insert(key_type key, Variable* v) {
|
||||
VariableMap::insert(key_type key, Variable* variable, generic_slot genericSlot, int flags) {
|
||||
iterator itr = base_type::find(key);
|
||||
|
||||
if (itr != base_type::end())
|
||||
throw torrent::internal_error("VariableMap::insert(...) tried to insert an already existing key.");
|
||||
|
||||
base_type::insert(itr, value_type(key, v));
|
||||
base_type::insert(itr, value_type(key, variable_map_data_type(variable, genericSlot, NULL, flags)));
|
||||
}
|
||||
|
||||
const VariableMap::mapped_type&
|
||||
@@ -72,7 +78,10 @@ VariableMap::get(key_type key) const {
|
||||
if (itr == base_type::end())
|
||||
throw torrent::input_error("Variable \"" + std::string(key) + "\" does not exist.");
|
||||
|
||||
return itr->second->get();
|
||||
if (itr->second.m_genericSlot != NULL)
|
||||
return itr->second.m_genericSlot(itr->second.m_variable, torrent::Object());
|
||||
|
||||
return itr->second.m_variable->get();
|
||||
}
|
||||
|
||||
const VariableMap::mapped_type&
|
||||
@@ -82,7 +91,13 @@ VariableMap::get_d(core::Download* download, key_type key) const {
|
||||
if (itr == base_type::end())
|
||||
throw torrent::input_error("Variable \"" + std::string(key) + "\" does not exist.");
|
||||
|
||||
return itr->second->get_d(download);
|
||||
if (itr->second.m_downloadSlot != NULL)
|
||||
return itr->second.m_downloadSlot(itr->second.m_variable, download, torrent::Object());
|
||||
|
||||
if (itr->second.m_genericSlot != NULL)
|
||||
return itr->second.m_genericSlot(itr->second.m_variable, torrent::Object());
|
||||
|
||||
return itr->second.m_variable->get_d(download);
|
||||
}
|
||||
|
||||
void
|
||||
@@ -94,7 +109,12 @@ VariableMap::set(key_type key, const mapped_type& arg) {
|
||||
if (itr == base_type::end())
|
||||
throw torrent::input_error("Variable \"" + std::string(key) + "\" does not exist.");
|
||||
|
||||
itr->second->set(arg);
|
||||
if (itr->second.m_genericSlot != NULL) {
|
||||
itr->second.m_genericSlot(itr->second.m_variable, arg);
|
||||
return;
|
||||
}
|
||||
|
||||
itr->second.m_variable->set(arg);
|
||||
}
|
||||
|
||||
void
|
||||
@@ -106,7 +126,17 @@ VariableMap::set_d(core::Download* download, key_type key, const mapped_type& ar
|
||||
if (itr == base_type::end())
|
||||
throw torrent::input_error("Variable \"" + std::string(key) + "\" does not exist.");
|
||||
|
||||
itr->second->set_d(download, arg);
|
||||
if (itr->second.m_downloadSlot != NULL) {
|
||||
itr->second.m_downloadSlot(itr->second.m_variable, download, arg);
|
||||
return;
|
||||
}
|
||||
|
||||
if (itr->second.m_genericSlot != NULL) {
|
||||
itr->second.m_genericSlot(itr->second.m_variable, arg);
|
||||
return;
|
||||
}
|
||||
|
||||
itr->second.m_variable->set_d(download, arg);
|
||||
}
|
||||
|
||||
struct variable_map_is_space : std::unary_function<char, bool> {
|
||||
@@ -236,13 +266,42 @@ VariableMap::process_file(key_type path) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const VariableMap::mapped_type&
|
||||
VariableMap::call_command(key_type key, const mapped_type& arg) {
|
||||
const_iterator itr = base_type::find(key);
|
||||
|
||||
if (itr == base_type::end())
|
||||
throw torrent::input_error("Variable \"" + std::string(key) + "\" does not exist.");
|
||||
|
||||
if (itr->second.m_genericSlot == NULL)
|
||||
throw torrent::input_error("Variable does not have a generic slot.");
|
||||
|
||||
return itr->second.m_genericSlot(itr->second.m_variable, arg);
|
||||
}
|
||||
|
||||
const VariableMap::mapped_type&
|
||||
VariableMap::call_command_get(key_type key, const mapped_type& arg) {
|
||||
const_iterator itr = base_type::find(key);
|
||||
|
||||
if (itr == base_type::end())
|
||||
throw torrent::input_error("Variable \"" + std::string(key) + "\" does not exist.");
|
||||
|
||||
if (itr->second.m_genericSlot != NULL)
|
||||
return itr->second.m_genericSlot(itr->second.m_variable, arg);
|
||||
|
||||
return get(key);
|
||||
}
|
||||
|
||||
const VariableMap::mapped_type&
|
||||
VariableMap::call_command_set(key_type key, const mapped_type& arg) {
|
||||
const_iterator itr = base_type::find(key);
|
||||
|
||||
if (itr == base_type::end())
|
||||
throw torrent::input_error("Variable \"" + std::string(key) + "\" does not exist.");
|
||||
|
||||
if (itr->second.m_genericSlot != NULL)
|
||||
return itr->second.m_genericSlot(itr->second.m_variable, arg);
|
||||
|
||||
set(key, arg);
|
||||
|
||||
return Variable::m_emptyObject;
|
||||
|
||||
@@ -59,20 +59,42 @@ struct variable_map_comp : public std::binary_function<const char*, const char*,
|
||||
|
||||
class Variable;
|
||||
|
||||
class VariableMap : public std::map<const char*, Variable*, variable_map_comp> {
|
||||
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&);
|
||||
|
||||
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) {}
|
||||
|
||||
Variable* m_variable;
|
||||
generic_slot m_genericSlot;
|
||||
download_slot m_downloadSlot;
|
||||
|
||||
int m_flags;
|
||||
};
|
||||
|
||||
class VariableMap : public std::map<const char*, variable_map_data_type, variable_map_comp> {
|
||||
public:
|
||||
typedef std::map<const char*, Variable*, variable_map_comp> base_type;
|
||||
typedef std::map<const char*, variable_map_data_type, variable_map_comp> base_type;
|
||||
|
||||
typedef variable_map_data_type::generic_slot generic_slot;
|
||||
typedef variable_map_data_type::download_slot download_slot;
|
||||
|
||||
typedef torrent::Object mapped_type;
|
||||
typedef mapped_type::value_type mapped_value_type;
|
||||
|
||||
using base_type::iterator;
|
||||
using base_type::key_type;
|
||||
using base_type::value_type;
|
||||
|
||||
static const int max_size_key = 128;
|
||||
static const int max_size_opt = 1024;
|
||||
static const int max_size_line = max_size_key + max_size_opt + 64;
|
||||
|
||||
using base_type::iterator;
|
||||
using base_type::key_type;
|
||||
using base_type::value_type;
|
||||
static const int flag_dont_delete = 0x1;
|
||||
|
||||
VariableMap() {}
|
||||
~VariableMap();
|
||||
@@ -80,7 +102,9 @@ public:
|
||||
bool has(const char* key) const { return base_type::find(key) != base_type::end(); }
|
||||
bool has(const std::string& key) const { return has(key.c_str()); }
|
||||
|
||||
void insert(key_type key, Variable* v);
|
||||
// Allow NULL slot as a temporary compatibility hack.
|
||||
|
||||
void insert(key_type key, Variable* variable, generic_slot genericSlot = NULL, int flags = 0);
|
||||
|
||||
// Consider uninlining the helper functions.
|
||||
|
||||
@@ -119,6 +143,8 @@ 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_get(key_type key, const mapped_type& arg);
|
||||
const mapped_type& call_command_set(key_type key, const mapped_type& arg);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user