From 5ac44ecb0053dd899418ecb172c896e7064b4d49 Mon Sep 17 00:00:00 2001 From: mandreyel Date: Mon, 22 Oct 2018 13:24:32 +0200 Subject: [PATCH] Create invalid_handle value and update unmapped shared_mmap handle methods to return it --- include/mio/detail/basic_mmap.hpp | 6 ++++-- include/mio/mmap.hpp | 4 ++++ include/mio/shared_mmap.hpp | 15 ++++++++++++--- test/test.cpp | 6 ++++-- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/include/mio/detail/basic_mmap.hpp b/include/mio/detail/basic_mmap.hpp index 4582626..50a12ad 100644 --- a/include/mio/detail/basic_mmap.hpp +++ b/include/mio/detail/basic_mmap.hpp @@ -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 struct basic_mmap { using value_type = ByteT; diff --git a/include/mio/mmap.hpp b/include/mio/mmap.hpp index 419509e..46c95d3 100644 --- a/include/mio/mmap.hpp +++ b/include/mio/mmap.hpp @@ -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 diff --git a/include/mio/shared_mmap.hpp b/include/mio/shared_mmap.hpp index 64f6a1b..7dcf74b 100644 --- a/include/mio/shared_mmap.hpp +++ b/include/mio/shared_mmap.hpp @@ -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 diff --git a/test/test.cpp b/test/test.cpp index aacae81..c9ae00b 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -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");