From a41299505c7b4f7cb9a2230d468ebef614325f0d Mon Sep 17 00:00:00 2001 From: q66 Date: Sun, 29 Jan 2017 15:16:13 +0100 Subject: [PATCH] use unique_ptr --- ostd/functional.hh | 6 +- ostd/memory.hh | 333 +-------------------------------------------- 2 files changed, 4 insertions(+), 335 deletions(-) diff --git a/ostd/functional.hh b/ostd/functional.hh index c75f106..1b2c8f0 100644 --- a/ostd/functional.hh +++ b/ostd/functional.hh @@ -551,7 +551,7 @@ public: using AA = AllocatorRebind; AA a(f_stor.second()); using D = AllocatorDestructor; - Box hold(a.allocate(1), D(a, 1)); + std::unique_ptr hold(a.allocate(1), D(a, 1)); ::new(hold.get()) FuncCore(f_stor.first(), A(a)); return hold.release(); } @@ -739,7 +739,7 @@ Function::Function(F f): p_f(nullptr) { using AA = Allocator; AA a; using D = detail::AllocatorDestructor; - Box hold(a.allocate(1), D(a, 1)); + std::unique_ptr hold(a.allocate(1), D(a, 1)); ::new(hold.get()) FF(std::move(f), Allocator(a)); p_f = hold.release(); } @@ -763,7 +763,7 @@ Function::Function(AllocatorArg, A const &a, F f): return; } using D = detail::AllocatorDestructor; - Box hold(aa.allocate(1), D(aa, 1)); + std::unique_ptr hold(aa.allocate(1), D(aa, 1)); ::new(hold.get()) FF(std::move(f), A(aa)); p_f = hold.release(); } diff --git a/ostd/memory.hh b/ostd/memory.hh index 8ef4ef4..5883623 100644 --- a/ostd/memory.hh +++ b/ostd/memory.hh @@ -8,6 +8,7 @@ #include #include +#include #include "ostd/utility.hh" #include "ostd/type_traits.hh" @@ -189,36 +190,6 @@ static T pointer_to( return detail::PointerTo::pointer_to(r); } -/* default deleter */ - -template -struct DefaultDelete { - constexpr DefaultDelete() = default; - - template - DefaultDelete(DefaultDelete const &) noexcept {}; - - void operator()(T *p) const { - delete p; - } -}; - -template -struct DefaultDelete { - constexpr DefaultDelete() = default; - - template - DefaultDelete(DefaultDelete const &) noexcept {}; - - void operator()(T *p) const { - delete[] p; - } - template - void operator()(U *) const = delete; -}; - -/* box */ - namespace detail { template static int ptr_test(...); @@ -244,308 +215,6 @@ namespace detail { }; } /* namespace detail */ -template> -struct Box { - using Element = T; - using Deleter = D; - using Pointer = typename detail::PointerType::Type; - -private: - struct Nat { int x; }; - - using Dref = RemoveReference &; - using Dcref = RemoveReference const &; - -public: - constexpr Box() noexcept: p_stor(nullptr, D()) { - static_assert(!IsPointer, "Box constructed with null fptr deleter"); - } - constexpr Box(Nullptr) noexcept: p_stor(nullptr, D()) { - static_assert(!IsPointer, "Box constructed with null fptr deleter"); - } - - explicit Box(Pointer p) noexcept: p_stor(p, D()) { - static_assert(!IsPointer, "Box constructed with null fptr deleter"); - } - - Box( - Pointer p, Conditional, D, AddLvalueReference> d - ) noexcept: - p_stor(p, d) - {} - - Box(Pointer p, RemoveReference &&d) noexcept: - p_stor(p, std::move(d)) - { - static_assert(!IsReference, "rvalue deleter cannot be a ref"); - } - - Box(Box &&u) noexcept: - p_stor(u.release(), std::forward(u.get_deleter())) - {} - - template - Box( - Box &&u, EnableIf< - !IsArray && - IsConvertible::Pointer, Pointer> && - IsConvertible && (!IsReference || IsSame), - Nat - > = Nat() - ) noexcept: p_stor(u.release(), std::forward
(u.get_deleter())) {} - - Box &operator=(Box &&u) noexcept { - reset(u.release()); - p_stor.second() = std::forward(u.get_deleter()); - return *this; - } - - template - EnableIf< - !IsArray && - IsConvertible::Pointer, Pointer> && - IsAssignable, - Box & - > operator=(Box &&u) noexcept { - reset(u.release()); - p_stor.second() = std::forward
(u.get_deleter()); - return *this; - } - - Box &operator=(Nullptr) noexcept { - reset(); - return *this; - } - - ~Box() { reset(); } - - AddLvalueReference operator*() const { return *p_stor.first(); } - Pointer operator->() const noexcept { return p_stor.first(); } - - explicit operator bool() const noexcept { - return p_stor.first() != nullptr; - } - - Pointer get() const noexcept { return p_stor.first(); } - - Dref get_deleter() noexcept { return p_stor.second(); } - Dcref get_deleter() const noexcept { return p_stor.second(); } - - Pointer release() noexcept { - Pointer p = p_stor.first(); - p_stor.first() = nullptr; - return p; - } - - void reset(Pointer p = nullptr) noexcept { - Pointer tmp = p_stor.first(); - p_stor.first() = p; - if (tmp) { - p_stor.second()(tmp); - } - } - - void swap(Box &u) noexcept { - p_stor.swap(u.p_stor); - } - -private: - detail::CompressedPair p_stor; -}; - -namespace detail { - template>, - RemoveCv> - >> - constexpr bool SameOrLessCvQualifiedBase = IsConvertible; - - template - constexpr bool SameOrLessCvQualifiedBase = false; - - template || IsSame || detail::HasElement::value - > - constexpr bool SameOrLessCvQualified = SameOrLessCvQualifiedBase; - - template - constexpr bool SameOrLessCvQualified = false; -} /* namespace detail */ - -template -struct Box { - using Element = T; - using Deleter = D; - using Pointer = typename detail::PointerType::Type; - -private: - struct Nat { int x; }; - - using Dref = RemoveReference &; - using Dcref = RemoveReference const &; - -public: - constexpr Box() noexcept: p_stor(nullptr, D()) { - static_assert(!IsPointer, "Box constructed with null fptr deleter"); - } - constexpr Box(Nullptr) noexcept: p_stor(nullptr, D()) { - static_assert(!IsPointer, "Box constructed with null fptr deleter"); - } - - template explicit Box(U p, EnableIf< - detail::SameOrLessCvQualified, Nat - > = Nat()) noexcept: p_stor(p, D()) { - static_assert(!IsPointer, "Box constructed with null fptr deleter"); - } - - template Box(U p, Conditional< - IsReference, D, AddLvalueReference - > d, EnableIf< - detail::SameOrLessCvQualified, Nat - > = Nat()) noexcept: - p_stor(p, d) - {} - - Box( - Nullptr, Conditional, D, AddLvalueReference> d - ) noexcept: - p_stor(nullptr, d) - {} - - template Box(U p, RemoveReference &&d, - EnableIf, Nat> = Nat()) noexcept: - p_stor(p, std::move(d)) - { - static_assert(!IsReference, "rvalue deleter cannot be a ref"); - } - - Box(Nullptr, RemoveReference &&d) noexcept: - p_stor(nullptr, std::move(d)) - { - static_assert(!IsReference, "rvalue deleter cannot be a ref"); - } - - Box(Box &&u) noexcept: - p_stor(u.release(), std::forward(u.get_deleter())) - {} - - template - Box( - Box &&u, EnableIf< - IsArray && - detail::SameOrLessCvQualified::Pointer,Pointer> && - IsConvertible && (!IsReference || IsSame), - Nat - > = Nat() - ) noexcept: - p_stor(u.release(), std::forward
(u.get_deleter())) - {} - - Box &operator=(Box &&u) noexcept { - reset(u.release()); - p_stor.second() = std::forward(u.get_deleter()); - return *this; - } - - template - EnableIf< - IsArray && - detail::SameOrLessCvQualified::Pointer, Pointer> && - IsAssignable, - Box & - > operator=(Box &&u) noexcept { - reset(u.release()); - p_stor.second() = std::forward
(u.get_deleter()); - return *this; - } - - Box &operator=(Nullptr) noexcept { - reset(); - return *this; - } - - ~Box() { reset(); } - - AddLvalueReference operator[](Size idx) const { - return p_stor.first()[idx]; - } - - explicit operator bool() const noexcept { - return p_stor.first() != nullptr; - } - - Pointer get() const noexcept { return p_stor.first(); } - - Dref get_deleter() noexcept { return p_stor.second(); } - Dcref get_deleter() const noexcept { return p_stor.second(); } - - Pointer release() noexcept { - Pointer p = p_stor.first(); - p_stor.first() = nullptr; - return p; - } - - template - EnableIf< - detail::SameOrLessCvQualified, void - > reset(U p) noexcept { - Pointer tmp = p_stor.first(); - p_stor.first() = p; - if (tmp) { - p_stor.second()(tmp); - } - } - - void reset(Nullptr) noexcept { - Pointer tmp = p_stor.first(); - p_stor.first() = nullptr; - if (tmp) { - p_stor.second()(tmp); - } - } - - void reset() noexcept { - reset(nullptr); - } - - void swap(Box &u) noexcept { - p_stor.swap(u.p_stor); - } - -private: - detail::CompressedPair p_stor; -}; - -namespace detail { - template - struct BoxIf { - using BoxType = Box; - }; - - template - struct BoxIf { - using BoxUnknownSize = Box; - }; - - template - struct BoxIf { - using BoxKnownSize = void; - }; -} - -template -typename detail::BoxIf::BoxType make_box(A &&...args) { - return Box(new T(std::forward(args)...)); -} - -template -typename detail::BoxIf::BoxUnknownSize make_box(Size n) { - return Box(new RemoveExtent[n]()); -} - -template -typename detail::BoxIf::BoxKnownSize make_box(A &&...args) = delete; - /* allocator */ template