From e4222f35fffc723191367d1a053b29f1270d2844 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Sun, 13 Apr 2025 12:44:26 +0300 Subject: [PATCH] Replace util::blob copy constructor/assignment with .copy() method and mark all appropriate methods noexcept --- libs/util/include/psemek/util/blob.hpp | 87 ++++++++++++-------------- 1 file changed, 40 insertions(+), 47 deletions(-) diff --git a/libs/util/include/psemek/util/blob.hpp b/libs/util/include/psemek/util/blob.hpp index 5b581cb5..08520d1b 100644 --- a/libs/util/include/psemek/util/blob.hpp +++ b/libs/util/include/psemek/util/blob.hpp @@ -11,60 +11,57 @@ namespace psemek::util struct blob { - blob() = default; - blob(blob const & other); - blob(blob && other); + blob() noexcept = default; + blob(blob const & other) = delete; + blob(blob && other) noexcept; blob(std::size_t size); - blob(std::size_t size, std::unique_ptr data); - blob(std::size_t size, char * data); + blob(std::size_t size, std::unique_ptr data) noexcept; + blob(std::size_t size, char * data) noexcept; - blob & operator = (blob const & other); - blob & operator = (blob && other); + blob & operator = (blob const & other) = delete; + blob & operator = (blob && other) noexcept; ~blob() = default; - char * data() { return data_.get(); } - char const * data() const { return data_.get(); } + char * data() noexcept { return data_.get(); } + char const * data() const noexcept { return data_.get(); } - std::size_t size() const { return size_; } + std::size_t size() const noexcept { return size_; } - explicit operator bool() const { return static_cast(data_); } + explicit operator bool() const noexcept { return static_cast(data_); } void reset(); - std::unique_ptr release(); + std::unique_ptr release() noexcept; - void swap(blob & other); + void swap(blob & other) noexcept; using iterator = char *; using const_iterator = char const *; - char * begin() { return data(); } - char const * begin() const { return data(); } + char * begin() noexcept { return data(); } + char const * begin() const noexcept { return data(); } - char * end() { return data() + size(); } - char const * end() const { return data() + size(); } + char * end() noexcept { return data() + size(); } + char const * end() const noexcept { return data() + size(); } - char & operator[](std::size_t i) { return data()[i]; } - char const & operator[](std::size_t i) const { return data()[i]; } + char & operator[](std::size_t i) noexcept { return data()[i]; } + char const & operator[](std::size_t i) const noexcept { return data()[i]; } std::string string() const; - std::string_view string_view() const; + std::string_view string_view() const noexcept; - util::span span(); - util::span span() const; + util::span span() noexcept; + util::span span() const noexcept; + + blob copy() const; private: std::unique_ptr data_; std::size_t size_ = 0; }; - inline blob::blob(blob const & other) - { - *this = other; - } - - inline blob::blob(blob && other) + inline blob::blob(blob && other) noexcept : data_{std::move(other.data_)} , size_{other.size_} { @@ -76,28 +73,17 @@ namespace psemek::util , size_{size} {} - inline blob::blob(std::size_t size, std::unique_ptr data) + inline blob::blob(std::size_t size, std::unique_ptr data) noexcept : data_{std::move(data)} , size_{size} {} - inline blob::blob(std::size_t size, char * data) + inline blob::blob(std::size_t size, char * data) noexcept : data_{data} , size_{size} {} - inline blob & blob::operator=(blob const & other) - { - if (this != &other) - { - data_.reset(new char [other.size()]); - std::copy(other.begin(), other.end(), begin()); - size_ = other.size(); - } - return *this; - } - - inline blob & blob::operator=(blob && other) + inline blob & blob::operator = (blob && other) noexcept { blob(std::move(other)).swap(*this); return *this; @@ -109,13 +95,13 @@ namespace psemek::util size_ = 0; } - inline std::unique_ptr blob::release() + inline std::unique_ptr blob::release() noexcept { size_ = 0; return std::move(data_); } - inline void blob::swap(blob & other) + inline void blob::swap(blob & other) noexcept { std::swap(data_, other.data_); std::swap(size_, other.size_); @@ -126,19 +112,26 @@ namespace psemek::util return std::string(data_.get(), data_.get() + size_); } - inline std::string_view blob::string_view() const + inline std::string_view blob::string_view() const noexcept { return std::string_view(data_.get(), size_); } - inline util::span blob::span() + inline util::span blob::span() noexcept { return {data_.get(), size_}; } - inline util::span blob::span() const + inline util::span blob::span() const noexcept { return {data_.get(), size_}; } + inline blob blob::copy() const + { + std::unique_ptr data(new char [size_]); + std::copy(begin(), end(), data.get()); + return blob(size_, std::move(data)); + } + }