Skip to content

Commit

Permalink
Make static_container nothrow
Browse files Browse the repository at this point in the history
  • Loading branch information
terrakuh committed Sep 3, 2020
1 parent 9ea6e49 commit 18c4cbd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 21 deletions.
6 changes: 5 additions & 1 deletion terraqtt/protocol/reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ inline bool read_element(Input& input, std::error_code& ec, read_context& contex
}

typename Input::char_type buffer[2];

input.read(buffer, sizeof(buffer));

if (!input) {
Expand Down Expand Up @@ -122,6 +121,11 @@ inline bool read_blob(Input& input, std::error_code& ec, read_context& context,

for (; context.sequence_data[0] && context.available;
--context.sequence_data[0], --context.available, --context.remaining_size) {
if (blob.size() >= blob.max_size()) {
ec = std::make_error_code(std::errc::not_enough_memory);
return false;
}

const auto c = input.get();

if (!input) {
Expand Down
23 changes: 3 additions & 20 deletions terraqtt/static_container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define TERRAQTT_STATIC_CONTAINER_HPP_

#include <cstddef>
#include <stdexcept>
#include <type_traits>
#include <utility>

Expand All @@ -14,12 +13,8 @@ class static_container
public:
typedef Type value_type;

void resize(std::size_t size)
void resize(std::size_t size) noexcept(std::is_nothrow_default_constructible<Type>::value)
{
if (size > max_size()) {
throw std::bad_alloc{};
}

while (size > _size) {
push_back(Type{});
}
Expand All @@ -30,26 +25,14 @@ class static_container
}
void push_back(const Type& value)
{
if (size() == max_size()) {
throw std::out_of_range{ "container is full" };
}

new (&_data[_size++]) Type{ value };
}
void push_back(Type&& value)
void push_back(Type&& value) noexcept(std::is_nothrow_move_constructible<Type>::value)
{
if (size() == max_size()) {
throw std::out_of_range{ "container is full" };
}

new (_data + _size++) Type{ std::move(value) };
}
void pop_back()
void pop_back() noexcept
{
if (empty()) {
throw std::out_of_range{ "container is empty" };
}

reinterpret_cast<Type*>(_data + --_size)->~Type();
}
Type* begin() noexcept
Expand Down

0 comments on commit 18c4cbd

Please sign in to comment.