Add unique_value_ptr.

This commit is contained in:
levlam 2023-01-22 23:36:30 +03:00
parent d6502824b3
commit 201af22ef2
3 changed files with 124 additions and 0 deletions

View File

@ -292,6 +292,7 @@ set(TDUTILS_SOURCE
td/utils/uint128.h
td/utils/unicode.h
td/utils/unique_ptr.h
td/utils/unique_value_ptr.h
td/utils/utf8.h
td/utils/Variant.h
td/utils/VectorQueue.h

View File

@ -0,0 +1,93 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#pragma once
#include "td/utils/unique_ptr.h"
#include <cstddef>
#include <utility>
namespace td {
// copyable by value td::unique_ptr
template <class T>
class unique_value_ptr final {
public:
unique_value_ptr() noexcept = default;
unique_value_ptr(const unique_value_ptr &other) {
if (other != nullptr) {
ptr_ = make_unique<T>(*other);
}
}
unique_value_ptr &operator=(const unique_value_ptr &other) {
if (other == nullptr) {
ptr_ = nullptr;
} else {
ptr_ = make_unique<T>(*other);
}
return *this;
}
unique_value_ptr(unique_value_ptr &&other) noexcept = default;
unique_value_ptr &operator=(unique_value_ptr &&other) = default;
unique_value_ptr(std::nullptr_t) noexcept {
}
unique_value_ptr(unique_ptr<T> &&ptr) noexcept : ptr_(std::move(ptr)) {
}
T *get() noexcept {
return ptr_.get();
}
const T *get() const noexcept {
return ptr_.get();
}
T *operator->() noexcept {
return ptr_.get();
}
const T *operator->() const noexcept {
return ptr_.get();
}
T &operator*() noexcept {
return *ptr_;
}
const T &operator*() const noexcept {
return *ptr_;
}
explicit operator bool() const noexcept {
return ptr_ != nullptr;
}
private:
unique_ptr<T> ptr_;
};
template <class T>
bool operator==(const unique_value_ptr<T> &p, std::nullptr_t) {
return !p;
}
template <class T>
bool operator!=(const unique_value_ptr<T> &p, std::nullptr_t) {
return static_cast<bool>(p);
}
template <class T>
bool operator==(const unique_value_ptr<T> &lhs, const unique_value_ptr<T> &rhs) {
if (lhs == nullptr) {
return rhs == nullptr;
}
return rhs != nullptr && *lhs == *rhs;
}
template <class T>
bool operator!=(const unique_value_ptr<T> &lhs, const unique_value_ptr<T> &rhs) {
return !(lhs == rhs);
}
template <class Type, class... Args>
unique_value_ptr<Type> make_unique_value(Args &&...args) {
return unique_value_ptr<Type>(make_unique<Type>(std::forward<Args>(args)...));
}
} // namespace td

View File

@ -40,6 +40,7 @@
#include "td/utils/translit.h"
#include "td/utils/uint128.h"
#include "td/utils/unicode.h"
#include "td/utils/unique_value_ptr.h"
#include "td/utils/utf8.h"
#include <algorithm>
@ -1260,3 +1261,32 @@ TEST(FloodControl, Fast) {
LOG(INFO) << ++count << ": " << now;
}
}
TEST(UniqueValuePtr, Basic) {
auto a = td::make_unique_value<int>(5);
td::unique_value_ptr<int> b;
ASSERT_TRUE(b == nullptr);
ASSERT_TRUE(a != nullptr);
ASSERT_TRUE(a != b);
b = a;
ASSERT_TRUE(a != nullptr);
ASSERT_TRUE(b != nullptr);
ASSERT_TRUE(a == b);
*a = 6;
ASSERT_TRUE(a != nullptr);
ASSERT_TRUE(b != nullptr);
ASSERT_TRUE(a != b);
b = std::move(a);
ASSERT_TRUE(a == nullptr);
ASSERT_TRUE(b != nullptr);
ASSERT_TRUE(a != b);
auto c = td::make_unique_value<td::unique_value_ptr<int>>(a);
ASSERT_TRUE(*c == a);
ASSERT_TRUE(*c == nullptr);
c = td::make_unique_value<td::unique_value_ptr<int>>(b);
ASSERT_TRUE(*c == b);
ASSERT_TRUE(**c == 6);
auto d = c;
ASSERT_TRUE(c == d);
ASSERT_TRUE(6 == **d);
}