From 78bbd298a50c91133fa6a3fff6b0874b6021ba11 Mon Sep 17 00:00:00 2001 From: arvidn Date: Sat, 22 Jul 2017 18:59:55 -0700 Subject: [PATCH] convert file_open_mode to type safe flags --- bindings/python/src/converters.cpp | 2 ++ bindings/python/src/torrent_handle.cpp | 23 +++++++++++++---------- examples/client_test.cpp | 13 ++++++------- include/libtorrent/disk_interface.hpp | 26 +++++++++++++++----------- src/file_pool.cpp | 6 +++--- 5 files changed, 39 insertions(+), 31 deletions(-) diff --git a/bindings/python/src/converters.cpp b/bindings/python/src/converters.cpp index ac494edb3..ac6f610bc 100644 --- a/bindings/python/src/converters.cpp +++ b/bindings/python/src/converters.cpp @@ -294,6 +294,7 @@ void bind_converters() to_python_converter>(); to_python_converter>(); to_python_converter>(); + to_python_converter>(); // work-around types to_python_converter, address_to_tuple< @@ -340,4 +341,5 @@ void bind_converters() to_bitfield_flag(); to_bitfield_flag(); to_bitfield_flag(); + to_bitfield_flag(); } diff --git a/bindings/python/src/torrent_handle.cpp b/bindings/python/src/torrent_handle.cpp index f6cd06988..dc1e9ebb4 100644 --- a/bindings/python/src/torrent_handle.cpp +++ b/bindings/python/src/torrent_handle.cpp @@ -427,6 +427,8 @@ void add_piece(torrent_handle& th, piece_index_t piece, char const *data, int fl th.add_piece(piece, data, flags); } +class dummy {}; + using by_value = return_value_policy; void bind_torrent_handle() { @@ -578,16 +580,17 @@ void bind_torrent_handle() .def_readonly("open_mode", &open_file_state::open_mode) ; - enum_("file_open_mode") - .value("read_only", file_open_mode::read_only) - .value("write_only", file_open_mode::write_only) - .value("read_write", file_open_mode::read_write) - .value("rw_mask", file_open_mode::rw_mask) - .value("sparse", file_open_mode::sparse) - .value("no_atime", file_open_mode::no_atime) - .value("random_access", file_open_mode::random_access) - .value("locked", file_open_mode::locked) - ; + { + scope s = class_("file_open_mode"); + s.attr("read_only") = file_open_mode::read_only; + s.attr("write_only") = file_open_mode::write_only; + s.attr("read_write") = file_open_mode::read_write; + s.attr("rw_mask") = file_open_mode::rw_mask; + s.attr("sparse") = file_open_mode::sparse; + s.attr("no_atime") = file_open_mode::no_atime; + s.attr("random_access") = file_open_mode::random_access; + s.attr("locked") = file_open_mode::locked; + } enum_("file_progress_flags") .value("piece_granularity", torrent_handle::piece_granularity) diff --git a/examples/client_test.cpp b/examples/client_test.cpp index fc77c9e85..1b08d5230 100644 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -70,7 +70,6 @@ using lt::total_milliseconds; using lt::alert; using lt::piece_index_t; using lt::file_index_t; -using lt::file_open_mode; using lt::torrent_handle; using lt::add_torrent_params; using lt::cache_status; @@ -1894,12 +1893,12 @@ COLUMN OPTIONS if (f != file_status.end() && f->file_index == i) { title += " [ "; - if ((f->open_mode & file_open_mode::rw_mask) == file_open_mode::read_write) title += "read/write "; - else if ((f->open_mode & file_open_mode::rw_mask) == file_open_mode::read_only) title += "read "; - else if ((f->open_mode & file_open_mode::rw_mask) == file_open_mode::write_only) title += "write "; - if (f->open_mode & file_open_mode::random_access) title += "random_access "; - if (f->open_mode & file_open_mode::locked) title += "locked "; - if (f->open_mode & file_open_mode::sparse) title += "sparse "; + if ((f->open_mode & lt::file_open_mode::rw_mask) == lt::file_open_mode::read_write) title += "read/write "; + else if ((f->open_mode & lt::file_open_mode::rw_mask) == lt::file_open_mode::read_only) title += "read "; + else if ((f->open_mode & lt::file_open_mode::rw_mask) == lt::file_open_mode::write_only) title += "write "; + if (f->open_mode & lt::file_open_mode::random_access) title += "random_access "; + if (f->open_mode & lt::file_open_mode::locked) title += "locked "; + if (f->open_mode & lt::file_open_mode::sparse) title += "sparse "; title += "]"; ++f; } diff --git a/include/libtorrent/disk_interface.hpp b/include/libtorrent/disk_interface.hpp index 193f9b4a5..66d0ca5a0 100644 --- a/include/libtorrent/disk_interface.hpp +++ b/include/libtorrent/disk_interface.hpp @@ -45,6 +45,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/storage_defs.hpp" #include "libtorrent/time.hpp" #include "libtorrent/sha1_hash.hpp" +#include "libtorrent/flags.hpp" namespace libtorrent { @@ -61,37 +62,40 @@ namespace libtorrent { struct storage_holder; - enum file_open_mode + struct file_open_mode_tag; + using file_open_mode_t = flags::bitfield_flag; + + namespace file_open_mode { // open the file for reading only - read_only = 0, + constexpr file_open_mode_t read_only{0}; // open the file for writing only - write_only = 1, + constexpr file_open_mode_t write_only{1}; // open the file for reading and writing - read_write = 2, + constexpr file_open_mode_t read_write{2}; // the mask for the bits determining read or write mode - rw_mask = read_only | write_only | read_write, + constexpr file_open_mode_t rw_mask = read_only | write_only | read_write; // open the file in sparse mode (if supported by the // filesystem). - sparse = 0x4, + constexpr file_open_mode_t sparse{0x4}; // don't update the access timestamps on the file (if // supported by the operating system and filesystem). // this generally improves disk performance. - no_atime = 0x8, + constexpr file_open_mode_t no_atime{0x8}; // open the file for random access. This disables read-ahead // logic - random_access = 0x10, + constexpr file_open_mode_t random_access{0x10}; // prevent the file from being opened by another process // while it's still being held open by this handle - locked = 0x20, - }; + constexpr file_open_mode_t locked{0x20}; + } // this contains information about a file that's currently open by the // libtorrent disk I/O subsystem. It's associated with a single torent. @@ -107,7 +111,7 @@ namespace libtorrent { // // Note that the read/write mode is not a bitmask. The two least significant bits are used // to represent the read/write mode. Those bits can be masked out using the ``rw_mask`` constant. - std::uint32_t open_mode; + file_open_mode_t open_mode; // a (high precision) timestamp of when the file was last used. time_point last_use; diff --git a/src/file_pool.cpp b/src/file_pool.cpp index 83960476e..465595c2b 100644 --- a/src/file_pool.cpp +++ b/src/file_pool.cpp @@ -182,9 +182,9 @@ namespace libtorrent { namespace { - std::uint32_t to_file_open_mode(open_mode_t const mode) + file_open_mode_t to_file_open_mode(open_mode_t const mode) { - std::uint32_t ret = 0; + file_open_mode_t ret; open_mode_t const rw_mode = mode & open_mode::rw_mask; ret = (rw_mode == open_mode::read_only) @@ -193,7 +193,7 @@ namespace libtorrent { ? file_open_mode::write_only : (rw_mode == open_mode::read_write) ? file_open_mode::read_write - : 0; + : file_open_mode_t{}; if (mode & open_mode::sparse) ret |= file_open_mode::sparse; if (mode & open_mode::no_atime) ret |= file_open_mode::no_atime;