diff --git a/bindings/python/src/torrent_handle.cpp b/bindings/python/src/torrent_handle.cpp index da851a37b..06a03d8c9 100644 --- a/bindings/python/src/torrent_handle.cpp +++ b/bindings/python/src/torrent_handle.cpp @@ -304,7 +304,7 @@ list get_download_queue(torrent_handle& handle) void set_metadata(torrent_handle& handle, std::string const& buf) { - handle.set_metadata(buf.c_str(), int(buf.size())); + handle.set_metadata(buf); } #ifndef TORRENT_NO_DEPRECATE diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 7a403069f..9c1e15b10 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -1015,7 +1015,7 @@ namespace libtorrent // to the checker thread for initial checking // of the storage. // a return value of false indicates an error - bool set_metadata(char const* metadata_buf, int metadata_size); + bool set_metadata(span metadata); #ifndef TORRENT_NO_DEPRECATE void on_torrent_download(error_code const& ec, http_parser const& parser diff --git a/include/libtorrent/torrent_handle.hpp b/include/libtorrent/torrent_handle.hpp index 012fad0cc..bb623d63a 100644 --- a/include/libtorrent/torrent_handle.hpp +++ b/include/libtorrent/torrent_handle.hpp @@ -51,6 +51,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/address.hpp" #include "libtorrent/socket.hpp" // tcp::endpoint +#include "libtorrent/span.hpp" namespace libtorrent { @@ -521,7 +522,13 @@ namespace libtorrent // if the metadata is successfully set on the torrent, and false // otherwise. If the torrent already has metadata, this function will not // affect the torrent, and false will be returned. - bool set_metadata(char const* metadata, int size) const; + bool set_metadata(span metadata) const; + +#ifndef TORRENT_NO_DEPRECATE + TORRENT_DEPRECATED + bool set_metadata(char const* metadata, int size) const + { return set_metadata({metadata, size_t(size)}); } +#endif // Returns true if this handle refers to a valid torrent and false if it // hasn't been initialized or if the torrent it refers to has been diff --git a/include/libtorrent/torrent_info.hpp b/include/libtorrent/torrent_info.hpp index cc46c4014..ee272cc29 100644 --- a/include/libtorrent/torrent_info.hpp +++ b/include/libtorrent/torrent_info.hpp @@ -36,12 +36,10 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include "libtorrent/aux_/disable_warnings_push.hpp" - -#include #include - #include "libtorrent/aux_/disable_warnings_pop.hpp" #include "libtorrent/config.hpp" @@ -63,8 +61,6 @@ namespace libtorrent struct announce_entry; struct lazy_entry; - namespace aux { struct session_settings; } - // internal, exposed for the unit test TORRENT_EXTRA_EXPORT void sanitize_append_path_element(std::string& path , string_view element); @@ -80,7 +76,7 @@ namespace libtorrent // http seed spec. by John Hoffman enum type_t { url_seed, http_seed }; - typedef std::vector > headers_t; + typedef std::vector> headers_t; web_seed_entry(std::string const& url_, type_t type_ , std::string const& auth_ = std::string() @@ -399,7 +395,7 @@ namespace libtorrent // returns true if this torrent_info object has a torrent loaded. // This is primarily used to determine if a magnet link has had its // metadata resolved yet or not. - bool is_valid() const { return (m_files.is_valid()) != 0; } + bool is_valid() const { return m_files.is_valid(); } // returns true if this torrent is private. i.e., it should not be // distributed on the trackerless network (the kademlia DHT). @@ -469,7 +465,8 @@ namespace libtorrent // // .. _`posix time`: http://www.opengroup.org/onlinepubs/009695399/functions/time.html const std::string& name() const { return m_files.name(); } - boost::optional creation_date() const; + time_t creation_date() const + { return m_creation_date; } const std::string& creator() const { return m_created_by; } const std::string& comment() const @@ -565,7 +562,7 @@ namespace libtorrent // pointers point directly into the info_section buffer and when copied, // these pointers must be corrected to point into the new buffer. The // int is the length of the string. Strings are not 0-terminated. - std::vector > m_collections; + std::vector> m_collections; // these are the collections from outside of the info-dict. These are // owning strings, since we only keep the info-section around, these @@ -580,6 +577,7 @@ namespace libtorrent // this is a copy of the info section from the torrent. // it use maintained in this flat format in order to // make it available through the metadata extension + // TODO: change the type to std::shared_ptr in C++17 boost::shared_array m_info_section; // this is a pointer into the m_info_section buffer @@ -640,4 +638,3 @@ namespace libtorrent } #endif // TORRENT_TORRENT_INFO_HPP_INCLUDED - diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index a9a09e6e4..89e7b5bd4 100644 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -1743,10 +1743,9 @@ namespace libtorrent , "msg: %d size: %d", extended_id, m_recv_buffer.packet_size()); #endif - for (extension_list_t::iterator i = m_extensions.begin() - , end(m_extensions.end()); i != end; ++i) + for (auto const& e : m_extensions) { - if ((*i)->on_extended(m_recv_buffer.packet_size() - 2, extended_id + if (e->on_extended(m_recv_buffer.packet_size() - 2, extended_id , recv_buffer)) return; } diff --git a/src/create_torrent.cpp b/src/create_torrent.cpp index 474e07a5f..ee7b98414 100644 --- a/src/create_torrent.cpp +++ b/src/create_torrent.cpp @@ -386,7 +386,7 @@ namespace libtorrent TORRENT_ASSERT(ti.num_files() > 0); TORRENT_ASSERT(ti.total_size() > 0); - if (ti.creation_date()) m_creation_date = *ti.creation_date(); + if (ti.creation_date() > 0) m_creation_date = ti.creation_date(); if (!ti.creator().empty()) set_creator(ti.creator().c_str()); if (!ti.comment().empty()) set_comment(ti.comment().c_str()); @@ -394,19 +394,15 @@ namespace libtorrent for (auto const& n : ti.nodes()) add_node(n); - std::vector const& trackers = ti.trackers(); - for (std::vector::const_iterator i = trackers.begin() - , end(trackers.end()); i != end; ++i) - add_tracker(i->url, i->tier); + for (auto const& t : ti.trackers()) + add_tracker(t.url, t.tier); - std::vector const& web_seeds = ti.web_seeds(); - for (std::vector::const_iterator i = web_seeds.begin() - , end(web_seeds.end()); i != end; ++i) + for (auto const& s : ti.web_seeds()) { - if (i->type == web_seed_entry::url_seed) - add_url_seed(i->url); - else if (i->type == web_seed_entry::http_seed) - add_http_seed(i->url); + if (s.type == web_seed_entry::url_seed) + add_url_seed(s.url); + else if (s.type == web_seed_entry::http_seed) + add_http_seed(s.url); } m_piece_hash.resize(m_files.num_pieces()); diff --git a/src/kademlia/dht_storage.cpp b/src/kademlia/dht_storage.cpp index a4e89ac75..b7cc6eb60 100644 --- a/src/kademlia/dht_storage.cpp +++ b/src/kademlia/dht_storage.cpp @@ -112,13 +112,13 @@ namespace std::string salt; }; - void touch_item(dht_immutable_item* f, address const& address) + void touch_item(dht_immutable_item* f, address const& addr) { f->last_seen = aux::time_now(); // maybe increase num_announcers if we haven't seen this IP before sha1_hash iphash; - hash_address(address, iphash); + hash_address(addr, iphash); if (!f->ips.find(iphash)) { f->ips.set(iphash); diff --git a/src/torrent.cpp b/src/torrent.cpp index 78891bc8d..eec2a79db 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -6375,8 +6375,7 @@ namespace libtorrent } // blocks per piece - int num_blocks_per_piece = - static_cast(torrent_file().piece_length()) / block_size(); + int num_blocks_per_piece = torrent_file().piece_length() / block_size(); ret["blocks per piece"] = num_blocks_per_piece; if (m_torrent_file->is_merkle_torrent()) @@ -7002,15 +7001,14 @@ namespace libtorrent return peerinfo->connection != nullptr; } - // TODO: 3 make this take a span instead - bool torrent::set_metadata(char const* metadata_buf, int metadata_size) + bool torrent::set_metadata(span metadata_buf) { TORRENT_ASSERT(is_single_thread()); INVARIANT_CHECK; if (m_torrent_file->is_valid()) return false; - sha1_hash const info_hash = hasher(metadata_buf, metadata_size).final(); + sha1_hash const info_hash = hasher(metadata_buf).final(); if (info_hash != m_torrent_file->info_hash()) { if (alerts().should_post()) @@ -7023,8 +7021,7 @@ namespace libtorrent bdecode_node metadata; error_code ec; - int ret = bdecode(metadata_buf - , metadata_buf + metadata_size, metadata, ec); + int ret = bdecode(metadata_buf.begin(), metadata_buf.end(), metadata, ec); if (ret != 0 || !m_torrent_file->parse_info_section(metadata, ec, 0)) { update_gauge(); diff --git a/src/torrent_handle.cpp b/src/torrent_handle.cpp index aec49254d..0dc0f7942 100644 --- a/src/torrent_handle.cpp +++ b/src/torrent_handle.cpp @@ -217,9 +217,9 @@ namespace libtorrent #endif } - bool torrent_handle::set_metadata(char const* metadata, int size) const + bool torrent_handle::set_metadata(span metadata) const { - return sync_call_ret(false, &torrent::set_metadata, metadata, size); + return sync_call_ret(false, &torrent::set_metadata, metadata); } void torrent_handle::pause(int flags) const diff --git a/src/torrent_info.cpp b/src/torrent_info.cpp index 7551d9f7c..06f56828c 100644 --- a/src/torrent_info.cpp +++ b/src/torrent_info.cpp @@ -662,9 +662,8 @@ namespace libtorrent if (m_info_section_size == 0) return; TORRENT_ASSERT(m_piece_hashes); - error_code ec; m_info_section.reset(new char[m_info_section_size]); - memcpy(m_info_section.get(), t.m_info_section.get(), m_info_section_size); + std::memcpy(m_info_section.get(), t.m_info_section.get(), m_info_section_size); ptrdiff_t offset = m_info_section.get() - t.m_info_section.get(); @@ -1546,16 +1545,6 @@ namespace libtorrent return true; } - boost::optional - torrent_info::creation_date() const - { - if (m_creation_date != 0) - { - return boost::optional(m_creation_date); - } - return boost::optional(); - } - void torrent_info::add_tracker(std::string const& url, int tier) { announce_entry e(url); diff --git a/src/ut_metadata.cpp b/src/ut_metadata.cpp index 752b590f9..06e3ffcd8 100644 --- a/src/ut_metadata.cpp +++ b/src/ut_metadata.cpp @@ -53,8 +53,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/performance_counters.hpp" // for counters #include "libtorrent/aux_/time.hpp" -using namespace std::placeholders; - namespace libtorrent { namespace { enum @@ -604,7 +602,7 @@ namespace libtorrent { namespace if (!have_all) return false; - if (!m_torrent.set_metadata(&m_metadata[0], m_metadata_size)) + if (!m_torrent.set_metadata({m_metadata.get(), size_t(m_metadata_size)})) { if (!m_torrent.valid_metadata()) { diff --git a/test/test_read_resume.cpp b/test/test_read_resume.cpp index d83e6ca55..34aa38c1f 100644 --- a/test/test_read_resume.cpp +++ b/test/test_read_resume.cpp @@ -166,7 +166,7 @@ TORRENT_TEST(read_resume_mismatching_torrent) TEST_CHECK(!atp.ti); } -boost::shared_ptr generate_torrent() +std::shared_ptr generate_torrent() { file_storage fs; fs.add_file("test_resume/tmp1", 128 * 1024 * 8); @@ -188,12 +188,12 @@ boost::shared_ptr generate_torrent() std::vector buf; bencode(std::back_inserter(buf), t.generate()); - return boost::make_shared(&buf[0], buf.size()); + return std::make_shared(&buf[0], buf.size()); } TORRENT_TEST(read_resume_torrent) { - boost::shared_ptr ti = generate_torrent(); + std::shared_ptr ti = generate_torrent(); entry rd; rd["file-format"] = "libtorrent resume file"; diff --git a/test/test_torrent_info.cpp b/test/test_torrent_info.cpp index e921963d7..634ff3306 100644 --- a/test/test_torrent_info.cpp +++ b/test/test_torrent_info.cpp @@ -38,10 +38,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/aux_/escape_string.hpp" // for convert_path_to_posix #include "libtorrent/hex.hpp" // to_hex -#include "libtorrent/aux_/disable_warnings_push.hpp" -#include -#include "libtorrent/aux_/disable_warnings_pop.hpp" - #include using namespace libtorrent; @@ -617,7 +613,7 @@ TORRENT_TEST(parse_torrents) std::fprintf(stderr, "loading %s\n", test_torrents[i].file); std::string filename = combine_path(combine_path(root_dir, "test_torrents") , test_torrents[i].file); - auto ti = boost::make_shared(filename, ec); + auto ti = std::make_shared(filename, ec); TEST_CHECK(!ec); if (ec) std::fprintf(stderr, " loading(\"%s\") -> failed %s\n", filename.c_str() , ec.message().c_str()); @@ -644,7 +640,7 @@ TORRENT_TEST(parse_torrents) } else if (std::string(test_torrents[i].file) == "creation_date.torrent") { - TEST_EQUAL(*ti->creation_date(), 1234567); + TEST_EQUAL(ti->creation_date(), 1234567); } else if (std::string(test_torrents[i].file) == "duplicate_web_seeds.torrent") { @@ -753,7 +749,7 @@ TORRENT_TEST(parse_torrents) { error_code ec; std::fprintf(stderr, "loading %s\n", test_error_torrents[i].file); - auto ti = boost::make_shared(combine_path( + auto ti = std::make_shared(combine_path( combine_path(root_dir, "test_torrents"), test_error_torrents[i].file), ec); std::fprintf(stderr, "E: \"%s\"\nexpected: \"%s\"\n", ec.message().c_str() , test_error_torrents[i].error.message().c_str()); @@ -859,7 +855,7 @@ TORRENT_TEST(resolve_duplicates) TORRENT_TEST(empty_file) { error_code ec; - auto ti = boost::make_shared("", 0, ec); + auto ti = std::make_shared("", 0, ec); TEST_CHECK(ec); } @@ -867,7 +863,7 @@ TORRENT_TEST(empty_file2) { try { - auto ti = boost::make_shared("", 0); + auto ti = std::make_shared("", 0); TEST_ERROR("expected exception thrown"); } catch (system_error& e) @@ -880,9 +876,9 @@ TORRENT_TEST(copy) { using namespace libtorrent; - boost::shared_ptr a(boost::make_shared( + std::shared_ptr a = std::make_shared( combine_path(parent_path(current_working_directory()) - , combine_path("test_torrents", "sample.torrent")))); + , combine_path("test_torrents", "sample.torrent"))); char const* expected_files[] = { @@ -909,12 +905,12 @@ TORRENT_TEST(copy) } // copy the torrent_info object - boost::shared_ptr b(boost::make_shared(*a)); + std::shared_ptr b = std::make_shared(*a); // clear out the buffer for a, just to make sure b doesn't have any // references into it by mistake int s = a->metadata_size(); - memset(a->metadata().get(), 0, s); + std::memset(a->metadata().get(), 0, s); a.reset(); @@ -930,5 +926,3 @@ TORRENT_TEST(copy) TEST_EQUAL(b->files().hash(i), file_hashes[i]); } } - -