Create invalid_handle value and update unmapped shared_mmap handle methods to return it

This commit is contained in:
mandreyel 2018-10-22 13:24:32 +02:00
parent 2dbf5d3050
commit 5ac44ecb00
4 changed files with 24 additions and 7 deletions

View File

@ -42,11 +42,13 @@ namespace detail {
enum { map_entire_file = 0 };
#ifdef _WIN32
using file_handle_type = HANDLE;
using file_handle_type = HANDLE;
#else
using file_handle_type = int;
using file_handle_type = int;
#endif
constexpr static file_handle_type invalid_handle = INVALID_HANDLE_VALUE;
template<typename ByteT> struct basic_mmap
{
using value_type = ByteT;

View File

@ -32,6 +32,10 @@ namespace mio {
// `map`, in which case a memory mapping of the entire file is created.
using detail::map_entire_file;
// This value represents an invalid file handle type. This can be used to
// determine whether `basic_mmap::file_handle` is valid, for example.
using detail::invalid_handle;
template<
access_mode AccessMode,
typename ByteT

View File

@ -120,8 +120,15 @@ public:
* however, a mapped region of a file gets its own handle, which is returned by
* 'mapping_handle'.
*/
handle_type file_handle() const noexcept { return pimpl_->file_handle(); }
handle_type mapping_handle() const noexcept { return pimpl_->mapping_handle(); }
handle_type file_handle() const noexcept
{
return pimpl_ ? pimpl_->file_handle() : invalid_handle;
}
handle_type mapping_handle() const noexcept
{
return pimpl_ ? pimpl_->mapping_handle() : invalid_handle;
}
/** Returns whether a valid memory mapping has been created. */
bool is_open() const noexcept { return pimpl_ && pimpl_->is_open(); }
@ -142,7 +149,9 @@ public:
size_type size() const noexcept { return pimpl_ ? pimpl_->length() : 0; }
size_type length() const noexcept { return pimpl_ ? pimpl_->length() : 0; }
size_type mapped_length() const noexcept
{ return pimpl_ ? pimpl_->mapped_length() : 0; }
{
return pimpl_ ? pimpl_->mapped_length() : 0;
}
/**
* Returns the offset, relative to the file's start, at which the mapping was

View File

@ -89,8 +89,7 @@ int main()
CHECK_INVALID_MMAP(m);
// Invalid handle?
m = mio::make_mmap_source(
INVALID_HANDLE_VALUE/*Psst... This is an implementation detail!*/, 0, 0, error);
m = mio::make_mmap_source(mio::invalid_handle, 0, 0, error);
CHECK_INVALID_MMAP(m);
// Invalid offset?
@ -102,6 +101,9 @@ int main()
// Make sure custom types compile.
mio::ummap_source _1;
mio::shared_ummap_source _2;
// Make sure shared_mmap mapping compiles as all testing was done on
// normal mmaps.
mio::shared_mmap_source _3(path, 0, mio::map_entire_file);
}
std::printf("all tests passed!\n");