io: add view operator to mapped_file

This commit is contained in:
Danny Robson 2016-06-28 14:15:19 +10:00
parent 8c5c98f5cc
commit 194cd6c574
5 changed files with 107 additions and 1 deletions

View File

@ -289,6 +289,7 @@ POSIX_FILES = \
debug_posix.cpp \
io_posix.cpp \
io_posix.hpp \
io_posix.ipp \
library_posix.hpp \
library_posix.cpp \
posix/dir.cpp \
@ -309,6 +310,8 @@ if PLATFORM_WIN32
UTIL_FILES += \
debug_win32.cpp \
io_win32.cpp \
io_win32.hpp \
io_win32.ipp \
library_win32.hpp \
library_win32.cpp \
time_win32.cpp \

View File

@ -19,6 +19,8 @@
#include "io.hpp"
#include "view.hpp"
#include <sys/mman.h>
#include <fcntl.h>
@ -48,6 +50,9 @@ namespace util {
const uint8_t* cbegin (void) const;
const uint8_t* cend (void) const;
template <typename T> operator util::view<const T *restrict> () const &;
template <typename T> operator util::view< T *restrict> () &;
private:
fd m_fd;
uint8_t *m_data;
@ -60,4 +65,6 @@ namespace util {
typedef detail::posix::mapped_file mapped_file;
}
#include "io_posix.ipp"
#endif

43
io_posix.ipp Normal file
View File

@ -0,0 +1,43 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2016 Danny Robson <danny@nerdcruft.net>
*/
#ifdef __UTIL_IO_POSIX_IPP
#error
#endif
#define __UTIL_IO_POSIX_IPP
///////////////////////////////////////////////////////////////////////////////
template <typename T>
util::detail::posix::mapped_file::operator util::view<const T *restrict> () const &
{
return {
reinterpret_cast<const T*restrict> (cbegin ()),
reinterpret_cast<const T*restrict> (cend ())
};
}
//-----------------------------------------------------------------------------
template <typename T>
util::detail::posix::mapped_file::operator util::view<T *restrict> () &
{
return {
reinterpret_cast<T* restrict> (begin ()),
reinterpret_cast<T* restrict> (end ())
};
}

View File

@ -17,7 +17,8 @@
#ifndef __UTIL_IO_WIN32_HPP
#define __UTIL_IO_WIN32_HPP
#include "win32/handle.hpp"
#include "./win32/handle.hpp"
#include "./view.hpp"
#include <cstdint>
#include <boost/filesystem/path.hpp>
@ -59,6 +60,12 @@ namespace util {
const uint8_t* cbegin (void) const;
const uint8_t* cend (void) const;
template <typename T>
operator util::view<const T *restrict> () const &;
template <typename T>
operator util::view<T *restrict> () &;
private:
::util::win32::handle m_file;
::util::win32::handle m_mapping;
@ -71,4 +78,6 @@ namespace util {
typedef detail::win32::mapped_file mapped_file;
}
#include "io_win32.ipp"
#endif

44
io_win32.ipp Normal file
View File

@ -0,0 +1,44 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2016 Danny Robson <danny@nerdcruft.net>
*/
#ifdef __UTIL_IO_WIN32_IPP
#error
#endif
#define __UTIL_IO_WIN32_IPP
///////////////////////////////////////////////////////////////////////////////
template <typename T>
util::detail::win32::mapped_file::operator util::view<const T *restrict> () const &
{
return {
reinterpret_cast<const T*restrict> (cbegin ()),
reinterpret_cast<const T*restrict> (cend ())
};
}
//-----------------------------------------------------------------------------
template <typename T>
util::detail::win32::mapped_file::operator util::view<T *restrict> () &
{
return {
reinterpret_cast<T* restrict> (begin ()),
reinterpret_cast<T* restrict> (end ())
};
}