Skip to content

Commit 79f040c

Browse files
authored
fix move special functions in mem in and outfile (#258)
Correcting a move bug that was introduced earlier.
1 parent 1f6ad44 commit 79f040c

File tree

2 files changed

+59
-22
lines changed

2 files changed

+59
-22
lines changed

src/core/openjph/ojph_file.h

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,10 @@ namespace ojph {
234234

235235
private:
236236

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

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

314+
mem_infile(mem_infile const&) = delete;
315+
mem_infile& operator=(mem_infile const&) = delete;
316+
317+
/**
318+
* Move construction leaves the moved-from value in default constructed state
319+
* and transfers ownership of the internal state to the moved-to instance.
320+
**/
321+
mem_infile(mem_infile &&) noexcept;
322+
/**
323+
* move assignment with the same ownership transfer semantics as
324+
* move construction.
325+
**/
326+
mem_infile& operator=(mem_infile&&) noexcept;
327+
316328
void open(const ui8* data, size_t size);
317329

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

326338
private:
339+
// swap the contents of two instances
340+
void swap(mem_infile&) noexcept;
341+
327342
const ui8 *data, *cur_ptr;
328343
size_t size;
329344
};

src/core/others/ojph_file.cpp

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,19 @@ namespace ojph {
107107
////////////////////////////////////////////////////////////////////////////
108108
mem_outfile::mem_outfile()
109109
{
110-
reset(*this);
110+
is_open = clear_mem = false;
111+
buf_size = used_size = 0;
112+
buf = cur_ptr = nullptr;
111113
}
112114

113115
////////////////////////////////////////////////////////////////////////////
114-
void mem_outfile::reset(mem_outfile& f) noexcept {
115-
f.is_open = f.clear_mem = false;
116-
f.buf_size = f.used_size = 0;
117-
f.buf = f.cur_ptr = nullptr;
116+
void mem_outfile::swap(mem_outfile& other) noexcept {
117+
std::swap(this->is_open,other.is_open);
118+
std::swap(this->clear_mem,other.clear_mem);
119+
std::swap(this->buf_size,other.buf_size);
120+
std::swap(this->used_size,other.used_size);
121+
std::swap(this->buf,other.buf);
122+
std::swap(this->cur_ptr,other.cur_ptr);
118123
}
119124

120125
////////////////////////////////////////////////////////////////////////////
@@ -128,24 +133,18 @@ namespace ojph {
128133
}
129134

130135
////////////////////////////////////////////////////////////////////////////
131-
mem_outfile::mem_outfile(mem_outfile&& rhs) noexcept
136+
mem_outfile::mem_outfile(mem_outfile&& rhs) noexcept: mem_outfile()
132137
{
133-
reset(*this);
134-
std::swap(*this, rhs);
138+
this->swap(rhs);
135139
}
136140

137141
////////////////////////////////////////////////////////////////////////////
138142
mem_outfile& mem_outfile::operator=(mem_outfile&& rhs) noexcept
139143
{
140-
if (this == &rhs)
141-
return *this;
142-
143-
if (this->buf)
144-
ojph_aligned_free(this->buf);
145-
146-
reset(*this);
147-
std::swap(*this, rhs);
148-
144+
if (this != &rhs) {
145+
mem_outfile tmp(std::move(rhs));
146+
this->swap(tmp);
147+
}
149148
return *this;
150149
}
151150

@@ -318,6 +317,22 @@ namespace ojph {
318317
//
319318
////////////////////////////////////////////////////////////////////////////
320319

320+
////////////////////////////////////////////////////////////////////////////
321+
mem_infile::mem_infile(mem_infile&& rhs) noexcept: mem_infile()
322+
{
323+
this->swap(rhs);
324+
}
325+
326+
////////////////////////////////////////////////////////////////////////////
327+
mem_infile& mem_infile::operator=(mem_infile&& rhs) noexcept
328+
{
329+
if (this != &rhs) {
330+
mem_infile tmp(std::move(rhs));
331+
this->swap(tmp);
332+
}
333+
return *this;
334+
}
335+
321336
////////////////////////////////////////////////////////////////////////////
322337
void mem_infile::open(const ui8* data, size_t size)
323338
{
@@ -376,5 +391,12 @@ namespace ojph {
376391
return result;
377392
}
378393

394+
////////////////////////////////////////////////////////////////////////////
395+
void mem_infile::swap(mem_infile& other) noexcept
396+
{
397+
std::swap(this->data,other.data);
398+
std::swap(this->cur_ptr,other.cur_ptr);
399+
std::swap(this->size,other.size);
400+
}
379401

380402
}

0 commit comments

Comments
 (0)