Skip to content

Commit

Permalink
Add && qualified methods to buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexMax committed Dec 31, 2023
1 parent 3bcafad commit e65e55e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
24 changes: 16 additions & 8 deletions include/lexio/bufreader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
Expand All @@ -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)
{
Expand Down
11 changes: 10 additions & 1 deletion include/lexio/bufwriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename READER = WRITER, typename = std::enable_if_t<IsReaderV<READER>>>
size_t LexRead(uint8_t *outDest, const size_t count)
Expand Down

0 comments on commit e65e55e

Please sign in to comment.