From 194cd6c5741aeeeeab5590bb5cf9b1ea66c53475 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 28 Jun 2016 14:15:19 +1000 Subject: [PATCH] io: add view operator to mapped_file --- Makefile.am | 3 +++ io_posix.hpp | 7 +++++++ io_posix.ipp | 43 +++++++++++++++++++++++++++++++++++++++++++ io_win32.hpp | 11 ++++++++++- io_win32.ipp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 io_posix.ipp create mode 100644 io_win32.ipp diff --git a/Makefile.am b/Makefile.am index 9a828133..dbcdd166 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/io_posix.hpp b/io_posix.hpp index 7ccc279d..4f3e12ee 100644 --- a/io_posix.hpp +++ b/io_posix.hpp @@ -19,6 +19,8 @@ #include "io.hpp" +#include "view.hpp" + #include #include @@ -48,6 +50,9 @@ namespace util { const uint8_t* cbegin (void) const; const uint8_t* cend (void) const; + template operator util::view () const &; + template 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 diff --git a/io_posix.ipp b/io_posix.ipp new file mode 100644 index 00000000..d101f5bf --- /dev/null +++ b/io_posix.ipp @@ -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 + */ + +#ifdef __UTIL_IO_POSIX_IPP +#error +#endif + +#define __UTIL_IO_POSIX_IPP + + +/////////////////////////////////////////////////////////////////////////////// +template +util::detail::posix::mapped_file::operator util::view () const & +{ + return { + reinterpret_cast (cbegin ()), + reinterpret_cast (cend ()) + }; +} + + +//----------------------------------------------------------------------------- +template +util::detail::posix::mapped_file::operator util::view () & +{ + return { + reinterpret_cast (begin ()), + reinterpret_cast (end ()) + }; +} diff --git a/io_win32.hpp b/io_win32.hpp index 907882fe..43f0ce67 100644 --- a/io_win32.hpp +++ b/io_win32.hpp @@ -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 #include @@ -59,6 +60,12 @@ namespace util { const uint8_t* cbegin (void) const; const uint8_t* cend (void) const; + template + operator util::view () const &; + + template + operator util::view () &; + 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 diff --git a/io_win32.ipp b/io_win32.ipp new file mode 100644 index 00000000..3e5d9d3a --- /dev/null +++ b/io_win32.ipp @@ -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 + */ + + +#ifdef __UTIL_IO_WIN32_IPP +#error +#endif + +#define __UTIL_IO_WIN32_IPP + + +/////////////////////////////////////////////////////////////////////////////// +template +util::detail::win32::mapped_file::operator util::view () const & +{ + return { + reinterpret_cast (cbegin ()), + reinterpret_cast (cend ()) + }; +} + + +//----------------------------------------------------------------------------- +template +util::detail::win32::mapped_file::operator util::view () & +{ + return { + reinterpret_cast (begin ()), + reinterpret_cast (end ()) + }; +}