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

View File

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

View File

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