Replace std::unique_ptr with td::unique_ptr to increase compilation speed and decrease compilation RAM usage.

GitOrigin-RevId: ffd9b89201e1bfa48d0cf62e7abe9e94b0db86f9
This commit is contained in:
levlam 2018-09-27 17:40:04 +03:00
parent b3bae546ec
commit c3417b5f94
3 changed files with 96 additions and 7 deletions

View File

@ -202,6 +202,7 @@ set(TDUTILS_SOURCE
td/utils/translit.h td/utils/translit.h
td/utils/type_traits.h td/utils/type_traits.h
td/utils/unicode.h td/utils/unicode.h
td/utils/unique_ptr.h
td/utils/utf8.h td/utils/utf8.h
td/utils/Variant.h td/utils/Variant.h
td/utils/VectorQueue.h td/utils/VectorQueue.h

View File

@ -43,10 +43,9 @@
// clang-format on // clang-format on
#include "td/utils/int_types.h" #include "td/utils/int_types.h"
#include "td/utils/unique_ptr.h"
#include <memory>
#include <string> #include <string>
#include <utility>
#include <vector> #include <vector>
#define TD_DEBUG #define TD_DEBUG
@ -99,11 +98,6 @@ using string = std::string;
template <class ValueT> template <class ValueT>
using vector = std::vector<ValueT>; using vector = std::vector<ValueT>;
template <class ValueT>
using unique_ptr = std::unique_ptr<ValueT>;
using std::make_unique;
struct Unit {}; struct Unit {};
struct Auto { struct Auto {

View File

@ -0,0 +1,94 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
//
// 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 <cstddef>
#include <type_traits>
#include <utility>
namespace td {
template <class T>
class unique_ptr {
public:
using pointer = T *;
using element_type = T;
unique_ptr() noexcept = default;
unique_ptr(const unique_ptr &other) = delete;
unique_ptr &operator=(const unique_ptr &other) = delete;
unique_ptr(unique_ptr &&other) noexcept : ptr_(other.release()) {
}
unique_ptr &operator=(unique_ptr &&other) noexcept {
reset(other.release());
return *this;
}
~unique_ptr() {
reset();
}
unique_ptr(std::nullptr_t) noexcept {
}
explicit unique_ptr(T *ptr) noexcept : ptr_(ptr) {
}
template <class S, class = std::enable_if_t<std::is_base_of<T, S>::value>>
unique_ptr(unique_ptr<S> &&other) noexcept : ptr_(static_cast<S *>(other.release())) {
}
template <class S, class = std::enable_if_t<std::is_base_of<T, S>::value>>
unique_ptr &operator=(unique_ptr<S> &&other) noexcept {
reset(static_cast<T *>(other.release()));
return *this;
}
void reset(T *new_ptr = nullptr) noexcept {
delete ptr_;
ptr_ = new_ptr;
}
T *release() noexcept {
auto res = ptr_;
ptr_ = nullptr;
return res;
}
T *get() const noexcept {
return ptr_;
}
T *operator->() const noexcept {
return ptr_;
}
T &operator*() const noexcept {
return *ptr_;
}
explicit operator bool() const noexcept {
return ptr_ != nullptr;
}
private:
T *ptr_{nullptr};
};
template <class T>
bool operator==(std::nullptr_t, const unique_ptr<T> &p) {
return !p;
}
template <class T>
bool operator==(const unique_ptr<T> &p, std::nullptr_t) {
return !p;
}
template <class T>
bool operator!=(std::nullptr_t, const unique_ptr<T> &p) {
return static_cast<bool>(p);
}
template <class T>
bool operator!=(const unique_ptr<T> &p, std::nullptr_t) {
return static_cast<bool>(p);
}
template <class Type, class... Args>
unique_ptr<Type> make_unique(Args &&... args) {
return unique_ptr<Type>(new Type(std::forward<Args>(args)...));
}
} // namespace td