From 7ac2805c2c26beafb11f46a58990b21176e7edf6 Mon Sep 17 00:00:00 2001 From: Alden Torres Date: Thu, 12 Jan 2017 18:40:59 -0500 Subject: [PATCH] added custom resize methods to aux::vector (#1514) added custom resize methods to aux::vector, code refactor --- include/libtorrent/aux_/vector.hpp | 34 +++++++++++++++++++++++++++++- src/create_torrent.cpp | 8 +++---- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/include/libtorrent/aux_/vector.hpp b/include/libtorrent/aux_/vector.hpp index 3c640dcb4..c0c7adc88 100644 --- a/include/libtorrent/aux_/vector.hpp +++ b/include/libtorrent/aux_/vector.hpp @@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE. #define TORRENT_VECTOR_HPP #include +#include #include "libtorrent/units.hpp" #include "libtorrent/assert.hpp" @@ -70,7 +71,38 @@ namespace libtorrent { namespace aux { } IndexType end_index() const - { return IndexType(static_cast(this->size())); } + { + TORRENT_ASSERT(this->size() <= std::size_t(std::numeric_limits::max())); + return IndexType(static_cast(this->size())); + } + + template ::value>::type> + void resize(underlying_index s) + { + TORRENT_ASSERT(s >= 0); + this->base::resize(std::size_t(s)); + } + + template ::value>::type> + void resize(underlying_index s, T const& v) + { + TORRENT_ASSERT(s >= 0); + this->base::resize(std::size_t(s), v); + } + + void resize(std::size_t s) + { + TORRENT_ASSERT(s <= std::size_t(std::numeric_limits::max())); + this->base::resize(s); + } + + void resize(std::size_t s, T const& v) + { + TORRENT_ASSERT(s <= std::size_t(std::numeric_limits::max())); + this->base::resize(s, v); + } }; template diff --git a/src/create_torrent.cpp b/src/create_torrent.cpp index a0c6c86d1..c9dcfc0ad 100644 --- a/src/create_torrent.cpp +++ b/src/create_torrent.cpp @@ -363,7 +363,7 @@ namespace libtorrent m_files.set_num_pieces(static_cast( (m_files.total_size() + m_files.piece_length() - 1) / m_files.piece_length())); - m_piece_hash.resize(std::size_t(m_files.num_pieces())); + m_piece_hash.resize(m_files.num_pieces()); } create_torrent::create_torrent(torrent_info const& ti) @@ -399,7 +399,7 @@ namespace libtorrent add_http_seed(s.url); } - m_piece_hash.resize(std::size_t(m_files.num_pieces())); + m_piece_hash.resize(m_files.num_pieces()); for (piece_index_t i(0); i != m_files.end_piece(); ++i) set_hash(i, ti.hash_for_piece(i)); @@ -614,7 +614,7 @@ namespace libtorrent int const num_leafs = merkle_num_leafs(m_files.num_pieces()); int const num_nodes = merkle_num_nodes(num_leafs); int const first_leaf = num_nodes - num_leafs; - m_merkle_tree.resize(std::size_t(num_nodes)); + m_merkle_tree.resize(num_nodes); int const num_pieces = int(m_piece_hash.size()); for (int i = 0; i < num_pieces; ++i) m_merkle_tree[first_leaf + i] = m_piece_hash[piece_index_t(i)]; @@ -695,7 +695,7 @@ namespace libtorrent { TORRENT_ASSERT(index >= file_index_t(0)); TORRENT_ASSERT(index < m_files.end_file()); - if (m_filehashes.empty()) m_filehashes.resize(std::size_t(m_files.num_files())); + if (m_filehashes.empty()) m_filehashes.resize(m_files.num_files()); m_filehashes[index] = h; }