Skip to content

Commit

Permalink
libmbbootimg: Kill Moved state in Reader and Writer
Browse files Browse the repository at this point in the history
It's easier to simply make Reader/Writer behave as if it was freshly
initialized.

Signed-off-by: Andrew Gunnerson <[email protected]>
  • Loading branch information
chenxiaolong committed Nov 27, 2018
1 parent 94b160b commit aa1efee
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 38 deletions.
1 change: 0 additions & 1 deletion libmbbootimg/include/mbbootimg/reader_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ enum class ReaderState : uint8_t
Header = 1u << 2,
Entry = 1u << 3,
Data = 1u << 4,
Moved = 1u << 5,
};
MB_DECLARE_FLAGS(ReaderStates, ReaderState)
MB_DECLARE_OPERATORS_FOR_FLAGS(ReaderStates)
Expand Down
1 change: 0 additions & 1 deletion libmbbootimg/include/mbbootimg/writer_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ enum class WriterState : uint8_t
Header = 1u << 2,
Entry = 1u << 3,
Data = 1u << 4,
Moved = 1u << 5,
};
MB_DECLARE_FLAGS(WriterStates, WriterState)
MB_DECLARE_OPERATORS_FOR_FLAGS(WriterStates)
Expand Down
42 changes: 21 additions & 21 deletions libmbbootimg/src/reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,28 +233,32 @@ Reader::~Reader()
}

Reader::Reader(Reader &&other) noexcept
: m_state(other.m_state)
, m_owned_file(std::move(other.m_owned_file))
, m_file(other.m_file)
, m_formats(std::move(other.m_formats))
, m_format(other.m_format)
, m_format_user_set(other.m_format_user_set)
: Reader()
{
other.m_state = ReaderState::Moved;
std::swap(m_state, other.m_state);
std::swap(m_owned_file, other.m_owned_file);
std::swap(m_file, other.m_file);
std::swap(m_formats, other.m_formats);
std::swap(m_format, other.m_format);
std::swap(m_format_user_set, other.m_format_user_set);
}

Reader & Reader::operator=(Reader &&rhs) noexcept
{
(void) close();

m_state = rhs.m_state;
m_owned_file.swap(rhs.m_owned_file);
m_file = rhs.m_file;
m_formats.swap(rhs.m_formats);
m_format = rhs.m_format;
m_format_user_set = rhs.m_format_user_set;

rhs.m_state = ReaderState::Moved;
if (this != &rhs) {
(void) close();

// close() only resets the autodetected format
m_formats.clear();
m_format = nullptr;

std::swap(m_state, rhs.m_state);
std::swap(m_owned_file, rhs.m_owned_file);
std::swap(m_file, rhs.m_file);
std::swap(m_formats, rhs.m_formats);
std::swap(m_format, rhs.m_format);
std::swap(m_format_user_set, rhs.m_format_user_set);
}

return *this;
}
Expand Down Expand Up @@ -399,8 +403,6 @@ oc::result<void> Reader::open(File *file)
*/
oc::result<void> Reader::close()
{
ENSURE_STATE_OR_RETURN_ERROR(~ReaderStates(ReaderState::Moved));

auto reset_state = finally([&] {
m_state = ReaderState::New;

Expand Down Expand Up @@ -521,8 +523,6 @@ oc::result<size_t> Reader::read_data(void *buf, size_t size)
*/
std::optional<Format> Reader::format()
{
ENSURE_STATE_OR_RETURN(~ReaderStates(ReaderState::Moved), std::nullopt);

if (!m_format) {
return std::nullopt;
}
Expand Down
26 changes: 11 additions & 15 deletions libmbbootimg/src/writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,24 +245,24 @@ Writer::~Writer()
}

Writer::Writer(Writer &&other) noexcept
: m_state(other.m_state)
, m_owned_file(std::move(other.m_owned_file))
, m_file(other.m_file)
, m_format(std::move(other.m_format))
: Writer()
{
other.m_state = WriterState::Moved;
*this = std::move(other);
}

Writer & Writer::operator=(Writer &&rhs) noexcept
{
(void) close();
if (this != &rhs) {
(void) close();

m_state = rhs.m_state;
m_owned_file.swap(rhs.m_owned_file);
m_file = rhs.m_file;
m_format.swap(rhs.m_format);
// close() keeps the selected format
m_format.reset();

rhs.m_state = WriterState::Moved;
std::swap(m_state, rhs.m_state);
std::swap(m_owned_file, rhs.m_owned_file);
std::swap(m_file, rhs.m_file);
std::swap(m_format, rhs.m_format);
}

return *this;
}
Expand Down Expand Up @@ -373,8 +373,6 @@ oc::result<void> Writer::open(File *file)
*/
oc::result<void> Writer::close()
{
ENSURE_STATE_OR_RETURN_ERROR(~WriterStates(WriterState::Moved));

auto reset_state = finally([&] {
m_state = WriterState::New;

Expand Down Expand Up @@ -514,8 +512,6 @@ oc::result<size_t> Writer::write_data(const void *buf, size_t size)
*/
std::optional<Format> Writer::format()
{
ENSURE_STATE_OR_RETURN(~WriterStates(WriterState::Moved), std::nullopt);

if (!m_format) {
return std::nullopt;
}
Expand Down

0 comments on commit aa1efee

Please sign in to comment.