nghttpx: Simplify move ctor and operator

This commit is contained in:
Tatsuhiro Tsujikawa 2018-11-02 15:40:53 +09:00
parent 2996c28456
commit dcbe0c690f
3 changed files with 35 additions and 55 deletions

View File

@ -32,6 +32,7 @@
#endif // !_WIN32
#include <cassert>
#include <utility>
#include "template.h"
@ -65,25 +66,19 @@ struct BlockAllocator {
~BlockAllocator() { reset(); }
BlockAllocator(BlockAllocator &&other) noexcept
: retain(other.retain),
head(other.head),
: retain{std::exchange(other.retain, nullptr)},
head{std::exchange(other.head, nullptr)},
block_size(other.block_size),
isolation_threshold(other.isolation_threshold) {
other.retain = nullptr;
other.head = nullptr;
}
isolation_threshold(other.isolation_threshold) {}
BlockAllocator &operator=(BlockAllocator &&other) noexcept {
reset();
retain = other.retain;
head = other.head;
retain = std::exchange(other.retain, nullptr);
head = std::exchange(other.head, nullptr);
block_size = other.block_size;
isolation_threshold = other.isolation_threshold;
other.retain = nullptr;
other.head = nullptr;
return *this;
}

View File

@ -44,6 +44,7 @@ struct iovec {
#include <array>
#include <algorithm>
#include <string>
#include <utility>
#include "template.h"
@ -111,11 +112,10 @@ template <typename Memchunk> struct Memchunks {
: pool(pool), head(nullptr), tail(nullptr), len(0) {}
Memchunks(const Memchunks &) = delete;
Memchunks(Memchunks &&other) noexcept
: pool(other.pool), head(other.head), tail(other.tail), len(other.len) {
// keep other.pool
other.head = other.tail = nullptr;
other.len = 0;
}
: pool{other.pool}, // keep other.pool
head{std::exchange(other.head, nullptr)},
tail{std::exchange(other.tail, nullptr)},
len{std::exchange(other.len, 0)} {}
Memchunks &operator=(const Memchunks &) = delete;
Memchunks &operator=(Memchunks &&other) noexcept {
if (this == &other) {
@ -125,12 +125,9 @@ template <typename Memchunk> struct Memchunks {
reset();
pool = other.pool;
head = other.head;
tail = other.tail;
len = other.len;
other.head = other.tail = nullptr;
other.len = 0;
head = std::exchange(other.head, nullptr);
tail = std::exchange(other.tail, nullptr);
len = std::exchange(other.len, 0);
return *this;
}
@ -335,14 +332,12 @@ template <typename Memchunk> struct PeekMemchunks {
peeking(true) {}
PeekMemchunks(const PeekMemchunks &) = delete;
PeekMemchunks(PeekMemchunks &&other) noexcept
: memchunks(std::move(other.memchunks)),
cur(other.cur),
cur_pos(other.cur_pos),
cur_last(other.cur_last),
len(other.len),
peeking(other.peeking) {
other.reset();
}
: memchunks{std::move(other.memchunks)},
cur{std::exchange(other.cur, nullptr)},
cur_pos{std::exchange(other.cur_pos, nullptr)},
cur_last{std::exchange(other.cur_last, nullptr)},
len{std::exchange(other.len, 0)},
peeking{std::exchange(other.peeking, true)} {}
PeekMemchunks &operator=(const PeekMemchunks &) = delete;
PeekMemchunks &operator=(PeekMemchunks &&other) noexcept {
if (this == &other) {
@ -350,13 +345,11 @@ template <typename Memchunk> struct PeekMemchunks {
}
memchunks = std::move(other.memchunks);
cur = other.cur;
cur_pos = other.cur_pos;
cur_last = other.cur_last;
len = other.len;
peeking = other.peeking;
other.reset();
cur = std::exchange(other.cur, nullptr);
cur_pos = std::exchange(other.cur_pos, nullptr);
cur_last = std::exchange(other.cur_last, nullptr);
len = std::exchange(other.len, 0);
peeking = std::exchange(other.peeking, true);
return *this;
}

View File

@ -36,6 +36,7 @@
#include <typeinfo>
#include <algorithm>
#include <ostream>
#include <utility>
namespace nghttp2 {
@ -89,21 +90,17 @@ template <typename T> struct DList {
DList &operator=(const DList &) = delete;
DList(DList &&other) noexcept
: head(other.head), tail(other.tail), len(other.len) {
other.head = other.tail = nullptr;
other.len = 0;
}
: head{std::exchange(other.head, nullptr)},
tail{std::exchange(other.tail, nullptr)},
len{std::exchange(other.len, 0)} {}
DList &operator=(DList &&other) noexcept {
if (this == &other) {
return *this;
}
head = other.head;
tail = other.tail;
len = other.len;
other.head = other.tail = nullptr;
other.len = 0;
head = std::exchange(other.head, nullptr);
tail = std::exchange(other.tail, nullptr);
len = std::exchange(other.len, 0);
return *this;
}
@ -250,10 +247,7 @@ public:
ImmutableString(const ImmutableString &other)
: len(other.len), base(copystr(std::begin(other), std::end(other))) {}
ImmutableString(ImmutableString &&other) noexcept
: len(other.len), base(other.base) {
other.len = 0;
other.base = "";
}
: len{std::exchange(other.len, 0)}, base{std::exchange(other.base, "")} {}
~ImmutableString() {
if (len) {
delete[] base;
@ -278,10 +272,8 @@ public:
if (len) {
delete[] base;
}
len = other.len;
base = other.base;
other.len = 0;
other.base = "";
len = std::exchange(other.len, 0);
base = std::exchange(other.base, "");
return *this;
}