view: require fully named types for casting

This commit is contained in:
Danny Robson 2018-05-10 12:52:01 +10:00
parent 29702704fb
commit ec44c68ce6
11 changed files with 25 additions and 24 deletions

View File

@ -157,14 +157,18 @@ namespace util::cast {
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
/// cast a pointer from one type to another, asserting that the required /// cast a pointer from one type to another, asserting that the required
/// alignment of the destination type has been satisfied. /// alignment of the destination type has been satisfied.
template <typename DstT, typename SrcT> template <
DstT* typename DstT,
alignment (SrcT *src) typename SrcT,
typename = std::enable_if_t<std::is_pointer_v<DstT> && std::is_pointer_v<SrcT>>
>
DstT
alignment (SrcT src)
{ {
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-align" #pragma GCC diagnostic ignored "-Wcast-align"
CHECK_MOD (reinterpret_cast<uintptr_t> (src), alignof (DstT)); CHECK_MOD (reinterpret_cast<uintptr_t> (src), alignof (DstT));
return reinterpret_cast<DstT*> (src); return reinterpret_cast<DstT> (src);
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
} }
}; };

View File

@ -535,6 +535,6 @@ json::schema::validate (json::tree::node &data,
const std::experimental::filesystem::path &schema_path) const std::experimental::filesystem::path &schema_path)
{ {
const util::mapped_file schema_data (schema_path); const util::mapped_file schema_data (schema_path);
auto schema_object = json::tree::parse (util::view(schema_data).cast<const char> ()); auto schema_object = json::tree::parse (util::view(schema_data).cast<const char*> ());
validate (data, schema_object->as_object ()); validate (data, schema_object->as_object ());
} }

View File

@ -242,7 +242,7 @@ std::unique_ptr<json::tree::node>
json::tree::parse (const std::experimental::filesystem::path &src) json::tree::parse (const std::experimental::filesystem::path &src)
{ {
const util::mapped_file data (src); const util::mapped_file data (src);
return parse (util::view{data}.cast<const char> ()); return parse (util::view{data}.cast<const char*> ());
} }

View File

