* More work on rpc::object_storage and moving simple functions over to it.

git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@1165 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2010-04-28 15:19:18 +00:00
parent 9362453391
commit b530d35d94
22 changed files with 450 additions and 421 deletions
-4
View File
@@ -13,10 +13,6 @@ libsub_rpc_a_SOURCES = \
command_scheduler_item.h \
command_new_slot.cc \
command_new_slot.h \
command_variable.cc \
command_variable.h \
command_new_slot.cc \
command_new_slot.h \
exec_file.cc \
exec_file.h \
object_storage.cc \
+5
View File
@@ -69,6 +69,11 @@ public:
template <typename T>
void set_function(T s, int value = command_base_is_valid<T>::value) { _pod<T>() = s; }
template <command_base_call_type T>
void set_function_2(typename command_base_is_type<T>::type s, int value = command_base_is_valid<typename command_base_is_type<T>::type>::value) {
_pod<typename command_base_is_type<T>::type>() = s;
}
// The std::function object in GCC is castable between types with a
// pointer to a struct of ctor/dtor/calls for non-POD slots. As such
// it should be safe to cast between different std::function
-169
View File
@@ -1,169 +0,0 @@
// rTorrent - BitTorrent client
// Copyright (C) 2005-2007, 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 "parse.h"
#include "command_variable.h"
namespace rpc {
const torrent::Object
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);
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 Command.
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(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, target_type target, const torrent::Object& rawArgs) {
CommandVariable* variable = static_cast<CommandVariable*>(rawCommand);
const torrent::Object& arg = convert_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;
parse_whole_value(arg.as_string().c_str(), &value, 0, 1);
variable->m_variable = value;
break;
default:
throw torrent::input_error("CommandValue unsupported type restriction.");
}
return variable->m_variable;
}
const torrent::Object
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, target_type target, const torrent::Object& rawArgs) {
CommandVariable* variable = static_cast<CommandVariable*>(rawCommand);
const torrent::Object& arg = convert_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(Command* rawCommand, target_type target, const torrent::Object& args) {
CommandVariable* variable = static_cast<CommandVariable*>(rawCommand);
return variable->m_variable;
}
//
// ObjectPtr
//
const torrent::Object
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, target_type target, const torrent::Object& args) {
CommandObjectPtr* command = static_cast<CommandObjectPtr*>(rawCommand);
return *command->m_object;
}
}
-94
View File
@@ -1,94 +0,0 @@
// rTorrent - BitTorrent client
// Copyright (C) 2005-2007, 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_RPC_COMMAND_VARIABLES_H
#define RTORRENT_RPC_COMMAND_VARIABLES_H
#include <string>
#include <limits>
#include <inttypes.h>
#include <torrent/object.h>
#include "command.h"
namespace rpc {
class CommandVariable : public Command {
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; }
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, 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, 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;
};
class CommandObjectPtr : public Command {
public:
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, 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, 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, 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, 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;
};
}
#endif
+66 -14
View File
@@ -39,6 +39,7 @@
#include "object_storage.h"
#include "parse.h"
#include "command_function.h"
namespace rpc {
@@ -54,14 +55,21 @@ object_storage::find_local(const torrent::raw_string& key) {
}
object_storage::iterator
object_storage::insert(const char* key_data, uint32_t key_size, const torrent::Object& object, unsigned int flags) {
object_storage::insert(const char* key_data, uint32_t key_size, const torrent::Object& rawObject, unsigned int flags) {
if (std::find(key_data, key_data + key_size, '\0') != key_data + key_size)
throw torrent::input_error("Found nul-char in string.");
// Check for size > key_size.
// Check for empty string.
// Ensure the object type is correct.
torrent::Object object;
switch (flags & mask_type) {
case flag_bool_type: object = !!convert_to_value(rawObject); break;
case flag_value_type: object = convert_to_value(rawObject); break;
case flag_string_type: object = convert_to_string(rawObject); break;
case flag_function_type: object = convert_to_string(rawObject); break;
}
if (!(flags & mask_type))
throw torrent::input_error("No type flags set when calling object_storage::insert.");
@@ -71,8 +79,8 @@ object_storage::insert(const char* key_data, uint32_t key_size, const torrent::O
if (!result.second)
throw torrent::input_error("Key already exists in object_storage.");
result.first->second.object = object;
result.first->second.flags = flags;
result.first->second.object = object;
return result.first;
}
@@ -87,23 +95,67 @@ object_storage::get(const torrent::raw_string& key) {
return itr->second.object;
}
// const torrent::Object&
// object_storage::set(const torrent::raw_string& key, const torrent::Object& object) {
// local_iterator itr = find_local(key);
// if (itr == end(bucket_count()) || (itr->second.flags & mask_type) != flag_generic_type)
// throw torrent::input_error("Key not found or wrong type.");
// return itr->second.object = object;
// }
const torrent::Object&
object_storage::set(const torrent::raw_string& key, const torrent::Object& object) {
object_storage::set_bool(const torrent::raw_string& key, int64_t object) {
local_iterator itr = find_local(key);
if (itr == end(bucket_count()))
throw torrent::input_error("Key not found.");
if (itr == end(bucket_count()) || (itr->second.flags & mask_type) != flag_bool_type)
throw torrent::input_error("Key not found or wrong type.");
// Redo this...
return itr->second.object = !!object;
}
switch (itr->second.flags & mask_type) {
case flag_generic_type: itr->second.object = object; break;
case flag_value_type: itr->second.object = convert_to_value(object); break;
case flag_string_type: itr->second.object = convert_to_string(object); break;
default: throw torrent::internal_error("object_storage::set: Type not set.");
}
return itr->second.object;
const torrent::Object&
object_storage::set_value(const torrent::raw_string& key, int64_t object) {
local_iterator itr = find_local(key);
if (itr == end(bucket_count()) || (itr->second.flags & mask_type) != flag_value_type)
throw torrent::input_error("Key not found or wrong type.");
return itr->second.object = object;
}
const torrent::Object&
object_storage::set_string(const torrent::raw_string& key, const std::string& object) {
local_iterator itr = find_local(key);
if (itr == end(bucket_count()) || (itr->second.flags & mask_type) != flag_string_type)
throw torrent::input_error("Key not found or wrong type.");
return itr->second.object = object;
}
const torrent::Object&
object_storage::set_function(const torrent::raw_string& key, const std::string& object) {
local_iterator itr = find_local(key);
if (itr == end(bucket_count()) || (itr->second.flags & mask_type) != flag_function_type)
throw torrent::input_error("Key not found or wrong type.");
return itr->second.object = object;
}
torrent::Object
object_storage::call_function(const torrent::raw_string& key, target_type target, const torrent::Object& object) {
local_iterator itr = find_local(key);
if (itr == end(bucket_count()) || !itr->second.object.is_string())
throw torrent::input_error("Key not found or wrong type.");
return command_function_call_str(itr->second.object.as_string(), target, object);
}
}
+41 -10
View File
@@ -45,6 +45,8 @@
#include <tr1/unordered_map>
#include <torrent/object.h>
#include "command.h"
namespace rpc {
// The key size should be such that the value type size which includes
@@ -150,31 +152,55 @@ public:
using base_type::find;
using base_type::erase;
static const unsigned int flag_generic_type = 0x1;
static const unsigned int flag_value_type = 0x2;
static const unsigned int flag_string_type = 0x4;
static const unsigned int flag_last_type = 0x8;
static const unsigned int flag_generic_type = 0x1;
static const unsigned int flag_bool_type = 0x2;
static const unsigned int flag_value_type = 0x3;
static const unsigned int flag_string_type = 0x4;
static const unsigned int flag_function_type = 0x5;
static const unsigned int mask_type = flag_last_type - 1;
static const unsigned int mask_type = 0xf;
static const unsigned int flag_constant = 0x10;
static const unsigned int flag_static = 0x20;
static const unsigned int flag_private = 0x40;
static const size_t key_size = key_type::max_size;
local_iterator find_local(const torrent::raw_string& key);
iterator insert(const char* key_data, uint32_t key_size, const torrent::Object& object, unsigned int flags);
iterator insert_c_str(const char* key, const torrent::Object& object, unsigned int flags) { return insert(key, std::strlen(key), object, flags); }
iterator insert(const char* key, const torrent::Object& object, unsigned int flags);
iterator insert(const torrent::raw_string& key, const torrent::Object& object, unsigned int flags);
iterator insert_string(const std::string& key, const torrent::Object& object, unsigned int flags);
iterator insert_str(const std::string& key, const torrent::Object& object, unsigned int flags);
// Access functions that throw on error.
const torrent::Object& get(const torrent::raw_string& key);
const torrent::Object& get_c_str(const char* str) { return get(torrent::raw_string(str, std::strlen(str))); }
const torrent::Object& get_c_str(const char* str) { return get(torrent::raw_string(str, std::strlen(str))); }
const torrent::Object& get_str(const std::string& str) { return get(torrent::raw_string(str.data(), str.size())); }
const torrent::Object& set(const torrent::raw_string& key, const torrent::Object& object);
// const torrent::Object& set(const torrent::raw_string& key, const torrent::Object& object);
// const torrent::Object& set_c_str(const char* str, const torrent::Object& object) { return set(torrent::raw_string(str, std::strlen(str)), object); }
const torrent::Object& set_c_str(const char* str, const torrent::Object& object) { return set(torrent::raw_string(str, std::strlen(str)), object); }
const torrent::Object& set_bool(const torrent::raw_string& key, int64_t object);
const torrent::Object& set_c_str_bool(const char* str, int64_t object) { return set_bool(torrent::raw_string::from_c_str(str), object); }
const torrent::Object& set_str_bool(const std::string& str, int64_t object) { return set_bool(torrent::raw_string::from_string(str), object); }
const torrent::Object& set_value(const torrent::raw_string& key, int64_t object);
const torrent::Object& set_c_str_value(const char* str, int64_t object) { return set_value(torrent::raw_string::from_string(str), object); }
const torrent::Object& set_str_value(const std::string& str, int64_t object) { return set_value(torrent::raw_string::from_string(str), object); }
const torrent::Object& set_string(const torrent::raw_string& key, const std::string& object);
const torrent::Object& set_c_str_string(const char* str, const std::string& object) { return set_string(torrent::raw_string::from_c_str(str), object); }
const torrent::Object& set_str_string(const std::string& str, const std::string& object) { return set_string(torrent::raw_string::from_string(str), object); }
torrent::Object call_function(const torrent::raw_string& key, target_type target, const torrent::Object& object);
torrent::Object call_function_str(const std::string& key, target_type target, const torrent::Object& object);
const torrent::Object& set_function(const torrent::raw_string& key, const std::string& object);
const torrent::Object& set_str_function(const std::string& str, const std::string& object) { return set_function(torrent::raw_string::from_string(str), object); }
};
//
@@ -227,10 +253,15 @@ object_storage::insert(const torrent::raw_string& key, const torrent::Object& ob
}
inline object_storage::iterator
object_storage::insert_string(const std::string& key, const torrent::Object& object, unsigned int flags) {
object_storage::insert_str(const std::string& key, const torrent::Object& object, unsigned int flags) {
return insert(key.data(), key.size(), object, flags);
}
inline torrent::Object
object_storage::call_function_str(const std::string& key, target_type target, const torrent::Object& object) {
return call_function(torrent::raw_string::from_string(key), target, object);
}
}
#endif