diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2019-07-01 20:49:25 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2019-07-01 20:49:25 +0100 |
commit | 6ba9774460e35a1f8933c3192b39244ddec9ad55 (patch) | |
tree | 86b97c424590d96c96591f40ce0388a3a2d4c122 | |
parent | Update Doxyfile and remove backup (diff) | |
download | libadhocutil-6ba9774460e35a1f8933c3192b39244ddec9ad55.tar.bz2 libadhocutil-6ba9774460e35a1f8933c3192b39244ddec9ad55.tar.xz libadhocutil-6ba9774460e35a1f8933c3192b39244ddec9ad55.zip |
MemStream error handling and move supportlibadhocutil-0.7.5
-rw-r--r-- | libadhocutil/memstream.cpp | 41 | ||||
-rw-r--r-- | libadhocutil/memstream.h | 6 | ||||
-rw-r--r-- | libadhocutil/unittests/testMemStream.cpp | 13 |
3 files changed, 57 insertions, 3 deletions
diff --git a/libadhocutil/memstream.cpp b/libadhocutil/memstream.cpp index a4ce95e..ec4b1bb 100644 --- a/libadhocutil/memstream.cpp +++ b/libadhocutil/memstream.cpp @@ -1,5 +1,8 @@ #include "memstream.h" #include <cstdlib> +#include <cerrno> +#include <cstring> +#include <sys.h> namespace AdHoc { MemStream::MemStream() : @@ -7,13 +10,49 @@ namespace AdHoc { len(0), strm(open_memstream(&buf, &len)) { + if (!strm) { + // LCOV_EXCL_START no sensible way to make open_memstream fail + throw SystemException("open_memstream failed", strerror(errno), errno); + // LCOV_EXCL_STOP + } + } + + MemStream::MemStream(MemStream && o) noexcept : + buf(o.buf), + len(o.len), + strm(o.strm) + { + o.buf = nullptr; + o.len = 0; + o.strm = nullptr; } MemStream::~MemStream() { - fclose(strm); + if (strm) { + fclose(strm); + } + // NOLINTNEXTLINE(hicpp-no-malloc) + free(buf); + } + + MemStream & MemStream::operator=(MemStream && o) noexcept + { + if (strm) { + fclose(strm); + } // NOLINTNEXTLINE(hicpp-no-malloc) free(buf); + + buf = o.buf; + len = o.len; + strm = o.strm; + + o.buf = nullptr; + o.len = 0; + o.strm = nullptr; + + return *this; } MemStream::operator FILE * () diff --git a/libadhocutil/memstream.h b/libadhocutil/memstream.h index 20b65fc..ba5f1fc 100644 --- a/libadhocutil/memstream.h +++ b/libadhocutil/memstream.h @@ -15,10 +15,12 @@ namespace AdHoc { ~MemStream(); MemStream(const MemStream &) = delete; - MemStream(MemStream &&) = delete; + /// Standard move constructor + MemStream(MemStream &&) noexcept; void operator=(const MemStream &) = delete; - void operator=(MemStream &&) = delete; + /// Standard move assignment + MemStream & operator=(MemStream &&) noexcept; /// Implicit conversion to use as FILE* for writes operator FILE * (); diff --git a/libadhocutil/unittests/testMemStream.cpp b/libadhocutil/unittests/testMemStream.cpp index b244b5e..79100ee 100644 --- a/libadhocutil/unittests/testMemStream.cpp +++ b/libadhocutil/unittests/testMemStream.cpp @@ -32,3 +32,16 @@ BOOST_AUTO_TEST_CASE(simple) BOOST_AUTO_TEST_SUITE_END(); +BOOST_AUTO_TEST_CASE(move_construct) +{ + MemStream ms1; + MemStream ms2(std::move(ms1)); +} + +BOOST_AUTO_TEST_CASE(move_assign) +{ + MemStream ms1; + MemStream ms2; + ms2 = std::move(ms1); +} + |