refactor in aux::write_ to avoid explicit castings (#1426)

refactor in aux::write_ to avoid explicit casts
This commit is contained in:
Alden Torres 2016-12-17 15:03:43 -05:00 committed by Arvid Norberg
parent 814f7363a4
commit 1b808b6516
2 changed files with 43 additions and 39 deletions

View File

@ -35,7 +35,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <cstdint>
#include <string>
#include <algorithm> // for copy
#include <type_traits>
#include "libtorrent/span.hpp"
namespace libtorrent { namespace aux
@ -50,7 +50,7 @@ namespace libtorrent { namespace aux
read_impl(span<Byte>& view, type<T>)
{
T ret = 0;
for (int i = 0; i < int(sizeof(T)); ++i)
for (std::size_t i = 0; i < sizeof(T); ++i)
{
ret <<= 8;
ret |= static_cast<std::uint8_t>(view[i]);
@ -59,12 +59,16 @@ namespace libtorrent { namespace aux
return ret;
}
template <class T, class Byte>
template <class T, class In, class Byte, typename Cond
= typename std::enable_if<std::is_integral<T>::value &&
(std::is_integral<In>::value || std::is_enum<In>::value)>::type>
inline typename std::enable_if<sizeof(Byte)==1, void>::type
write_impl(T val, span<Byte>& view)
write_impl(In data, span<Byte>& view)
{
T val = static_cast<T>(data);
TORRENT_ASSERT(data == static_cast<In>(val));
int shift = int(sizeof(T)) * 8;
for (int i = 0; i < int(sizeof(T)); ++i)
for (std::size_t i = 0; i < sizeof(T); ++i)
{
shift -= 8;
view[i] = static_cast<Byte>((val >> shift) & 0xff);
@ -128,47 +132,47 @@ namespace libtorrent { namespace aux
{ return read_impl(view, type<std::uint8_t>()); }
template <typename Byte>
void write_uint64(std::uint64_t val, span<Byte>& view)
{ write_impl(val, view); }
template <typename T, typename Byte>
void write_uint64(T val, span<Byte>& view)
{ write_impl<std::uint64_t>(val, view); }
template <typename Byte>
void write_int64(std::int64_t val, span<Byte>& view)
{ write_impl(val, view); }
template <typename T, typename Byte>
void write_int64(T val, span<Byte>& view)
{ write_impl<std::int64_t>(val, view); }
template <typename Byte>
void write_uint32(std::uint32_t val, span<Byte>& view)
{ write_impl(val, view); }
template <typename T, typename Byte>
void write_uint32(T val, span<Byte>& view)
{ write_impl<std::uint32_t>(val, view); }
template <typename Byte>
void write_int32(std::int32_t val, span<Byte>& view)
{ write_impl(val, view); }
template <typename T, typename Byte>
void write_int32(T val, span<Byte>& view)
{ write_impl<std::int32_t>(val, view); }
template <typename Byte>
void write_uint16(std::uint16_t val, span<Byte>& view)
{ write_impl(val, view); }
template <typename T, typename Byte>
void write_uint16(T val, span<Byte>& view)
{ write_impl<std::uint16_t>(val, view); }
template <typename Byte>
void write_int16(std::int16_t val, span<Byte>& view)
{ write_impl(val, view); }
template <typename T, typename Byte>
void write_int16(T val, span<Byte>& view)
{ write_impl<std::int16_t>(val, view); }
template <typename Byte>
void write_uint8(std::uint8_t val, span<Byte>& view)
{ write_impl(val, view); }
template <typename T, typename Byte>
void write_uint8(T val, span<Byte>& view)
{ write_impl<std::uint8_t>(val, view); }
template <typename Byte>
void write_int8(std::int8_t val, span<Byte>& view)
{ write_impl(val, view); }
template <typename T, typename Byte>
void write_int8(T val, span<Byte>& view)
{ write_impl<std::int8_t>(val, view); }
template<typename Byte>
inline int write_string(std::string const& str, span<Byte>& view)
{
int const len = int(str.size());
for (int i = 0; i < len; ++i)
std::size_t const len = str.size();
for (std::size_t i = 0; i < len; ++i)
view[i] = str[i];
view = span<Byte>(view.data() + len, int(view.size()) - len);
return len;
view = view.subspan(len);
return int(len);
}
}}

View File

@ -495,7 +495,7 @@ namespace libtorrent
aux::write_uint32(0x417, view);
aux::write_uint32(0x27101980, view); // connection_id
aux::write_int32(static_cast<int>(action_t::connect), view); // action (connect)
aux::write_int32(action_t::connect, view); // action (connect)
aux::write_int32(m_transaction_id, view); // transaction_id
TORRENT_ASSERT(view.size() == 0);
@ -556,7 +556,7 @@ namespace libtorrent
span<char> view = buf;
aux::write_int64(i->second.connection_id, view); // connection_id
aux::write_int32(static_cast<int>(action_t::scrape), view); // action (scrape)
aux::write_int32(action_t::scrape, view); // action (scrape)
aux::write_int32(m_transaction_id, view); // transaction_id
// info_hash
std::copy(tracker_req().info_hash.begin(), tracker_req().info_hash.end()
@ -708,7 +708,7 @@ namespace libtorrent
if (i == m_connection_cache.end()) return;
aux::write_int64(i->second.connection_id, out); // connection_id
aux::write_int32(static_cast<int>(action_t::announce), out); // action (announce)
aux::write_int32(action_t::announce, out); // action (announce)
aux::write_int32(m_transaction_id, out); // transaction_id
std::copy(req.info_hash.begin(), req.info_hash.end(), out.data()); // info_hash
out.subspan(20);
@ -728,7 +728,7 @@ namespace libtorrent
address ip = address::from_string(settings.get_str(settings_pack::announce_ip).c_str(), ec);
if (!ec && ip.is_v4()) announce_ip = ip.to_v4();
}
aux::write_uint32(std::uint32_t(announce_ip.to_ulong()), out);
aux::write_uint32(announce_ip.to_ulong(), out);
aux::write_int32(req.key, out); // key
aux::write_int32(req.num_want, out); // num_want
aux::write_uint16(req.listen_port, out); // port
@ -742,11 +742,11 @@ namespace libtorrent
if (!request_string.empty())
{
int str_len = (std::min)(int(request_string.size()), 255);
std::size_t str_len = std::min(request_string.size(), std::size_t(255));
request_string.resize(str_len);
aux::write_uint8(2, out);
aux::write_uint8(std::uint8_t(str_len), out);
aux::write_uint8(str_len, out);
aux::write_string(request_string, out);
}