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:
kannibalox
2024-10-27 11:07:41 -04:00
committed by Jari Sundell
parent ef937863fd
commit 34de2b4ac9
5 changed files with 47 additions and 106 deletions
+18 -24
View File
@@ -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());
}