24
24
#include " mn_basic_allocator.hpp"
25
25
#include " mn_allocator_typetraits.hpp"
26
26
27
+ #include " ../mn_functional.hpp"
28
+
27
29
namespace mn {
28
30
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 >
30
37
class basic_deleter {
31
38
public:
32
39
using value_type = Type;
33
40
using allocator = TAllocator;
34
41
using pointer = TAllocator*;
35
42
using reference = TAllocator&;
36
43
44
+ /* *
45
+ * @brief Construct a basic deleter, without a allocator
46
+ * @note Atentation: call set_allocator first, otherwise the deleter not work
47
+ */
37
48
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
39
54
: m_refAllocator(&alloc) { }
40
55
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 () {
42
78
return *m_refAllocator;
43
79
}
44
80
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
+ }
48
88
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));
51
97
}
52
98
private:
53
99
pointer m_refAllocator;
54
100
};
55
101
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
+ */
56
108
template <typename Type, class TAllocator >
57
109
class basic_deleter <Type[], TAllocator> {
58
110
public:
@@ -61,25 +113,82 @@ namespace mn {
61
113
using pointer = TAllocator*;
62
114
using reference = TAllocator&;
63
115
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
+ */
64
130
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
+ */
65
136
constexpr basic_deleter (reference alloc, size_t size) noexcept
66
137
: m_refAllocator(&alloc), m_sArraySize(size) { }
67
138
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
+ */
68
152
bool is_valid () const noexcept {
69
153
return m_refAllocator != nullptr ;
70
154
}
71
155
156
+ /* *
157
+ * @brief Get a reference from the using allocator.
158
+ * @return The reference from the using allocator.
159
+ */
72
160
reference get_allocator () {
73
161
return *m_refAllocator;
74
162
}
75
163
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
+ */
76
176
size_t get_size () const {
77
177
return m_sArraySize;
78
178
}
79
179
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));
82
186
}
187
+
188
+
189
+ template <typename U>
190
+ typename enable_if<is_derived_Tp<U>::value>::type operator ()(value_type*) const = delete;
191
+
83
192
private:
84
193
pointer m_refAllocator;
85
194
size_t m_sArraySize;
0 commit comments