Skip to content

Commit

Permalink
simplify move assignment/constructor overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
falconindy committed Sep 29, 2024
1 parent a506e3f commit 6c15c25
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/archive_io.hh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <memory>
#include <optional>
#include <string>
#include <utility>

namespace pkgfile {

Expand All @@ -16,22 +17,20 @@ class ReadOnlyFile {

struct MMappedRegion {
MMappedRegion(void* ptr, off_t size) : ptr(ptr), size(size) {}
~MMappedRegion();

// Cannot be copied
// Moveable, but not copyable.
MMappedRegion(const MMappedRegion&) = delete;
MMappedRegion& operator=(const MMappedRegion&) = delete;

MMappedRegion(MMappedRegion&& other) : ptr(other.ptr), size(other.size) {
other.ptr = MAP_FAILED;
}
MMappedRegion& operator=(MMappedRegion&& other) {
ptr = other.ptr;
size = other.size;
other.ptr = MAP_FAILED;
ptr = std::exchange(other.ptr, MAP_FAILED);
size = std::exchange(other.size, -1);
return *this;
}

~MMappedRegion();
MMappedRegion(MMappedRegion&& other) : ptr(other.ptr), size(other.size) {
*this = std::move(other);
}

void* ptr;
off_t size;
Expand Down

0 comments on commit 6c15c25

Please sign in to comment.