Replace util::blob copy constructor/assignment with .copy() method and mark all appropriate methods noexcept

This commit is contained in:
Nikita Lisitsa 2025-04-13 12:44:26 +03:00
parent 73abc03ae2
commit e4222f35ff

View file

@ -11,60 +11,57 @@ namespace psemek::util
struct blob struct blob
{ {
blob() = default; blob() noexcept = default;
blob(blob const & other); blob(blob const & other) = delete;
blob(blob && other); blob(blob && other) noexcept;
blob(std::size_t size); blob(std::size_t size);
blob(std::size_t size, std::unique_ptr<char[]> data); blob(std::size_t size, std::unique_ptr<char[]> data) noexcept;
blob(std::size_t size, char * data); blob(std::size_t size, char * data) noexcept;
blob & operator = (blob const & other); blob & operator = (blob const & other) = delete;
blob & operator = (blob && other); blob & operator = (blob && other) noexcept;
~blob() = default; ~blob() = default;
char * data() { return data_.get(); } char * data() noexcept { return data_.get(); }
char const * data() const { 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<bool>(data_); } explicit operator bool() const noexcept { return static_cast<bool>(data_); }
void reset(); void reset();
std::unique_ptr<char[]> release(); std::unique_ptr<char[]> release() noexcept;
void swap(blob & other); void swap(blob & other) noexcept;
using iterator = char *; using iterator = char *;
using const_iterator = char const *; using const_iterator = char const *;
char * begin() { return data(); } char * begin() noexcept { return data(); }
char const * begin() const { return data(); } char const * begin() const noexcept { return data(); }
char * end() { return data() + size(); } char * end() noexcept { return data() + size(); }
char const * end() const { return data() + size(); } char const * end() const noexcept { return data() + size(); }
char & operator[](std::size_t i) { return data()[i]; } char & operator[](std::size_t i) noexcept { return data()[i]; }
char const & operator[](std::size_t i) const { return data()[i]; } char const & operator[](std::size_t i) const noexcept { return data()[i]; }
std::string string() const; std::string string() const;
std::string_view string_view() const; std::string_view string_view() const noexcept;
util::span<char> span(); util::span<char> span() noexcept;
util::span<char const> span() const; util::span<char const> span() const noexcept;
blob copy() const;
private: private:
std::unique_ptr<char[]> data_; std::unique_ptr<char[]> data_;
std::size_t size_ = 0; std::size_t size_ = 0;
}; };
inline blob::blob(blob const & other) inline blob::blob(blob && other) noexcept
{
*this = other;
}
inline blob::blob(blob && other)
: data_{std::move(other.data_)} : data_{std::move(other.data_)}
, size_{other.size_} , size_{other.size_}
{ {
@ -76,28 +73,17 @@ namespace psemek::util
, size_{size} , size_{size}
{} {}
inline blob::blob(std::size_t size, std::unique_ptr<char[]> data) inline blob::blob(std::size_t size, std::unique_ptr<char[]> data) noexcept
: data_{std::move(data)} : data_{std::move(data)}
, size_{size} , size_{size}
{} {}
inline blob::blob(std::size_t size, char * data) inline blob::blob(std::size_t size, char * data) noexcept
: data_{data} : data_{data}
, size_{size} , size_{size}
{} {}
inline blob & blob::operator=(blob const & other) inline blob & blob::operator = (blob && other) noexcept
{
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)
{ {
blob(std::move(other)).swap(*this); blob(std::move(other)).swap(*this);
return *this; return *this;
@ -109,13 +95,13 @@ namespace psemek::util
size_ = 0; size_ = 0;
} }
inline std::unique_ptr<char[]> blob::release() inline std::unique_ptr<char[]> blob::release() noexcept
{ {
size_ = 0; size_ = 0;
return std::move(data_); return std::move(data_);
} }
inline void blob::swap(blob & other) inline void blob::swap(blob & other) noexcept
{ {
std::swap(data_, other.data_); std::swap(data_, other.data_);
std::swap(size_, other.size_); std::swap(size_, other.size_);
@ -126,19 +112,26 @@ namespace psemek::util
return std::string(data_.get(), data_.get() + size_); 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_); return std::string_view(data_.get(), size_);
} }
inline util::span<char> blob::span() inline util::span<char> blob::span() noexcept
{ {
return {data_.get(), size_}; return {data_.get(), size_};
} }
inline util::span<char const> blob::span() const inline util::span<char const> blob::span() const noexcept
{ {
return {data_.get(), size_}; return {data_.get(), size_};
} }
inline blob blob::copy() const
{
std::unique_ptr<char[]> data(new char [size_]);
std::copy(begin(), end(), data.get());
return blob(size_, std::move(data));
}
} }