From 79b7d575324753b0920a1ba4ceab6409ace999e4 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 14 Mar 2018 20:27:11 +0000 Subject: Add a string_view accessor for MemMap --- libadhocutil/fileUtils.h | 8 ++++++++ libadhocutil/unittests/testFileUtils.cpp | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/libadhocutil/fileUtils.h b/libadhocutil/fileUtils.h index 4224643..56ece22 100644 --- a/libadhocutil/fileUtils.h +++ b/libadhocutil/fileUtils.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "visibility.h" namespace AdHoc { @@ -150,6 +151,13 @@ namespace AdHoc { /// The file data. void * const data; + template + auto + sv() const + { + return std::basic_string_view((const T *)data, st.st_size / sizeof(T)); + } + private: DLL_PUBLIC void * setupMapInt(int flags) const; DLL_PUBLIC void * setupMap(int flags) const; diff --git a/libadhocutil/unittests/testFileUtils.cpp b/libadhocutil/unittests/testFileUtils.cpp index 973b9bb..01f1738 100644 --- a/libadhocutil/unittests/testFileUtils.cpp +++ b/libadhocutil/unittests/testFileUtils.cpp @@ -103,6 +103,15 @@ BOOST_AUTO_TEST_CASE( memmap ) BOOST_REQUIRE(f.fh); BOOST_REQUIRE_EQUAL(f.getStat().st_mode, 0100644); BOOST_REQUIRE_EQUAL(0, memcmp(f.data, "#define BOOST_TEST_MODULE FileUtils", 35)); + // String things + auto s = f.sv(); + BOOST_REQUIRE_EQUAL(s.substr(0, 35), "#define BOOST_TEST_MODULE FileUtils"); + BOOST_REQUIRE_EQUAL(s.length(), f.getStat().st_size); + // Objects + auto i = f.sv(); + BOOST_REQUIRE_EQUAL(i[0], *reinterpret_cast("#def")); + BOOST_REQUIRE_EQUAL(i[1], *reinterpret_cast("ine ")); + BOOST_REQUIRE_EQUAL(i.length(), f.getStat().st_size / sizeof(int)); } BOOST_AUTO_TEST_CASE( openmode ) -- cgit v1.2.3