From 79bfc65e710cd53e36613b1433f0f027da5fdf65 Mon Sep 17 00:00:00 2001 From: Jesse Chan Date: Mon, 23 May 2022 20:07:52 -0700 Subject: [PATCH] Replace custom "rt_triple" with "std::tuple" --- include/display/text_element_string.h | 5 ++-- include/rpc/command.h | 43 +++++---------------------- include/rpc/command_impl.h | 8 ++--- include/rpc/command_map.h | 28 ++++++++++------- src/command_logic.cc | 2 +- src/display/text_element_value.cc | 2 +- src/display/window_text.cc | 2 +- src/rpc/rpc_json.cc | 2 +- src/rpc/rpc_xml.cc | 6 ++-- 9 files changed, 38 insertions(+), 60 deletions(-) diff --git a/include/display/text_element_string.h b/include/display/text_element_string.h index 8f7f7dc3..a9dfb87f 100644 --- a/include/display/text_element_string.h +++ b/include/display/text_element_string.h @@ -119,10 +119,11 @@ class TextElementStringSlot : public TextElementStringBase { private: char* copy_string(char* first, char* last, rpc::target_type target) override { - if (target.second == nullptr) + if (std::get<1>(target) == nullptr) return first; - result_type result = m_slot(reinterpret_cast(target.second)); + result_type result = + m_slot(reinterpret_cast(std::get<1>(target))); extent_type length = std::min(result_length(&result), last - first); diff --git a/include/rpc/command.h b/include/rpc/command.h index 1075e44a..0af8331e 100644 --- a/include/rpc/command.h +++ b/include/rpc/command.h @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -38,40 +39,10 @@ struct target_wrapper { using cleaned_type = no_type*; }; -// Since c++0x isn't out yet... -template -struct rt_triple : private std::pair { - using base_type = std::pair; - using third_type = T3; - - using base_type::first; - using base_type::second; - using typename base_type::first_type; - using typename base_type::second_type; - - T3 third; - - rt_triple() - : base_type() - , third() {} - - rt_triple(const T1& a, const T2& b) - : base_type(a, b) - , third() {} - - rt_triple(const T1& a, const T2& b, const T3& c) - : base_type(a, b) - , third(c) {} - - rt_triple(const base_type& b) - : base_type(b) - , third() {} -}; - // Since it gets used so many places we might as well put it in the // rpc namespace. // typedef std::pair target_type; -using target_type = rt_triple; +using target_type = std::tuple; class command_base; @@ -251,28 +222,28 @@ struct target_type_id { template inline bool is_target_compatible(const target_type& target) { - return target.first == target_type_id::value; + return std::get<0>(target) == target_type_id::value; } // Splitting pairs into separate targets. inline bool is_target_pair(const target_type& target) { - return target.first >= command_base::target_download_pair; + return std::get<0>(target) >= command_base::target_download_pair; } template inline T get_target_cast(target_type target, int = target_type_id::value) { - return (T)target.second; + return (T)std::get<1>(target); } inline target_type get_target_left(const target_type& target) { - return target_type(target.first - 5, target.second); + return { std::get<0>(target) - 5, std::get<1>(target), nullptr }; } inline target_type get_target_right(const target_type& target) { - return target_type(target.first - 5, target.third); + return { std::get<0>(target) - 5, std::get<2>(target), nullptr }; } } diff --git a/include/rpc/command_impl.h b/include/rpc/command_impl.h index edd61c77..0c7f1b46 100644 --- a/include/rpc/command_impl.h +++ b/include/rpc/command_impl.h @@ -90,7 +90,7 @@ is_target_compatible(const target_type&) { template<> inline bool is_target_compatible(const target_type& target) { - return target.first == command_base::target_file || + return std::get<0>(target) == command_base::target_file || command_base::target_file_itr; } @@ -103,10 +103,10 @@ get_target_cast(target_type target, int) { template<> inline torrent::File* get_target_cast(target_type target, int) { - if (target.first == command_base::target_file_itr) - return static_cast(target.second)->file(); + if (std::get<0>(target) == command_base::target_file_itr) + return static_cast(std::get<1>(target))->file(); else - return static_cast(target.second); + return static_cast(std::get<1>(target)); } inline torrent::Object* diff --git a/include/rpc/command_map.h b/include/rpc/command_map.h index 727ac2ca..1a6c1e4b 100644 --- a/include/rpc/command_map.h +++ b/include/rpc/command_map.h @@ -123,62 +123,68 @@ class CommandMap key_type key, const mapped_type& arg, target_type target = target_type((int)command_base::target_generic, - nullptr)); + nullptr, + nullptr)); const mapped_type call_command( iterator itr, const mapped_type& arg, target_type target = target_type((int)command_base::target_generic, - nullptr)); + nullptr, + nullptr)); 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)); + key, + arg, + target_type((int)command_base::target_download, download, nullptr)); } 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)); + key, arg, target_type((int)command_base::target_peer, peer, nullptr)); } 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)); + key, + arg, + target_type((int)command_base::target_tracker, tracker, nullptr)); } 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)); + key, arg, target_type((int)command_base::target_file, file, nullptr)); } }; inline target_type make_target() { - return target_type((int)command_base::target_generic, nullptr); + return { (int)command_base::target_generic, nullptr, nullptr }; } inline target_type make_target(int type, void* target) { - return target_type(type, target); + return { type, target, nullptr }; } inline target_type make_target(int type, void* target1, void* target2) { - return target_type(type, target1, target2); + return { type, target1, target2 }; } template inline target_type make_target(T target) { - return target_type((int)target_type_id::value, target); + return { (int)target_type_id::value, target, nullptr }; } template inline target_type make_target_pair(T target1, T target2) { - return target_type((int)target_type_id::value, target1, target2); + return { (int)target_type_id::value, target1, target2 }; } // TODO: Helper-functions that really should be in the diff --git a/src/command_logic.cc b/src/command_logic.cc index 81a57f36..753f3be3 100644 --- a/src/command_logic.cc +++ b/src/command_logic.cc @@ -272,7 +272,7 @@ apply_compare(rpc::target_type target, const torrent::Object::list_type& args) { } // if all else is equal, ensure stable sort order based on memory location - return (int64_t)(target.second < target.third); + return (int64_t)(std::get<1>(target) < std::get<2>(target)); } // Regexp based 'match' function. diff --git a/src/display/text_element_value.cc b/src/display/text_element_value.cc index 95756cff..dcd72b33 100644 --- a/src/display/text_element_value.cc +++ b/src/display/text_element_value.cc @@ -37,7 +37,7 @@ TextElementValueBase::print(char* first, push_attribute(attributes, Attributes(first, m_attributes, Attributes::color_invalid)); - int64_t val = value(target.second); + int64_t val = value(std::get<1>(target)); // Transform the value if needed. if (m_flags & flag_elapsed) diff --git a/src/display/window_text.cc b/src/display/window_text.cc index b58a2cbc..5129bdbc 100644 --- a/src/display/window_text.cc +++ b/src/display/window_text.cc @@ -64,7 +64,7 @@ WindowText::redraw() { unsigned int position = 0; - if (m_errorHandler != nullptr && m_target.second == nullptr) { + if (m_errorHandler != nullptr && std::get<1>(m_target) == nullptr) { char* buffer = static_cast(calloc(width + 1, sizeof(char))); Canvas::attributes_list attributes; diff --git a/src/rpc/rpc_json.cc b/src/rpc/rpc_json.cc index 84b5581a..3bc76f80 100644 --- a/src/rpc/rpc_json.cc +++ b/src/rpc/rpc_json.cc @@ -103,7 +103,7 @@ string_to_target(const std::string_view& targetString, throw torrent::input_error("invalid parameters: invalid index"); } - if (target == nullptr || target->second == nullptr) { + if (target == nullptr || std::get<1>(*target) == nullptr) { throw torrent::input_error( "invalid parameters: unable to find requested target"); } diff --git a/src/rpc/rpc_xml.cc b/src/rpc/rpc_xml.cc index 4e2d183b..c77057cf 100644 --- a/src/rpc/rpc_xml.cc +++ b/src/rpc/rpc_xml.cc @@ -231,7 +231,7 @@ xmlrpc_to_target(xmlrpc_env* env, xmlrpc_value* value) { ::free((void*)str); // Check if the target pointer is NULL. - if (target.second == nullptr) + if (std::get<1>(target) == nullptr) throw xmlrpc_error(XMLRPC_TYPE_ERROR, "Invalid index."); return target; @@ -364,7 +364,7 @@ xmlrpc_to_object(xmlrpc_env* env, if (env->fault_occurred) throw xmlrpc_error(env); - if (target->first == command_base::target_download && + if (std::get<0>(*target) == command_base::target_download && (callType == command_base::target_file || callType == command_base::target_tracker)) { // If we have a download target and the call type requires @@ -378,7 +378,7 @@ xmlrpc_to_object(xmlrpc_env* env, *target = xmlrpc_to_index_type( xmlrpc_list_entry_to_value(env, value, current++), callType, - (core::Download*)target->second); + (core::Download*)std::get<1>(*target)); } }