From e65e55e5ba7dfb0cbfa23f052a13e3addd3b3897 Mon Sep 17 00:00:00 2001 From: Lexi Mayfield Date: Sun, 31 Dec 2023 17:44:01 -0500 Subject: [PATCH] Add && qualified methods to buffers --- include/lexio/bufreader.hpp | 24 ++++++++++++++++-------- include/lexio/bufwriter.hpp | 11 ++++++++++- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/include/lexio/bufreader.hpp b/include/lexio/bufreader.hpp index cf2b6b1..0bb55f9 100644 --- a/include/lexio/bufreader.hpp +++ b/include/lexio/bufreader.hpp @@ -71,11 +71,10 @@ class GenericBufReader std::copy(&other.m_buffer[0], &other.m_buffer[m_size], m_buffer); } - GenericBufReader(GenericBufReader &&other) - : m_wrapped(std::move(other.m_wrapped)), m_buffer(other.m_buffer), m_allocSize(other.m_allocSize), - m_size(other.m_size) + GenericBufReader(GenericBufReader &&other) noexcept + : m_wrapped(std::move(other.m_wrapped)), m_buffer(std::exchange(other.m_buffer, nullptr)), + m_allocSize(other.m_allocSize), m_size(other.m_size) { - other.m_buffer = nullptr; } GenericBufReader(READER &&wrapped) : m_wrapped(wrapped) {} @@ -98,20 +97,29 @@ class GenericBufReader GenericBufReader &operator=(GenericBufReader &&other) noexcept { - ::delete[] m_buffer; + if (this == &other) + { + return *this; + } + ::delete[] m_buffer; m_wrapped = std::move(other.m_wrapped); - m_buffer = other.m_buffer; + m_buffer = std::exchange(other.m_buffer, nullptr); m_allocSize = other.m_allocSize; m_size = other.m_size; - other.m_buffer = nullptr; return *this; } /** * @brief Return underlying Reader. */ - const READER &Reader() const { return m_wrapped; } + const READER &Reader() const & { return m_wrapped; } + + /** + * @brief Obtain the underlying wrapped Reader while moving-from the + * GenericBufReader. + */ + READER Reader() && { return m_wrapped; } size_t LexRead(uint8_t *outDest, const size_t count) { diff --git a/include/lexio/bufwriter.hpp b/include/lexio/bufwriter.hpp index c728e62..446648d 100644 --- a/include/lexio/bufwriter.hpp +++ b/include/lexio/bufwriter.hpp @@ -59,7 +59,16 @@ class FixedBufWriter ::delete[] m_buffer; } - WRITER &&Writer() && { return std::move(m_wrapped); } + /** + * @brief Return underlying Writer. + */ + const WRITER &Writer() const & { return m_wrapped; } + + /** + * @brief Obtain the underlying wrapped Writer while moving-from the + * FixedBufWriter. + */ + WRITER Writer() && { return m_wrapped; } template >> size_t LexRead(uint8_t *outDest, const size_t count)