convert file_open_mode to type safe flags

This commit is contained in:
arvidn 2017-07-22 18:59:55 -07:00 committed by Arvid Norberg
parent 140b8ace8d
commit 78bbd298a5
5 changed files with 39 additions and 31 deletions

View File

@ -294,6 +294,7 @@ void bind_converters()
to_python_converter<lt::peer_flags_t, from_bitfield_flag<lt::peer_flags_t>>(); to_python_converter<lt::peer_flags_t, from_bitfield_flag<lt::peer_flags_t>>();
to_python_converter<lt::peer_source_flags_t, from_bitfield_flag<lt::peer_source_flags_t>>(); to_python_converter<lt::peer_source_flags_t, from_bitfield_flag<lt::peer_source_flags_t>>();
to_python_converter<lt::bandwidth_state_flags_t, from_bitfield_flag<lt::bandwidth_state_flags_t>>(); to_python_converter<lt::bandwidth_state_flags_t, from_bitfield_flag<lt::bandwidth_state_flags_t>>();
to_python_converter<lt::file_open_mode_t, from_bitfield_flag<lt::file_open_mode_t>>();
// work-around types // work-around types
to_python_converter<lt::aux::noexcept_movable<lt::address>, address_to_tuple< to_python_converter<lt::aux::noexcept_movable<lt::address>, address_to_tuple<
@ -340,4 +341,5 @@ void bind_converters()
to_bitfield_flag<lt::peer_flags_t>(); to_bitfield_flag<lt::peer_flags_t>();
to_bitfield_flag<lt::peer_source_flags_t>(); to_bitfield_flag<lt::peer_source_flags_t>();
to_bitfield_flag<lt::bandwidth_state_flags_t>(); to_bitfield_flag<lt::bandwidth_state_flags_t>();
to_bitfield_flag<lt::file_open_mode_t>();
} }

View File

@ -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); th.add_piece(piece, data, flags);
} }
class dummy {};
using by_value = return_value_policy<return_by_value>; using by_value = return_value_policy<return_by_value>;
void bind_torrent_handle() void bind_torrent_handle()
{ {
@ -578,16 +580,17 @@ void bind_torrent_handle()
.def_readonly("open_mode", &open_file_state::open_mode) .def_readonly("open_mode", &open_file_state::open_mode)
; ;
enum_<file_open_mode>("file_open_mode") {
.value("read_only", file_open_mode::read_only) scope s = class_<dummy>("file_open_mode");
.value("write_only", file_open_mode::write_only) s.attr("read_only") = file_open_mode::read_only;
.value("read_write", file_open_mode::read_write) s.attr("write_only") = file_open_mode::write_only;
.value("rw_mask", file_open_mode::rw_mask) s.attr("read_write") = file_open_mode::read_write;
.value("sparse", file_open_mode::sparse) s.attr("rw_mask") = file_open_mode::rw_mask;
.value("no_atime", file_open_mode::no_atime) s.attr("sparse") = file_open_mode::sparse;
.value("random_access", file_open_mode::random_access) s.attr("no_atime") = file_open_mode::no_atime;
.value("locked", file_open_mode::locked) s.attr("random_access") = file_open_mode::random_access;
; s.attr("locked") = file_open_mode::locked;
}
enum_<torrent_handle::file_progress_flags_t>("file_progress_flags") enum_<torrent_handle::file_progress_flags_t>("file_progress_flags")
.value("piece_granularity", torrent_handle::piece_granularity) .value("piece_granularity", torrent_handle::piece_granularity)

View File

@ -70,7 +70,6 @@ using lt::total_milliseconds;
using lt::alert; using lt::alert;
using lt::piece_index_t; using lt::piece_index_t;
using lt::file_index_t; using lt::file_index_t;
using lt::file_open_mode;
using lt::torrent_handle; using lt::torrent_handle;
using lt::add_torrent_params; using lt::add_torrent_params;
using lt::cache_status; using lt::cache_status;
@ -1894,12 +1893,12 @@ COLUMN OPTIONS
if (f != file_status.end() && f->file_index == i) if (f != file_status.end() && f->file_index == i)
{ {
title += " [ "; title += " [ ";
if ((f->open_mode & file_open_mode::rw_mask) == file_open_mode::read_write) title += "read/write "; if ((f->open_mode & lt::file_open_mode::rw_mask) == lt::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 & lt::file_open_mode::rw_mask) == lt::file_open_mode::read_only) title += "read ";
else if ((f->open_mode & file_open_mode::rw_mask) == file_open_mode::write_only) title += "write "; else if ((f->open_mode & lt::file_open_mode::rw_mask) == lt::file_open_mode::write_only) title += "write ";
if (f->open_mode & file_open_mode::random_access) title += "random_access "; if (f->open_mode & lt::file_open_mode::random_access) title += "random_access ";
if (f->open_mode & file_open_mode::locked) title += "locked "; if (f->open_mode & lt::file_open_mode::locked) title += "locked ";
if (f->open_mode & file_open_mode::sparse) title += "sparse "; if (f->open_mode & lt::file_open_mode::sparse) title += "sparse ";
title += "]"; title += "]";
++f; ++f;
} }

View File

@ -45,6 +45,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/storage_defs.hpp" #include "libtorrent/storage_defs.hpp"
#include "libtorrent/time.hpp" #include "libtorrent/time.hpp"
#include "libtorrent/sha1_hash.hpp" #include "libtorrent/sha1_hash.hpp"
#include "libtorrent/flags.hpp"
namespace libtorrent { namespace libtorrent {
@ -61,37 +62,40 @@ namespace libtorrent {
struct storage_holder; struct storage_holder;
enum file_open_mode struct file_open_mode_tag;
using file_open_mode_t = flags::bitfield_flag<std::uint32_t, file_open_mode_tag>;
namespace file_open_mode
{ {
// open the file for reading only // open the file for reading only
read_only = 0, constexpr file_open_mode_t read_only{0};
// open the file for writing only // open the file for writing only
write_only = 1, constexpr file_open_mode_t write_only{1};
// open the file for reading and writing // 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 // 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 // open the file in sparse mode (if supported by the
// filesystem). // filesystem).
sparse = 0x4, constexpr file_open_mode_t sparse{0x4};
// don't update the access timestamps on the file (if // don't update the access timestamps on the file (if
// supported by the operating system and filesystem). // supported by the operating system and filesystem).
// this generally improves disk performance. // 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 // open the file for random access. This disables read-ahead
// logic // logic
random_access = 0x10, constexpr file_open_mode_t random_access{0x10};
// prevent the file from being opened by another process // prevent the file from being opened by another process
// while it's still being held open by this handle // 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 // this contains information about a file that's currently open by the
// libtorrent disk I/O subsystem. It's associated with a single torent. // 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 // 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. // 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. // a (high precision) timestamp of when the file was last used.
time_point last_use; time_point last_use;

View File

@ -182,9 +182,9 @@ namespace libtorrent {
namespace { 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; open_mode_t const rw_mode = mode & open_mode::rw_mask;
ret = (rw_mode == open_mode::read_only) ret = (rw_mode == open_mode::read_only)
@ -193,7 +193,7 @@ namespace libtorrent {
? file_open_mode::write_only ? file_open_mode::write_only
: (rw_mode == open_mode::read_write) : (rw_mode == open_mode::read_write)
? file_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::sparse) ret |= file_open_mode::sparse;
if (mode & open_mode::no_atime) ret |= file_open_mode::no_atime; if (mode & open_mode::no_atime) ret |= file_open_mode::no_atime;