Add util::blob::operator bool() and util::blob::swap

This commit is contained in:
Nikita Lisitsa 2020-11-24 23:10:52 +03:00
parent 1e1679ffe0
commit bfff9b2c4d

View file

@ -27,9 +27,13 @@ namespace psemek::util
std::size_t size() const { return size_; }
explicit operator bool() const { return static_cast<bool>(data_); }
void reset();
std::unique_ptr<char[]> release();
void swap(blob & other);
using iterator = char *;
using const_iterator = char const *;
@ -56,8 +60,10 @@ namespace psemek::util
}
inline blob::blob(blob && other)
: data_{std::move(other.data_)}
, size_{other.size_}
{
*this = std::move(other);
other.size_ = 0;
}
inline blob::blob(std::size_t size)
@ -88,12 +94,7 @@ namespace psemek::util
inline blob & blob::operator=(blob && other)
{
if (this != &other)
{
data_ = std::move(other.data_);
size_ = other.size();
other.size_ = 0;
}
blob(std::move(other)).swap(*this);
return *this;
}
@ -109,6 +110,12 @@ namespace psemek::util
return std::move(data_);
}
inline void blob::swap(blob & other)
{
std::swap(data_, other.data_);
std::swap(size_, other.size_);
}
inline std::string blob::string() const
{
return std::string(data_.get(), data_.get() + size_);