From ec44c68ce643d7aa52dd105400bbdfbba452c2b0 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Thu, 10 May 2018 12:52:01 +1000 Subject: [PATCH] view: require fully named types for casting --- cast.hpp | 12 ++++++++---- json/schema.cpp | 2 +- json/tree.cpp | 2 +- test/hash/checksum.cpp | 4 ++-- test/hash/crc.cpp | 2 +- test/hash/fasthash.cpp | 4 ++-- test/hash/fnv1a.cpp | 4 ++-- tools/json-clean.cpp | 2 +- tools/json-schema.cpp | 4 ++-- tools/json-validate.cpp | 2 +- view.hpp | 11 ++++------- 11 files changed, 25 insertions(+), 24 deletions(-) diff --git a/cast.hpp b/cast.hpp index 9cd5e1f5..4ac27666 100644 --- a/cast.hpp +++ b/cast.hpp @@ -157,14 +157,18 @@ namespace util::cast { /////////////////////////////////////////////////////////////////////////// /// cast a pointer from one type to another, asserting that the required /// alignment of the destination type has been satisfied. - template - DstT* - alignment (SrcT *src) + template < + typename DstT, + typename SrcT, + typename = std::enable_if_t && std::is_pointer_v> + > + DstT + alignment (SrcT src) { #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wcast-align" CHECK_MOD (reinterpret_cast (src), alignof (DstT)); - return reinterpret_cast (src); + return reinterpret_cast (src); #pragma GCC diagnostic pop } }; diff --git a/json/schema.cpp b/json/schema.cpp index 0d2cc46a..47e9eb8d 100644 --- a/json/schema.cpp +++ b/json/schema.cpp @@ -535,6 +535,6 @@ json::schema::validate (json::tree::node &data, const std::experimental::filesystem::path &schema_path) { const util::mapped_file schema_data (schema_path); - auto schema_object = json::tree::parse (util::view(schema_data).cast ()); + auto schema_object = json::tree::parse (util::view(schema_data).cast ()); validate (data, schema_object->as_object ()); } diff --git a/json/tree.cpp b/json/tree.cpp index d41cccbc..81c1bafd 100644 --- a/json/tree.cpp +++ b/json/tree.cpp @@ -242,7 +242,7 @@ std::unique_ptr json::tree::parse (const std::experimental::filesystem::path &src) { const util::mapped_file data (src); - return parse (util::view{data}.cast ()); + return parse (util::view{data}.cast ()); } diff --git a/test/hash/checksum.cpp b/test/hash/checksum.cpp index 2bbcc899..69a5737a 100644 --- a/test/hash/checksum.cpp +++ b/test/hash/checksum.cpp @@ -40,13 +40,13 @@ main (int, char**) { for (const auto &t: TESTS) { tap.expect_eq ( t.adler, - a (util::view {t.data}.template cast ()), + a (util::view {t.data}.template cast ()), "adler checksum: %s", t.msg ); tap.expect_eq ( t.bsd, - b (util::view {t.data}.template cast ()), + b (util::view {t.data}.template cast ()), "bsdsum checksum: %s", t.msg); } diff --git a/test/hash/crc.cpp b/test/hash/crc.cpp index e8d9c110..382929ae 100644 --- a/test/hash/crc.cpp +++ b/test/hash/crc.cpp @@ -34,7 +34,7 @@ main (int, char**) for (const auto &t: TESTS) { #define TEST(KLASS) do { \ - auto computed = util::hash::KLASS{}(util::view {t.dat}.template cast ()); \ + auto computed = util::hash::KLASS{}(util::view {t.dat}.template cast ()); \ tap.expect_eq (t.result.KLASS, computed, "%s: %s", #KLASS, t.msg); \ } while (0) diff --git a/test/hash/fasthash.cpp b/test/hash/fasthash.cpp index 11b8bd44..4cc8efc3 100644 --- a/test/hash/fasthash.cpp +++ b/test/hash/fasthash.cpp @@ -47,8 +47,8 @@ main (void) for (const auto &t: TESTS) { util::view data { t.data }; - tap.expect_eq (t.hash32, h32 (t.seed32, data.cast ()), "h32(%0" PRIx32 ")", t.seed32); - tap.expect_eq (t.hash64, h64 (t.seed64, data.cast ()), "h64(%0" PRIx64 ")", t.seed64); + tap.expect_eq (t.hash32, h32 (t.seed32, data.cast ()), "h32(%0" PRIx32 ")", t.seed32); + tap.expect_eq (t.hash64, h64 (t.seed64, data.cast ()), "h64(%0" PRIx64 ")", t.seed64); } tap.expect (success32, "fasthash32"); diff --git a/test/hash/fnv1a.cpp b/test/hash/fnv1a.cpp index 06ff28ea..ad97795d 100644 --- a/test/hash/fnv1a.cpp +++ b/test/hash/fnv1a.cpp @@ -24,8 +24,8 @@ main (void) const util::hash::fnv1a h64; for (const auto &t: TESTS) { - tap.expect_eq (h32 (util::view{t.data}.cast ()), t.h32, "fnv1a32: '%s'", t.data); - tap.expect_eq (h64 (util::view{t.data}.cast ()), t.h64, "fnv1a64: '%s'", t.data); + tap.expect_eq (h32 (util::view{t.data}.cast ()), t.h32, "fnv1a32: '%s'", t.data); + tap.expect_eq (h64 (util::view{t.data}.cast ()), t.h64, "fnv1a64: '%s'", t.data); } return tap.status (); diff --git a/tools/json-clean.cpp b/tools/json-clean.cpp index 0dd873de..35147193 100644 --- a/tools/json-clean.cpp +++ b/tools/json-clean.cpp @@ -51,7 +51,7 @@ main (int argc, char **argv) try { const util::mapped_file src (argv[ARG_INPUT]); - std::cout << *json::tree::parse (util::view{src}.cast ()) << '\n'; + std::cout << *json::tree::parse (util::view{src}.cast ()) << '\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 cd8e67ce..6e19fe9e 100644 --- a/tools/json-schema.cpp +++ b/tools/json-schema.cpp @@ -50,8 +50,8 @@ main (int argc, char **argv) { const util::mapped_file schema_src (argv[ARG_SCHEMA]); const util::mapped_file input_src (argv[ARG_INPUT]); - auto schema = json::tree::parse (util::view{schema_src}.cast()); - auto input = json::tree::parse (util::view{input_src} .cast()); + auto schema = json::tree::parse (util::view{schema_src}.cast()); + auto input = json::tree::parse (util::view{input_src} .cast()); 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 499d61b9..dfde9099 100644 --- a/tools/json-validate.cpp +++ b/tools/json-validate.cpp @@ -42,7 +42,7 @@ main (int argc, char ** argv) { try { const util::mapped_file data (argv[ARG_PATH]); - json::flat::parse (util::view{data}.cast ()); + json::flat::parse (util::view{data}.cast ()); } catch (const json::error &x) { std::cerr << "error: " << x.what () << '\n'; return EXIT_FAILURE; diff --git a/view.hpp b/view.hpp index 5d9484ec..855e57f9 100644 --- a/view.hpp +++ b/view.hpp @@ -336,18 +336,15 @@ namespace util { template < typename ValueT, typename = std::enable_if_t< - std::is_pointer_v && - sizeof (*std::declval ()) == sizeof (ValueT) - , - void + std::is_pointer_v && std::is_pointer_v > > - view + view cast (void) const { return { - reinterpret_cast (m_begin), - reinterpret_cast (m_end) + reinterpret_cast (m_begin), + reinterpret_cast (m_end) }; }