diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2018-03-14 20:27:11 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2018-03-14 20:27:11 +0000 |
commit | 79b7d575324753b0920a1ba4ceab6409ace999e4 (patch) | |
tree | 121a99bb5232501ba6aa18e9d99073a327ebf7dc | |
parent | Updated compile flags (diff) | |
download | libadhocutil-79b7d575324753b0920a1ba4ceab6409ace999e4.tar.bz2 libadhocutil-79b7d575324753b0920a1ba4ceab6409ace999e4.tar.xz libadhocutil-79b7d575324753b0920a1ba4ceab6409ace999e4.zip |
Add a string_view accessor for MemMap
-rw-r--r-- | libadhocutil/fileUtils.h | 8 | ||||
-rw-r--r-- | libadhocutil/unittests/testFileUtils.cpp | 9 |
2 files changed, 17 insertions, 0 deletions
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 <boost/filesystem/path.hpp> #include <sys/stat.h> #include <fcntl.h> +#include <string_view> #include "visibility.h" namespace AdHoc { @@ -150,6 +151,13 @@ namespace AdHoc { /// The file data. void * const data; + template<typename T = char> + auto + sv() const + { + return std::basic_string_view<T>((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<int>(); + BOOST_REQUIRE_EQUAL(i[0], *reinterpret_cast<const int *>("#def")); + BOOST_REQUIRE_EQUAL(i[1], *reinterpret_cast<const int *>("ine ")); + BOOST_REQUIRE_EQUAL(i.length(), f.getStat().st_size / sizeof(int)); } BOOST_AUTO_TEST_CASE( openmode ) |