Skip to content

Commit b57e23d

Browse files
author
Amber-Sophia Schroeck
committed
add smart_pointer_traits
add container/array add default_deleter and add this support to list
1 parent aa6de76 commit b57e23d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2071
-1256
lines changed

include/allocator/mn_basic_allocator.hpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "../mn_config.hpp"
2222

23+
#include "../mn_algorithm.hpp"
2324
#include "../mn_functional.hpp"
2425
#include "../mn_typetraits.hpp"
2526
#include "../utils/mn_alignment.hpp"
@@ -145,8 +146,9 @@ namespace mn {
145146
Type* construct(Args&&... args) {
146147
auto _size = sizeof(Type);
147148

148-
pointer _mem = allocate(_size, mn::alignment_for(_size) );
149-
return new (_mem) Type(mn::forward<Args>(args)...);
149+
void* _mem = allocate(_size, mn::alignment_for(_size) );
150+
151+
return ::new (_mem) Type(mn::forward<Args>(args)...);
150152
}
151153

152154
/**
@@ -160,7 +162,7 @@ namespace mn {
160162

161163
auto _size = sizeof(Type);
162164

163-
if(mn::is_class<Type>::value) address->~Type();
165+
mn::destruct<Type>(address);
164166
deallocate(address, _size, mn::alignment_for(_size));
165167
}
166168

include/allocator/mn_basic_deleter.hpp

+119-10
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,87 @@
2424
#include "mn_basic_allocator.hpp"
2525
#include "mn_allocator_typetraits.hpp"
2626

27+
#include "../mn_functional.hpp"
28+
2729
namespace mn {
2830
namespace memory {
29-
template<typename Type, class TAllocator>
31+
/**
32+
* @brief A Simple template for a deleter.
33+
* @tparam Type The type of pointer to delete.
34+
* @tparam TAllocator The using allocator for call deallocate
35+
*/
36+
template<typename Type, class TAllocator>
3037
class basic_deleter {
3138
public:
3239
using value_type = Type;
3340
using allocator = TAllocator;
3441
using pointer = TAllocator*;
3542
using reference = TAllocator&;
3643

44+
/**
45+
* @brief Construct a basic deleter, without a allocator
46+
* @note Atentation: call set_allocator first, otherwise the deleter not work
47+
*/
3748
constexpr basic_deleter() : m_refAllocator(nullptr) {}
38-
constexpr basic_deleter(reference alloc) noexcept
49+
/**
50+
* @brief Construt a basic deleter
51+
* @param alloc The using allocator class
52+
*/
53+
constexpr basic_deleter(reference alloc, size_t) noexcept
3954
: m_refAllocator(&alloc) { }
4055

41-
reference get_allocator() {
56+
/**
57+
* @brief Converting constructor.
58+
* Allows conversion from a deleter for arrays of another type, @p U,
59+
* only if @p U* is convertible to @p value_type*.
60+
*/
61+
template<typename U, typename = typename enable_if<is_convertible<U*,value_type*>::value>::type>
62+
basic_deleter(const basic_deleter<U, TAllocator>& other) noexcept
63+
: m_refAllocator(other.m_refAllocator) { }
64+
65+
/**
66+
* @brief Is the deleter valid - have a allocator
67+
* @return True when the deleter valid is anh false if not.
68+
*/
69+
bool is_valid() const noexcept {
70+
return m_refAllocator != nullptr;
71+
}
72+
73+
/**
74+
* @brief Get a reference from the using allocator.
75+
* @return The reference from the using allocator.
76+
*/
77+
reference get_allocator() {
4278
return *m_refAllocator;
4379
}
4480

45-
bool is_valid() const noexcept {
46-
return m_refAllocator != nullptr;
47-
}
81+
/**
82+
* @brief Set the using allocator for delete.
83+
* @param alloc The reference of the using allocator.
84+
*/
85+
void set_allocator(reference alloc) noexcept {
86+
m_refAllocator = &alloc;
87+
}
4888

49-
void operator()(value_type* pointer) noexcept {
50-
m_refAllocator.deallocate(pointer, sizeof(value_type), alignof(value_type));
89+
/**
90+
* @brief Calls @c deallocate @p pArray
91+
*/
92+
void operator()(value_type* pP) noexcept {
93+
static_assert(!mn::is_void<value_type>::value, "can't delete pP to incomplete type");
94+
static_assert(sizeof(value_type) > 0, "can't delete pP to incomplete type");
95+
96+
m_refAllocator->deallocate(pP, sizeof(value_type), alignof(value_type));
5197
}
5298
private:
5399
pointer m_refAllocator;
54100
};
55101

102+
/**
103+
* @brief A Simple template for a deleter.
104+
* @tparam Type The type of pointer to delete.
105+
* @tparam TAllocator The using allocator for call deallocate
106+
* @note Specialization for arrays
107+
*/
56108
template<typename Type, class TAllocator>
57109
class basic_deleter<Type[], TAllocator> {
58110
public:
@@ -61,25 +113,82 @@ namespace mn {
61113
using pointer = TAllocator*;
62114
using reference = TAllocator&;
63115

116+
template<typename U>
117+
using remove_cv_type = typename mn::remove_cv<U>::type;
118+
119+
/**
120+
* @brief Like is_base_of<_Tp, _Up> but false if unqualified types are the same
121+
*/
122+
template<typename U>
123+
using is_derived_Tp = integral_constant< bool, is_base_of<value_type, U>::value &
124+
!(is_same<remove_cv_type<value_type>, remove_cv_type<U>>::value )>;
125+
126+
/**
127+
* @brief Construct a basic deleter, without a allocator
128+
* @note Atentation: call set_allocator first, otherwise the deleter not work
129+
*/
64130
constexpr basic_deleter() : m_refAllocator(nullptr), m_sArraySize(0u) {}
131+
/**
132+
* @brief Construt a basic deleter
133+
* @param alloc The using allocator class
134+
* @param size The size of the array
135+
*/
65136
constexpr basic_deleter(reference alloc, size_t size) noexcept
66137
: m_refAllocator(&alloc), m_sArraySize(size) { }
67138

139+
/**
140+
* @brief Converting constructor.
141+
* Allows conversion from a deleter for arrays of another type, @p U,
142+
* only if @p U* is convertible to @p value_type*.
143+
*/
144+
template<typename U, typename = typename enable_if<is_convertible<U*, value_type*>::value>::type>
145+
basic_deleter(const basic_deleter<U[], TAllocator>& other) noexcept
146+
: m_refAllocator(other.m_refAllocator), m_sArraySize(other.m_sArraySize) { }
147+
148+
/**
149+
* @brief Is the deleter valid - have a allocator
150+
* @return True when the deleter valid is anh false if not.
151+
*/
68152
bool is_valid() const noexcept {
69153
return m_refAllocator != nullptr;
70154
}
71155

156+
/**
157+
* @brief Get a reference from the using allocator.
158+
* @return The reference from the using allocator.
159+
*/
72160
reference get_allocator() {
73161
return *m_refAllocator;
74162
}
75163

164+
/**
165+
* @brief Set the using allocator for delete.
166+
* @param alloc The reference of the using allocator.
167+
*/
168+
void set_allocator(reference alloc) noexcept {
169+
m_refAllocator = &alloc;
170+
}
171+
172+
/**
173+
* @brief Get the size of the array.
174+
* @return The size of the array.
175+
*/
76176
size_t get_size() const {
77177
return m_sArraySize;
78178
}
79179

80-
void operator()(value_type* pointer) noexcept {
81-
m_refAllocator.deallocate(pointer, m_sArraySize, sizeof(value_type), alignof(value_type));
180+
/**
181+
* @brief Calls @c deallocate @p pArray
182+
*/
183+
void operator()(value_type* pArray) noexcept {
184+
static_assert(sizeof(value_type) > 0, "can't delete pArray to incomplete type");
185+
m_refAllocator->deallocate(pArray, m_sArraySize, sizeof(value_type), alignof(value_type));
82186
}
187+
188+
189+
template<typename U>
190+
typename enable_if<is_derived_Tp<U>::value>::type operator()(value_type*) const = delete;
191+
83192
private:
84193
pointer m_refAllocator;
85194
size_t m_sArraySize;

0 commit comments

Comments
 (0)