From bf5cab61561ef114ea8207e0f95de55e2ff2ca3a Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 28 Jun 2016 15:58:41 +1000 Subject: [PATCH] io: move view conversion operator to named function --- io_posix.hpp | 11 +++++++++-- io_posix.ipp | 16 ++++++++++------ io_win32.hpp | 9 +++++++-- io_win32.ipp | 16 ++++++++++------ tools/json-clean.cpp | 17 ++++++++++++----- tools/json-schema.cpp | 9 +++++++-- tools/json-validate.cpp | 4 ++-- 7 files changed, 57 insertions(+), 25 deletions(-) diff --git a/io_posix.hpp b/io_posix.hpp index 7af80da6..c443b3f4 100644 --- a/io_posix.hpp +++ b/io_posix.hpp @@ -21,6 +21,8 @@ #include "view.hpp" +#include + #include #include @@ -50,8 +52,13 @@ 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> () &; + template + util::view*> + as_view () const &; + + template + util::view + as_view () &; private: fd m_fd; diff --git a/io_posix.ipp b/io_posix.ipp index d101f5bf..d6814682 100644 --- a/io_posix.ipp +++ b/io_posix.ipp @@ -20,24 +20,28 @@ #define __UTIL_IO_POSIX_IPP +#include "./pointer.hpp" + /////////////////////////////////////////////////////////////////////////////// template -util::detail::posix::mapped_file::operator util::view () const & +util::view*> +util::detail::posix::mapped_file::as_view (void) const& { return { - reinterpret_cast (cbegin ()), - reinterpret_cast (cend ()) + reinterpret_cast (cbegin ()), + reinterpret_cast (align (cend (), alignof (T))) }; } //----------------------------------------------------------------------------- template -util::detail::posix::mapped_file::operator util::view () & +util::view +util::detail::posix::mapped_file::as_view (void) & { return { - reinterpret_cast (begin ()), - reinterpret_cast (end ()) + reinterpret_cast (begin ()), + reinterpret_cast (align (end (), alignof(T))) }; } diff --git a/io_win32.hpp b/io_win32.hpp index 73b69c4e..bd3e6c7c 100644 --- a/io_win32.hpp +++ b/io_win32.hpp @@ -60,8 +60,13 @@ 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> () &; + template + util::view*> + as_view () const &; + + template + util::view + as_view () &; private: ::util::win32::handle m_file; diff --git a/io_win32.ipp b/io_win32.ipp index 3e5d9d3a..ce4b53a6 100644 --- a/io_win32.ipp +++ b/io_win32.ipp @@ -21,24 +21,28 @@ #define __UTIL_IO_WIN32_IPP +#include "pointer.hpp" + /////////////////////////////////////////////////////////////////////////////// template -util::detail::win32::mapped_file::operator util::view () const & +util::view*> +util::detail::win32::mapped_file::as_view (void) const& { return { - reinterpret_cast (cbegin ()), - reinterpret_cast (cend ()) + reinterpret_cast (cbegin ()), + reinterpret_cast (align (cend (), alignof (T))) }; } //----------------------------------------------------------------------------- template -util::detail::win32::mapped_file::operator util::view () & +util::view +util::detail::win32::mapped_file::as_view (void) & { return { - reinterpret_cast (begin ()), - reinterpret_cast (end ()) + reinterpret_cast (begin ()), + reinterpret_cast (align (end (), alignof(T))) }; } diff --git a/tools/json-clean.cpp b/tools/json-clean.cpp index 69abbba5..d65e36be 100644 --- a/tools/json-clean.cpp +++ b/tools/json-clean.cpp @@ -11,9 +11,10 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2012 Danny Robson + * Copyright 2012-2016 Danny Robson */ +#include "io.hpp" #include "json/except.hpp" #include "json/tree.hpp" @@ -22,6 +23,8 @@ #include + +/////////////////////////////////////////////////////////////////////////////// enum { ARG_COMMAND, ARG_INPUT, @@ -30,23 +33,27 @@ enum { }; +//----------------------------------------------------------------------------- void -print_usage (int argc, char **argv) { +print_usage (int argc, char **argv) +{ (void)argc; std::cerr << "usage: " << argv[0] << " \n"; } +//----------------------------------------------------------------------------- int -main (int argc, char **argv) { +main (int argc, char **argv) +{ if (argc != NUM_ARGS) { print_usage (argc, argv); return EXIT_FAILURE; } try { - boost::filesystem::path input (argv[ARG_INPUT]); - std::cout << *json::tree::parse (input) << "\n"; + const util::mapped_file src (argv[ARG_INPUT]); + std::cout << *json::tree::parse (src.as_view ()) << '\n'; } catch (const json::error& err) { std::cerr << err.what () << "\n"; return EXIT_FAILURE; diff --git a/tools/json-schema.cpp b/tools/json-schema.cpp index cb143f22..0fce2853 100644 --- a/tools/json-schema.cpp +++ b/tools/json-schema.cpp @@ -20,6 +20,8 @@ #include "json/schema.hpp" #include "json/except.hpp" +#include "io.hpp" + #include namespace fs = boost::filesystem; @@ -41,8 +43,11 @@ main (int argc, char **argv) { } try { - auto schema = json::tree::parse (fs::path (argv[ARG_SCHEMA])); - auto input = json::tree::parse (fs::path (argv[ARG_INPUT])); + const util::mapped_file schema_src (argv[ARG_SCHEMA]); + const util::mapped_file input_src (argv[ARG_INPUT]); + + auto schema = json::tree::parse (schema_src.as_view ()); + auto input = json::tree::parse (input_src.as_view ()); json::schema::validate (*input, schema->as_object ()); } catch (const json::error &e) { diff --git a/tools/json-validate.cpp b/tools/json-validate.cpp index ec173037..7fe9d097 100644 --- a/tools/json-validate.cpp +++ b/tools/json-validate.cpp @@ -42,8 +42,8 @@ main (int argc, char ** argv) { } try { - util::mapped_file data (argv[ARG_PATH]); - json::flat::parse (data.operator util::view ()); + const util::mapped_file data (argv[ARG_PATH]); + json::flat::parse (data.as_view ()); } catch (const json::error &x) { std::cerr << "error: " << x.what () << '\n'; return EXIT_FAILURE;