Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/core/openjph/ojph_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,10 @@ namespace ojph {

private:

/** @brief A utility to set an instance's fields to default values.
*
* This function is used in the default constructor as well as
* move constructor and assignment.
/**
* @brief A utility function to swap the contents of two instances
*/
static void reset(mem_outfile&) noexcept;
void swap(mem_outfile& other) noexcept;

/**
* @brief This function expands storage by x1.5 needed space.
Expand Down Expand Up @@ -313,6 +311,20 @@ namespace ojph {
mem_infile() { close(); }
~mem_infile() override { }

mem_infile(mem_infile const&) = delete;
mem_infile& operator=(mem_infile const&) = delete;

/**
* Move construction leaves the moved-from value in default constructed state
* and transfers ownership of the internal state to the moved-to instance.
**/
mem_infile(mem_infile &&) noexcept;
/**
* move assignment with the same ownership transfer semantics as
* move construction.
**/
mem_infile& operator=(mem_infile&&) noexcept;

void open(const ui8* data, size_t size);

//read reads size bytes, returns the number of bytes read
Expand All @@ -324,6 +336,9 @@ namespace ojph {
void close() override { data = cur_ptr = NULL; size = 0; }

private:
// swap the contents of two instances
void swap(mem_infile&) noexcept;

const ui8 *data, *cur_ptr;
size_t size;
};
Expand Down
56 changes: 39 additions & 17 deletions src/core/others/ojph_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,19 @@ namespace ojph {
////////////////////////////////////////////////////////////////////////////
mem_outfile::mem_outfile()
{
reset(*this);
is_open = clear_mem = false;
buf_size = used_size = 0;
buf = cur_ptr = nullptr;
}

////////////////////////////////////////////////////////////////////////////
void mem_outfile::reset(mem_outfile& f) noexcept {
f.is_open = f.clear_mem = false;
f.buf_size = f.used_size = 0;
f.buf = f.cur_ptr = nullptr;
void mem_outfile::swap(mem_outfile& other) noexcept {
std::swap(this->is_open,other.is_open);
std::swap(this->clear_mem,other.clear_mem);
std::swap(this->buf_size,other.buf_size);
std::swap(this->used_size,other.used_size);
std::swap(this->buf,other.buf);
std::swap(this->cur_ptr,other.cur_ptr);
}

////////////////////////////////////////////////////////////////////////////
Expand All @@ -128,24 +133,18 @@ namespace ojph {
}

////////////////////////////////////////////////////////////////////////////
mem_outfile::mem_outfile(mem_outfile&& rhs) noexcept
mem_outfile::mem_outfile(mem_outfile&& rhs) noexcept: mem_outfile()
{
reset(*this);
std::swap(*this, rhs);
this->swap(rhs);
}

////////////////////////////////////////////////////////////////////////////
mem_outfile& mem_outfile::operator=(mem_outfile&& rhs) noexcept
{
if (this == &rhs)
return *this;

if (this->buf)
ojph_aligned_free(this->buf);

reset(*this);
std::swap(*this, rhs);

if (this != &rhs) {
mem_outfile tmp(std::move(rhs));
this->swap(tmp);
}
return *this;
}

Expand Down Expand Up @@ -318,6 +317,22 @@ namespace ojph {
//
////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////
mem_infile::mem_infile(mem_infile&& rhs) noexcept: mem_infile()
{
this->swap(rhs);
}

////////////////////////////////////////////////////////////////////////////
mem_infile& mem_infile::operator=(mem_infile&& rhs) noexcept
{
if (this != &rhs) {
mem_infile tmp(std::move(rhs));
this->swap(tmp);
}
return *this;
}

////////////////////////////////////////////////////////////////////////////
void mem_infile::open(const ui8* data, size_t size)
{
Expand Down Expand Up @@ -376,5 +391,12 @@ namespace ojph {
return result;
}

////////////////////////////////////////////////////////////////////////////
void mem_infile::swap(mem_infile& other) noexcept
{
std::swap(this->data,other.data);
std::swap(this->cur_ptr,other.cur_ptr);
std::swap(this->size,other.size);
}

}
Loading