@ -40,13 +40,13 @@ main (int, char**) {
for (const auto &t: TESTS) { for (const auto &t: TESTS) {
tap.expect_eq ( tap.expect_eq (
t.adler, t.adler,
a (util::view {t.data}.template cast<const uint8_t> ()), a (util::view {t.data}.template cast<const uint8_t*> ()),
"adler checksum: %s", t.msg "adler checksum: %s", t.msg
); );
tap.expect_eq ( tap.expect_eq (
t.bsd, t.bsd,
b (util::view {t.data}.template cast<const uint8_t> ()), b (util::view {t.data}.template cast<const uint8_t*> ()),
"bsdsum checksum: %s", t.msg); "bsdsum checksum: %s", t.msg);
} }

View File

@ -34,7 +34,7 @@ main (int, char**)
for (const auto &t: TESTS) { for (const auto &t: TESTS) {
#define TEST(KLASS) do { \ #define TEST(KLASS) do { \
auto computed = util::hash::KLASS{}(util::view {t.dat}.template cast<const uint8_t> ()); \ auto computed = util::hash::KLASS{}(util::view {t.dat}.template cast<const uint8_t*> ()); \
tap.expect_eq (t.result.KLASS, computed, "%s: %s", #KLASS, t.msg); \ tap.expect_eq (t.result.KLASS, computed, "%s: %s", #KLASS, t.msg); \
} while (0) } while (0)

View File

@ -47,8 +47,8 @@ main (void)
for (const auto &t: TESTS) { for (const auto &t: TESTS) {
util::view data { t.data }; util::view data { t.data };
tap.expect_eq (t.hash32, h32 (t.seed32, data.cast<uint8_t const> ()), "h32(%0" PRIx32 ")", t.seed32); tap.expect_eq (t.hash32, h32 (t.seed32, data.cast<uint8_t const*> ()), "h32(%0" PRIx32 ")", t.seed32);
tap.expect_eq (t.hash64, h64 (t.seed64, data.cast<uint8_t const> ()), "h64(%0" PRIx64 ")", t.seed64); tap.expect_eq (t.hash64, h64 (t.seed64, data.cast<uint8_t const*> ()), "h64(%0" PRIx64 ")", t.seed64);
} }
tap.expect (success32, "fasthash32"); tap.expect (success32, "fasthash32");

View File

@ -24,8 +24,8 @@ main (void)
const util::hash::fnv1a<uint64_t> h64; const util::hash::fnv1a<uint64_t> h64;
for (const auto &t: TESTS) { for (const auto &t: TESTS) {
tap.expect_eq (h32 (util::view{t.data}.cast <const uint8_t> ()), t.h32, "fnv1a32: '%s'", t.data); tap.expect_eq (h32 (util::view{t.data}.cast <const uint8_t*> ()), t.h32, "fnv1a32: '%s'", t.data);
tap.expect_eq (h64 (util::view{t.data}.cast <const uint8_t> ()), t.h64, "fnv1a64: '%s'", t.data); tap.expect_eq (h64 (util::view{t.data}.cast <const uint8_t*> ()), t.h64, "fnv1a64: '%s'", t.data);
} }
return tap.status (); return tap.status ();

View File

@ -51,7 +51,7 @@ main (int argc, char **argv)
try { try {
const util::mapped_file src (argv[ARG_INPUT]); const util::mapped_file src (argv[ARG_INPUT]);
std::cout << *json::tree::parse (util::view{src}.cast<const char> ()) << '\n'; std::cout << *json::tree::parse (util::view{src}.cast<const char*> ()) << '\n';
} catch (const json::error& err) { } catch (const json::error& err) {
std::cerr << err.what () << "\n"; std::cerr << err.what () << "\n";
return EXIT_FAILURE; return EXIT_FAILURE;

View File

@ -50,8 +50,8 @@ main (int argc, char **argv) {
const util::mapped_file schema_src (argv[ARG_SCHEMA]); const util::mapped_file schema_src (argv[ARG_SCHEMA]);
const util::mapped_file input_src (argv[ARG_INPUT]); const util::mapped_file input_src (argv[ARG_INPUT]);
auto schema = json::tree::parse (util::view{schema_src}.cast<const char>()); auto schema = json::tree::parse (util::view{schema_src}.cast<const char*>());
auto input = json::tree::parse (util::view{input_src} .cast<const char>()); auto input = json::tree::parse (util::view{input_src} .cast<const char*>());
json::schema::validate (*input, schema->as_object ()); json::schema::validate (*input, schema->as_object ());
} catch (const json::error &e) { } catch (const json::error &e) {

View File

@ -42,7 +42,7 @@ main (int argc, char ** argv) {
try { try {
const util::mapped_file data (argv[ARG_PATH]); const util::mapped_file data (argv[ARG_PATH]);
json::flat::parse (util::view{data}.cast<const char> ()); json::flat::parse (util::view{data}.cast<const char*> ());
} catch (const json::error &x) { } catch (const json::error &x) {
std::cerr << "error: " << x.what () << '\n'; std::cerr << "error: " << x.what () << '\n';
return EXIT_FAILURE; return EXIT_FAILURE;

View File

@ -336,18 +336,15 @@ namespace util {
template < template <
typename ValueT, typename ValueT,
typename = std::enable_if_t< typename = std::enable_if_t<
std::is_pointer_v<BeginT> && std::is_pointer_v<BeginT> && std::is_pointer_v<ValueT>
sizeof (*std::declval<BeginT> ()) == sizeof (ValueT)
,
void
> >
> >
view<ValueT*> view<ValueT>
cast (void) const cast (void) const
{ {
return { return {
reinterpret_cast<ValueT*> (m_begin), reinterpret_cast<ValueT> (m_begin),
reinterpret_cast<ValueT*> (m_end) reinterpret_cast<ValueT> (m_end)
}; };
} }