#ifndef RTORRENT_UTILS_COMMAND_HELPERS_H #define RTORRENT_UTILS_COMMAND_HELPERS_H #include "rpc/command.h" #include "rpc/parse_commands.h" #include "rpc/object_storage.h" void initialize_commands(); // // Aliases with CMD_* for the below // #define CMD_ANY(key, slot) CMD2_ANY(key, slot) #define CMD_ANY_P(key, slot) CMD2_ANY_P(key, slot) #define CMD_REDIRECT(key, slot) CMD2_REDIRECT(key, slot) #define CMD_REDIRECT_NO_EXPORT(key, slot) CMD2_REDIRECT_NO_EXPORT(key, slot) #define CMD_ANY_STRING(key, slot) CMD2_ANY_STRING(key, slot) #define CMD_ANY_STRING_V(key, slot) CMD2_ANY_STRING_V(key, slot) #define CMD_ANY_LIST(key, slot) CMD2_ANY_LIST(key, slot) #define CMD_VAR_VALUE(key, value) CMD2_VAR_VALUE(key, value) #define CMD_VAR_BOOL(key, value) CMD2_VAR_BOOL(key, value) #define CMD_VAR_STRING(key, value) CMD2_VAR_STRING(key, value) #define CMD_VAR_C_STRING(key, value) CMD2_VAR_C_STRING(key, value) #define CMD_VAR_LIST(key) CMD2_VAR_LIST(key) #define CMD_ANY_V(key, slot) CMD2_ANY_V(key, slot) #define CMD_ANY_VALUE_V(key, slot) CMD2_ANY_VALUE_V(key, slot) // // New std::function based command_base helper functions: // #define CMD2_A_FUNCTION(key, function, slot, parm, doc) \ rpc::commands.insert_slot::type>(key, slot, &rpc::function, \ rpc::CommandMap::flag_dont_delete | rpc::CommandMap::flag_public_rpc, NULL, NULL); #define CMD2_A_FUNCTION_PRIVATE(key, function, slot, parm, doc) \ rpc::commands.insert_slot::type>(key, slot, &rpc::function, \ rpc::CommandMap::flag_dont_delete, NULL, NULL); #define CMD2_ANY(key, slot) CMD2_A_FUNCTION(key, command_base_call, slot, "i:", "") #define CMD2_ANY_P(key, slot) CMD2_A_FUNCTION_PRIVATE(key, command_base_call, slot, "i:", "") #define CMD2_ANY_VOID(key, slot) CMD2_A_FUNCTION(key, command_base_call, object_convert_void(slot), "i:", "") #define CMD2_ANY_V(key, slot) CMD2_A_FUNCTION(key, command_base_call_list, object_convert_void(slot), "i:", "") #define CMD2_ANY_L(key, slot) CMD2_A_FUNCTION(key, command_base_call_list, slot, "A:", "") #define CMD2_ANY_VALUE(key, slot) CMD2_A_FUNCTION(key, command_base_call_value, slot, "i:i", "") #define CMD2_ANY_VALUE_V(key, slot) CMD2_A_FUNCTION(key, command_base_call_value, object_convert_void(slot), "i:i", "") #define CMD2_ANY_VALUE_KB(key, slot) CMD2_A_FUNCTION(key, command_base_call_value_kb, object_convert_void(slot), "i:i", "") #define CMD2_ANY_STRING(key, slot) CMD2_A_FUNCTION(key, command_base_call_string, slot, "i:s", "") #define CMD2_ANY_STRING_V(key, slot) CMD2_A_FUNCTION(key, command_base_call_string, object_convert_void(slot), "i:s", "") #define CMD2_ANY_LIST(key, slot) CMD2_A_FUNCTION(key, command_base_call_list, slot, "i:", "") #define CMD2_DL(key, slot) CMD2_A_FUNCTION(key, command_base_call, slot, "i:", "") #define CMD2_DL_V(key, slot) CMD2_A_FUNCTION(key, command_base_call, object_convert_void(slot), "i:", "") #define CMD2_DL_VALUE(key, slot) CMD2_A_FUNCTION(key, command_base_call_value, slot, "i:", "") #define CMD2_DL_VALUE_V(key, slot) CMD2_A_FUNCTION(key, command_base_call_value, object_convert_void(slot), "i:", "") #define CMD2_DL_STRING(key, slot) CMD2_A_FUNCTION(key, command_base_call_string, slot, "i:", "") #define CMD2_DL_STRING_V(key, slot) CMD2_A_FUNCTION(key, command_base_call_string, object_convert_void(slot), "i:", "") #define CMD2_DL_LIST(key, slot) CMD2_A_FUNCTION(key, command_base_call_list, slot, "i:", "") #define CMD2_DL_VALUE_P(key, slot) CMD2_A_FUNCTION_PRIVATE(key, command_base_call_value, slot, "i:", "") #define CMD2_DL_STRING_P(key, slot) CMD2_A_FUNCTION_PRIVATE(key, command_base_call_string, slot, "i:", "") #define CMD2_FILE(key, slot) CMD2_A_FUNCTION(key, command_base_call, slot, "i:", "") #define CMD2_FILE_V(key, slot) CMD2_A_FUNCTION(key, command_base_call, object_convert_void(slot), "i:", "") #define CMD2_FILE_VALUE_V(key, slot) CMD2_A_FUNCTION(key, command_base_call_value, object_convert_void(slot), "i:i", "") #define CMD2_FILEITR(key, slot) CMD2_A_FUNCTION(key, command_base_call, slot, "i:", "") #define CMD2_PEER(key, slot) CMD2_A_FUNCTION(key, command_base_call, slot, "i:", "") #define CMD2_PEER_V(key, slot) CMD2_A_FUNCTION(key, command_base_call, object_convert_void(slot), "i:", "") #define CMD2_PEER_VALUE_V(key, slot) CMD2_A_FUNCTION(key, command_base_call_value, object_convert_void(slot), "i:i", "") #define CMD2_TRACKER(key, slot) CMD2_A_FUNCTION(key, command_base_call, slot, "i:", "") #define CMD2_TRACKER_V(key, slot) CMD2_A_FUNCTION(key, command_base_call, object_convert_void(slot), "i:", "") #define CMD2_TRACKER_VALUE_V(key, slot) CMD2_A_FUNCTION(key, command_base_call_value, object_convert_void(slot), "i:i", "") #define CMD2_VAR_BOOL(key, value) \ control->object_storage()->insert_c_str(key, int64_t(value), rpc::object_storage::flag_bool_type); \ CMD2_ANY(key, std::bind(&rpc::object_storage::get, control->object_storage(), \ torrent::raw_string::from_c_str(key))); \ CMD2_ANY_VALUE(key ".set", std::bind(&rpc::object_storage::set_bool, control->object_storage(), \ torrent::raw_string::from_c_str(key), std::placeholders::_2)); #define CMD2_VAR_VALUE(key, value) \ control->object_storage()->insert_c_str(key, int64_t(value), rpc::object_storage::flag_value_type); \ CMD2_ANY(key, std::bind(&rpc::object_storage::get, control->object_storage(), \ torrent::raw_string::from_c_str(key))); \ CMD2_ANY_VALUE(key ".set", std::bind(&rpc::object_storage::set_value, control->object_storage(), \ torrent::raw_string::from_c_str(key), std::placeholders::_2)); #define CMD2_VAR_STRING(key, value) \ control->object_storage()->insert_c_str(key, value, rpc::object_storage::flag_string_type); \ CMD2_ANY(key, std::bind(&rpc::object_storage::get, control->object_storage(), \ torrent::raw_string::from_c_str(key))); \ CMD2_ANY_STRING(key ".set", std::bind(&rpc::object_storage::set_string, control->object_storage(), \ torrent::raw_string::from_c_str(key), std::placeholders::_2)); #define CMD2_VAR_C_STRING(key, value) \ control->object_storage()->insert_c_str(key, value, rpc::object_storage::flag_string_type); \ CMD2_ANY(key, std::bind(&rpc::object_storage::get, control->object_storage(), \ torrent::raw_string::from_c_str(key))); #define CMD2_VAR_LIST(key) \ control->object_storage()->insert_c_str(key, torrent::Object::create_list(), rpc::object_storage::flag_list_type); \ CMD2_ANY(key, std::bind(&rpc::object_storage::get, control->object_storage(), \ torrent::raw_string::from_c_str(key))); \ CMD2_ANY_LIST(key ".set", std::bind(&rpc::object_storage::set_list, control->object_storage(), \ torrent::raw_string::from_c_str(key), std::placeholders::_2)); \ CMD2_ANY_VOID(key ".push_back", std::bind(&rpc::object_storage::list_push_back, control->object_storage(), \ torrent::raw_string::from_c_str(key), std::placeholders::_2)); #define CMD2_FUNC_SINGLE(key, cmds) \ CMD2_ANY(key, std::bind(&rpc::command_function_call_object, torrent::Object(torrent::raw_string::from_c_str(cmds)), \ std::placeholders::_1, std::placeholders::_2)); #define CMD2_REDIRECT(from_key, to_key) \ rpc::commands.create_redirect(from_key, to_key, rpc::CommandMap::flag_public_rpc | rpc::CommandMap::flag_dont_delete); #define CMD2_REDIRECT_NO_EXPORT(from_key, to_key) \ rpc::commands.create_redirect(from_key, to_key, rpc::CommandMap::flag_dont_delete); #define CMD2_REDIRECT_MUTABLE(from_key, to_key) \ rpc::commands.create_redirect(from_key, to_key, rpc::CommandMap::flag_public_rpc); #define CMD2_REDIRECT_STR(from_key, to_key) \ rpc::commands.create_redirect(from_key, to_key, rpc::CommandMap::flag_public_rpc); #define CMD2_REDIRECT_STR_NO_EXPORT(from_key, to_key) \ rpc::commands.create_redirect(from_key, to_key, 0); #define CMD2_REDIRECT_FILE(from_key, to_key) \ rpc::commands.create_redirect(from_key, to_key, rpc::CommandMap::flag_public_rpc | rpc::CommandMap::flag_file_target | rpc::CommandMap::flag_dont_delete); #define CMD2_REDIRECT_TRACKER(from_key, to_key) \ rpc::commands.create_redirect(from_key, to_key, rpc::CommandMap::flag_public_rpc | rpc::CommandMap::flag_tracker_target | rpc::CommandMap::flag_dont_delete); // // Conversion of return types: // template struct object_convert_type; template struct object_convert_type { template struct result { typedef torrent::Object type; }; object_convert_type(Functor s) : m_slot(s) {} torrent::Object operator () () { m_slot(); return torrent::Object(); } template torrent::Object operator () (Arg1& arg1) { m_slot(arg1); return torrent::Object(); } template torrent::Object operator () (const Arg1& arg1) { m_slot(arg1); return torrent::Object(); } template torrent::Object operator () (Arg1& arg1, Arg2& arg2) { m_slot(arg1, arg2); return torrent::Object(); } template torrent::Object operator () (const Arg1& arg1, const Arg2& arg2) { m_slot(arg1, arg2); return torrent::Object(); } Functor m_slot; }; template object_convert_type object_convert_void(T f) { return f; } #endif