Commit f9409ea7 authored by Dominik Charousset's avatar Dominik Charousset

Fix receive_buffer(size_t) ctor behavior + cleanup

parent 0a8ab78e
......@@ -46,11 +46,11 @@ public:
std::default_delete<value_type[]>>;
/// Create an empty container.
receive_buffer();
receive_buffer() noexcept;
/// Create an empty container with `size` storage. Data in the storage is
/// not initialized.
receive_buffer(size_t size);
/// Create an empty container of size `count`. Data in the storage is not
/// initialized.
receive_buffer(size_t count);
/// Move constructor.
receive_buffer(receive_buffer&& other) noexcept;
......@@ -75,18 +75,18 @@ public:
}
/// Returns the number of stored elements.
size_type size() const noexcept {
inline size_type size() const noexcept {
return size_;
}
/// Returns the number of elements that the container has allocated space for.
size_type capacity() const noexcept {
inline size_type capacity() const noexcept {
return capacity_;
}
/// Returns the maximum possible number of elements the container
/// could theoretically hold.
size_type max_size() const noexcept {
inline size_type max_size() const noexcept {
return std::numeric_limits<size_t>::max();
}
......
......@@ -32,63 +32,41 @@ namespace caf {
namespace io {
namespace network {
receive_buffer::receive_buffer()
receive_buffer::receive_buffer() noexcept
: buffer_(nullptr),
capacity_(0),
size_(0) {
// nop
}
receive_buffer::receive_buffer(size_type size)
: buffer_(new value_type[size]),
capacity_(size),
size_(0) {
// nop
receive_buffer::receive_buffer(size_type count) : receive_buffer() {
resize(count);
}
receive_buffer::receive_buffer(receive_buffer&& other) noexcept
: capacity_(std::move(other.capacity_)),
size_(std::move(other.size_)) {
buffer_ = std::move(other.buffer_);
other.size_ = 0;
other.capacity_ = 0;
other.buffer_.reset();
: receive_buffer() {
swap(other);
}
receive_buffer::receive_buffer(const receive_buffer& other)
: capacity_(other.capacity_),
size_(other.size_) {
if (other.size_ == 0) {
buffer_.reset();
} else {
buffer_.reset(new value_type[other.size_]);
std::copy(other.cbegin(), other.cend(), buffer_.get());
}
: receive_buffer(other.size()) {
std::copy(other.cbegin(), other.cend(), buffer_.get());
}
receive_buffer& receive_buffer::operator=(receive_buffer&& other) noexcept {
size_ = std::move(other.size_);
capacity_ = std::move(other.capacity_);
buffer_ = std::move(other.buffer_);
other.clear();
receive_buffer tmp{std::move(other)};
swap(tmp);
return *this;
}
receive_buffer& receive_buffer::operator=(const receive_buffer& other) {
size_ = other.size_;
capacity_ = other.capacity_;
if (other.size_ == 0) {
buffer_.reset();
} else {
buffer_.reset(new value_type[other.size_]);
std::copy(other.cbegin(), other.cend(), buffer_.get());
}
resize(other.size());
std::copy(other.cbegin(), other.cend(), buffer_.get());
return *this;
}
void receive_buffer::resize(size_type new_size) {
if (new_size > capacity_)
increase_by(new_size - capacity_);
reserve(new_size);
size_ = new_size;
}
......@@ -104,14 +82,13 @@ void receive_buffer::shrink_to_fit() {
void receive_buffer::clear() {
size_ = 0;
buffer_ptr new_buffer{new value_type[capacity_]};
std::swap(buffer_, new_buffer);
}
void receive_buffer::swap(receive_buffer& other) noexcept {
std::swap(capacity_, other.capacity_);
std::swap(size_, other.size_);
std::swap(buffer_, other.buffer_);
using std::swap;
swap(capacity_, other.capacity_);
swap(size_, other.size_);
swap(buffer_, other.buffer_);
}
void receive_buffer::push_back(value_type value) {
......@@ -127,9 +104,10 @@ void receive_buffer::increase_by(size_t bytes) {
if (!buffer_) {
buffer_.reset(new value_type[bytes]);
} else {
using std::swap;
buffer_ptr new_buffer{new value_type[capacity_ + bytes]};
std::copy(begin(), end(), new_buffer.get());
std::swap(buffer_, new_buffer);
swap(buffer_, new_buffer);
}
capacity_ += bytes;
}
......@@ -140,9 +118,10 @@ void receive_buffer::shrink_by(size_t bytes) {
if (new_size == 0) {
buffer_.reset();
} else {
using std::swap;
buffer_ptr new_buffer{new value_type[new_size]};
std::copy(begin(), begin() + new_size, new_buffer.get());
std::swap(buffer_, new_buffer);
swap(buffer_, new_buffer);
}
capacity_ = new_size;
}
......
......@@ -49,15 +49,15 @@ CAF_TEST(constuctors) {
CAF_CHECK_EQUAL(a.capacity(), 0ul);
CAF_CHECK_EQUAL(a.data(), nullptr);
CAF_CHECK(a.empty());
CAF_CHECK_EQUAL(b.size(), 0ul);
CAF_CHECK_EQUAL(b.size(), 1024ul);
CAF_CHECK_EQUAL(b.capacity(), 1024ul);
CAF_CHECK(b.data() != nullptr);
CAF_CHECK(b.empty());
CAF_CHECK(!b.empty());
receive_buffer other{std::move(b)};
CAF_CHECK_EQUAL(other.size(), 0ul);
CAF_CHECK_EQUAL(other.size(), 1024ul);
CAF_CHECK_EQUAL(other.capacity(), 1024ul);
CAF_CHECK(other.data() != nullptr);
CAF_CHECK(other.empty());
CAF_CHECK(!other.empty());
}
CAF_TEST(reserve) {
......@@ -165,7 +165,7 @@ CAF_TEST(swap) {
for (auto c : vec)
a.push_back(c);
std::swap(a, b);
CAF_CHECK_EQUAL(a.size(), 0ul);
CAF_CHECK_EQUAL(a.size(), 1024ul);
CAF_CHECK_EQUAL(a.capacity(), 1024ul);
CAF_CHECK(a.data() != nullptr);
CAF_CHECK_EQUAL(b.size(), vec.size());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment