From bfff9b2c4d6515c1bf0577defa4223bc3e8581a2 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Tue, 24 Nov 2020 23:10:52 +0300 Subject: [PATCH] Add util::blob::operator bool() and util::blob::swap --- libs/util/include/psemek/util/blob.hpp | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/libs/util/include/psemek/util/blob.hpp b/libs/util/include/psemek/util/blob.hpp index 2eafccab..31ac7463 100644 --- a/libs/util/include/psemek/util/blob.hpp +++ b/libs/util/include/psemek/util/blob.hpp @@ -27,9 +27,13 @@ namespace psemek::util std::size_t size() const { return size_; } + explicit operator bool() const { return static_cast(data_); } + void reset(); std::unique_ptr 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_);