Skip to content

Commit

Permalink
Replace custom "rt_triple" with "std::tuple"
Browse files Browse the repository at this point in the history
  • Loading branch information
jesec committed May 27, 2022
1 parent b001c00 commit 79bfc65
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 60 deletions.
5 changes: 3 additions & 2 deletions include/display/text_element_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<arg1_type>(target.second));
result_type result =
m_slot(reinterpret_cast<arg1_type>(std::get<1>(target)));
extent_type length =
std::min<extent_type>(result_length(&result), last - first);

Expand Down
43 changes: 7 additions & 36 deletions include/rpc/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <functional>
#include <limits>
#include <tuple>

#include <torrent/data/file_list_iterator.h>
#include <torrent/object.h>
Expand Down Expand Up @@ -38,40 +39,10 @@ struct target_wrapper<void> {
using cleaned_type = no_type*;
};

// Since c++0x isn't out yet...
template<typename T1, typename T2, typename T3>
struct rt_triple : private std::pair<T1, T2> {
using base_type = std::pair<T1, T2>;
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<int, void*> target_type;
using target_type = rt_triple<int, void*, void*>;
using target_type = std::tuple<int, void*, void*>;

class command_base;

Expand Down Expand Up @@ -251,28 +222,28 @@ struct target_type_id {
template<typename T>
inline bool
is_target_compatible(const target_type& target) {
return target.first == target_type_id<T>::value;
return std::get<0>(target) == target_type_id<T>::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<typename T>
inline T
get_target_cast(target_type target, int = target_type_id<T>::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 };
}

}
Expand Down
8 changes: 4 additions & 4 deletions include/rpc/command_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ is_target_compatible<target_type>(const target_type&) {
template<>
inline bool
is_target_compatible<torrent::File*>(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;
}

Expand All @@ -103,10 +103,10 @@ get_target_cast<target_type>(target_type target, int) {
template<>
inline torrent::File*
get_target_cast<torrent::File*>(target_type target, int) {
if (target.first == command_base::target_file_itr)
return static_cast<torrent::FileListIterator*>(target.second)->file();
if (std::get<0>(target) == command_base::target_file_itr)
return static_cast<torrent::FileListIterator*>(std::get<1>(target))->file();
else
return static_cast<torrent::File*>(target.second);
return static_cast<torrent::File*>(std::get<1>(target));
}

inline torrent::Object*
Expand Down
28 changes: 17 additions & 11 deletions include/rpc/command_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename T>
inline target_type
make_target(T target) {
return target_type((int)target_type_id<T>::value, target);
return { (int)target_type_id<T>::value, target, nullptr };
}

template<typename T>
inline target_type
make_target_pair(T target1, T target2) {
return target_type((int)target_type_id<T, T>::value, target1, target2);
return { (int)target_type_id<T, T>::value, target1, target2 };
}

// TODO: Helper-functions that really should be in the
Expand Down
2 changes: 1 addition & 1 deletion src/command_logic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/display/text_element_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/display/window_text.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<char*>(calloc(width + 1, sizeof(char)));

Canvas::attributes_list attributes;
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/rpc_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
6 changes: 3 additions & 3 deletions src/rpc/rpc_xml.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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));
}
}

Expand Down

0 comments on commit 79bfc65

Please sign in to comment.