diff --git a/src/allocator.h b/src/allocator.h index fa0de3ad..6ad83522 100644 --- a/src/allocator.h +++ b/src/allocator.h @@ -32,6 +32,7 @@ #endif // !_WIN32 #include +#include #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; } diff --git a/src/memchunk.h b/src/memchunk.h index d373d06a..ba8d0e18 100644 --- a/src/memchunk.h +++ b/src/memchunk.h @@ -44,6 +44,7 @@ struct iovec { #include #include #include +#include #include "template.h" @@ -111,11 +112,10 @@ template 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 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 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 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; } diff --git a/src/template.h b/src/template.h index 6b35f8af..4a240b3e 100644 --- a/src/template.h +++ b/src/template.h @@ -36,6 +36,7 @@ #include #include #include +#include namespace nghttp2 { @@ -89,21 +90,17 @@ template 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; }