mirror of
https://github.com/rakshasa/rtorrent.git
synced 2026-08-16 07:02:30 +00:00
Convert CommandMap to use std::string instead of char*
This allows it to own its keys and clean them up automatically on destruction.
This commit is contained in:
+7
-40
@@ -36,7 +36,6 @@
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <vector>
|
||||
#include <torrent/exceptions.h>
|
||||
#include <torrent/object.h>
|
||||
#include <torrent/data/file_list_iterator.h>
|
||||
@@ -57,23 +56,8 @@ namespace rpc {
|
||||
|
||||
command_base::stack_type command_base::current_stack;
|
||||
|
||||
CommandMap::~CommandMap() {
|
||||
std::vector<const char*> keys;
|
||||
|
||||
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;
|
||||
|
||||
if (itr->second.m_flags & flag_delete_key)
|
||||
keys.push_back(itr->first);
|
||||
}
|
||||
|
||||
for (std::vector<const char*>::iterator itr = keys.begin(), last = keys.end(); itr != last; itr++)
|
||||
delete [] *itr;
|
||||
}
|
||||
|
||||
CommandMap::iterator
|
||||
CommandMap::insert(key_type key, int flags, const char* parm, const char* doc) {
|
||||
CommandMap::insert(const key_type& key, int flags, const char* parm, const char* doc) {
|
||||
iterator itr = base_type::find(key);
|
||||
|
||||
if (itr != base_type::end())
|
||||
@@ -81,25 +65,11 @@ CommandMap::insert(key_type key, int flags, const char* parm, const char* doc) {
|
||||
|
||||
// TODO: This is not honoring the public_xmlrpc flags!!!
|
||||
if (rpc::xmlrpc.is_valid() && (flags & flag_public_xmlrpc))
|
||||
// if (rpc::xmlrpc.is_valid())
|
||||
rpc::xmlrpc.insert_command(key, parm, doc);
|
||||
rpc::xmlrpc.insert_command(key.c_str(), parm, doc);
|
||||
|
||||
return base_type::insert(itr, value_type(key, command_map_data_type(flags, parm, doc)));
|
||||
}
|
||||
|
||||
// void
|
||||
// CommandMap::insert(key_type key, const command_map_data_type src) {
|
||||
// iterator itr = base_type::find(key);
|
||||
|
||||
// if (itr != base_type::end())
|
||||
// throw torrent::internal_error("CommandMap::insert(...) tried to insert an already existing key.");
|
||||
|
||||
// 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_anySlot = src.m_anySlot;
|
||||
// }
|
||||
|
||||
void
|
||||
CommandMap::erase(iterator itr) {
|
||||
if (itr == end())
|
||||
@@ -112,14 +82,11 @@ CommandMap::erase(iterator itr) {
|
||||
// if (!(itr->second.m_flags & flag_dont_delete))
|
||||
// delete itr->second.m_variable;
|
||||
|
||||
const char* key = itr->second.m_flags & flag_delete_key ? itr->first : NULL;
|
||||
|
||||
base_type::erase(itr);
|
||||
delete [] key;
|
||||
}
|
||||
|
||||
void
|
||||
CommandMap::create_redirect(key_type key_new, key_type key_dest, int flags) {
|
||||
CommandMap::create_redirect(const key_type& key_new, const key_type& key_dest, int flags) {
|
||||
iterator new_itr = base_type::find(key_new);
|
||||
iterator dest_itr = base_type::find(key_dest);
|
||||
|
||||
@@ -139,7 +106,7 @@ CommandMap::create_redirect(key_type key_new, key_type key_dest, int flags) {
|
||||
|
||||
// TODO: This is not honoring the public_xmlrpc flags!!!
|
||||
if (rpc::xmlrpc.is_valid() && (flags & flag_public_xmlrpc))
|
||||
rpc::xmlrpc.insert_command(key_new, dest_itr->second.m_parm, dest_itr->second.m_doc);
|
||||
rpc::xmlrpc.insert_command(key_new.c_str(), dest_itr->second.m_parm, dest_itr->second.m_doc);
|
||||
|
||||
iterator itr = base_type::insert(base_type::end(),
|
||||
value_type(key_new, command_map_data_type(flags,
|
||||
@@ -152,7 +119,7 @@ CommandMap::create_redirect(key_type key_new, key_type key_dest, int flags) {
|
||||
}
|
||||
|
||||
const CommandMap::mapped_type
|
||||
CommandMap::call_catch(key_type key, target_type target, const mapped_type& args, const char* err) {
|
||||
CommandMap::call_catch(const key_type& key, const target_type& target, const mapped_type& args, const char* err) {
|
||||
try {
|
||||
return call_command(key, args, target);
|
||||
} catch (torrent::input_error& e) {
|
||||
@@ -162,7 +129,7 @@ CommandMap::call_catch(key_type key, target_type target, const mapped_type& args
|
||||
}
|
||||
|
||||
const CommandMap::mapped_type
|
||||
CommandMap::call_command(key_type key, const mapped_type& arg, target_type target) {
|
||||
CommandMap::call_command(const key_type& key, const mapped_type& arg, const target_type& target) {
|
||||
iterator itr = base_type::find(key);
|
||||
|
||||
if (itr == base_type::end())
|
||||
@@ -172,7 +139,7 @@ CommandMap::call_command(key_type key, const mapped_type& arg, target_type targe
|
||||
}
|
||||
|
||||
const CommandMap::mapped_type
|
||||
CommandMap::call_command(iterator itr, const mapped_type& arg, target_type target) {
|
||||
CommandMap::call_command(iterator itr, const mapped_type& arg, const target_type& target) {
|
||||
return itr->second.m_anySlot(&itr->second.m_variable, target, arg);
|
||||
}
|
||||
|
||||
|
||||
+18
-24
@@ -46,10 +46,6 @@
|
||||
|
||||
namespace rpc {
|
||||
|
||||
struct command_map_comp : public std::binary_function<const char*, const char*, bool> {
|
||||
bool operator () (const char* arg1, const char* arg2) const { return std::strcmp(arg1, arg2) < 0; }
|
||||
};
|
||||
|
||||
struct command_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
|
||||
@@ -73,9 +69,9 @@ struct command_map_data_type {
|
||||
const char* m_doc;
|
||||
};
|
||||
|
||||
class CommandMap : public std::map<const char*, command_map_data_type, command_map_comp> {
|
||||
class CommandMap : public std::map<std::string, command_map_data_type> {
|
||||
public:
|
||||
typedef std::map<const char*, command_map_data_type, command_map_comp> base_type;
|
||||
typedef std::map<std::string, command_map_data_type> base_type;
|
||||
|
||||
typedef torrent::Object mapped_type;
|
||||
typedef mapped_type::value_type mapped_value_type;
|
||||
@@ -101,39 +97,37 @@ public:
|
||||
static const int flag_tracker_target = 0x400;
|
||||
|
||||
CommandMap() {}
|
||||
~CommandMap();
|
||||
~CommandMap() {}
|
||||
|
||||
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()); }
|
||||
bool has(const std::string& key) const { return base_type::find(key) != base_type::end(); }
|
||||
|
||||
bool is_modifiable(const_iterator itr) { return itr != end() && (itr->second.m_flags & flag_modifiable); }
|
||||
|
||||
iterator insert(key_type key, int flags, const char* parm, const char* doc);
|
||||
iterator insert(const key_type& key, int flags, const char* parm, const char* doc);
|
||||
|
||||
template <typename T, typename Slot>
|
||||
void
|
||||
insert_slot(key_type key, Slot variable, command_base::any_slot targetSlot, int flags, const char* parm, const char* doc) {
|
||||
insert_slot(const key_type& key, Slot variable, command_base::any_slot target_slot, int flags, const char* parm, const char* doc) {
|
||||
iterator itr = insert(key, flags, parm, doc);
|
||||
itr->second.m_variable.set_function<T>(variable);
|
||||
itr->second.m_anySlot = targetSlot;
|
||||
itr->second.m_anySlot = target_slot;
|
||||
}
|
||||
|
||||
// void insert(key_type key, const command_map_data_type src);
|
||||
void erase(iterator itr);
|
||||
|
||||
void create_redirect(key_type key_new, key_type key_dest, int flags);
|
||||
void create_redirect(const key_type& key_new, const key_type& key_dest, int flags);
|
||||
|
||||
const mapped_type call(key_type key, const mapped_type& args = mapped_type());
|
||||
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(const key_type& key, const mapped_type& args = mapped_type());
|
||||
const mapped_type call(const key_type& key, const target_type& target, const mapped_type& args = mapped_type()) { return call_command(key, args, target); }
|
||||
const mapped_type call_catch(const key_type& key, const 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_base::target_generic, NULL));
|
||||
const mapped_type call_command (iterator itr, const mapped_type& arg, target_type target = target_type((int)command_base::target_generic, NULL));
|
||||
const mapped_type call_command (const key_type& key, const mapped_type& arg, const target_type& target = target_type((int)command_base::target_generic, NULL));
|
||||
const mapped_type call_command (iterator itr, const mapped_type& arg, const target_type& target = target_type((int)command_base::target_generic, NULL));
|
||||
|
||||
const mapped_type call_command_d(key_type key, core::Download* download, const mapped_type& arg) { return call_command(key, arg, target_type((int)command_base::target_download, download)); }
|
||||
const mapped_type call_command_p(key_type key, torrent::Peer* peer, const mapped_type& arg) { return call_command(key, arg, target_type((int)command_base::target_peer, peer)); }
|
||||
const mapped_type call_command_t(key_type key, torrent::Tracker* tracker, const mapped_type& arg) { return call_command(key, arg, target_type((int)command_base::target_tracker, tracker)); }
|
||||
const mapped_type call_command_f(key_type key, torrent::File* file, const mapped_type& arg) { return call_command(key, arg, target_type((int)command_base::target_file, file)); }
|
||||
const mapped_type call_command_d(const key_type& key, core::Download* download, const mapped_type& arg) { return call_command(key, arg, target_type((int)command_base::target_download, download)); }
|
||||
const mapped_type call_command_p(const key_type& key, torrent::Peer* peer, const mapped_type& arg) { return call_command(key, arg, target_type((int)command_base::target_peer, peer)); }
|
||||
const mapped_type call_command_t(const key_type& key, torrent::Tracker* tracker, const mapped_type& arg) { return call_command(key, arg, target_type((int)command_base::target_tracker, tracker)); }
|
||||
const mapped_type call_command_f(const key_type& key, torrent::File* file, const mapped_type& arg) { return call_command(key, arg, target_type((int)command_base::target_file, file)); }
|
||||
|
||||
private:
|
||||
CommandMap(const CommandMap&);
|
||||
@@ -175,7 +169,7 @@ create_object_list(const torrent::Object& o1, const torrent::Object& o2, const t
|
||||
}
|
||||
|
||||
inline const CommandMap::mapped_type
|
||||
CommandMap::call(key_type key, const mapped_type& args) {
|
||||
CommandMap::call(const key_type& key, const mapped_type& args) {
|
||||
return call_command(key, args, make_target());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user