mn_basic_deleter.hpp
Go to the documentation of this file.
1 /*
2 *This file is part of the Mini Thread Library (https://github.com/RoseLeBlood/MiniThread ).
3 *Copyright (c) 2018 Amber-Sophia Schroeck
4 *
5 *The Mini Thread Library is free software; you can redistribute it and/or modify
6 *it under the terms of the GNU Lesser General Public License as published by
7 *the Free Software Foundation, version 3, or (at your option) any later version.
8 
9 *The Mini Thread Library is distributed in the hope that it will be useful, but
10 *WITHOUT ANY WARRANTY; without even the implied warranty of
11 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 *General Public License for more details.
13 *
14 *You should have received a copy of the GNU Lesser General Public
15 *License along with the Mini Thread Library; if not, see
16 *<https://www.gnu.org/licenses/>.
17 */
18 #ifndef __MINLIB_BASIC_DELETER_H__
19 #define __MINLIB_BASIC_DELETER_H__
20 
21 
22 #include "../mn_config.hpp"
23 
24 #include "mn_basic_allocator.hpp"
26 
27 #include "../mn_functional.hpp"
28 
29 namespace mn {
30  namespace memory {
36  template<typename Type, class TAllocator>
37  class basic_deleter {
38  public:
39  using value_type = Type;
40  using allocator = TAllocator;
41  using pointer = TAllocator*;
42  using reference = TAllocator&;
43 
47  constexpr basic_deleter() : m_refAllocator() {}
52  constexpr basic_deleter(reference alloc, size_t) noexcept
53  : m_refAllocator(&alloc) { }
54 
60  template<typename U, typename = typename enable_if<is_convertible<U*,value_type*>::value>::type>
62  : m_refAllocator(other.m_refAllocator) { }
63 
68  bool is_valid() const noexcept {
69  return m_refAllocator != nullptr;
70  }
71 
77  return *m_refAllocator;
78  }
79 
84  void set_allocator(reference alloc) noexcept {
85  m_refAllocator = &alloc;
86  }
87 
91  void operator()(value_type* pP) noexcept {
92  static_assert(!mn::is_void<value_type>::value, "can't delete pP to incomplete type");
93  static_assert(sizeof(value_type) > 0, "can't delete pP to incomplete type");
94 
95  m_refAllocator->deallocate(pP, sizeof(value_type), alignof(value_type));
96  }
97  private:
99  };
100 
107  template<typename Type, class TAllocator>
108  class basic_deleter<Type[], TAllocator> {
109  public:
110  using value_type = Type;
111  using allocator = TAllocator;
112  using pointer = TAllocator*;
113  using reference = TAllocator&;
114 
115  template<typename U>
117 
121  template<typename U>
124 
128  constexpr basic_deleter() : m_refAllocator(), m_sArraySize(0u) {}
134  constexpr basic_deleter(reference alloc, size_t size) noexcept
135  : m_refAllocator(&alloc), m_sArraySize(size) { }
136 
142  template<typename U, typename = typename enable_if<is_convertible<U*, value_type*>::value>::type>
143  basic_deleter(const basic_deleter<U[], TAllocator>& other) noexcept
144  : m_refAllocator(other.m_refAllocator), m_sArraySize(other.m_sArraySize) { }
145 
150  bool is_valid() const noexcept {
151  return m_refAllocator != nullptr;
152  }
153 
159  return *m_refAllocator;
160  }
161 
166  void set_allocator(reference alloc) noexcept {
167  m_refAllocator = &alloc;
168  }
169 
174  size_t get_size() const {
175  return m_sArraySize;
176  }
177 
181  void operator()(value_type* pArray) noexcept {
182  static_assert(sizeof(value_type) > 0, "can't delete pArray to incomplete type");
183  m_refAllocator->deallocate(pArray, m_sArraySize, sizeof(value_type), alignof(value_type));
184  }
185 
186 
187  template<typename U>
189 
190  private:
192  size_t m_sArraySize;
193  };
194  }
195 }
196 
197 
198 #endif // __MINLIB_BASIC_DELETER_H__
size_t m_sArraySize
Definition: mn_basic_deleter.hpp:192
typename mn::remove_cv< U >::type remove_cv_type
Definition: mn_basic_deleter.hpp:116
TAllocator & reference
Definition: mn_basic_deleter.hpp:113
constexpr basic_deleter(reference alloc, size_t size) noexcept
Construt a basic deleter.
Definition: mn_basic_deleter.hpp:134
basic_deleter(const basic_deleter< U[], TAllocator > &other) noexcept
Converting constructor. Allows conversion from a deleter for arrays of another type,...
Definition: mn_basic_deleter.hpp:143
void set_allocator(reference alloc) noexcept
Set the using allocator for delete.
Definition: mn_basic_deleter.hpp:166
void operator()(value_type *pArray) noexcept
Calls deallocate pArray.
Definition: mn_basic_deleter.hpp:181
size_t get_size() const
Get the size of the array.
Definition: mn_basic_deleter.hpp:174
enable_if< is_derived_Tp< U >::value >::type operator()(value_type *) const =delete
pointer m_refAllocator
Definition: mn_basic_deleter.hpp:191
TAllocator * pointer
Definition: mn_basic_deleter.hpp:112
constexpr basic_deleter()
Construct a basic deleter.
Definition: mn_basic_deleter.hpp:128
reference get_allocator()
Get a reference from the using allocator.
Definition: mn_basic_deleter.hpp:158
Type value_type
Definition: mn_basic_deleter.hpp:110
TAllocator allocator
Definition: mn_basic_deleter.hpp:111
bool is_valid() const noexcept
Is the deleter valid - have a allocator.
Definition: mn_basic_deleter.hpp:150
A Simple template for a deleter.
Definition: mn_basic_deleter.hpp:37
basic_deleter(const basic_deleter< U, TAllocator > &other) noexcept
Converting constructor. Allows conversion from a deleter for arrays of another type,...
Definition: mn_basic_deleter.hpp:61
Type value_type
Definition: mn_basic_deleter.hpp:39
TAllocator * pointer
Definition: mn_basic_deleter.hpp:41
TAllocator allocator
Definition: mn_basic_deleter.hpp:40
constexpr basic_deleter()
Construct a basic deleter.
Definition: mn_basic_deleter.hpp:47
bool is_valid() const noexcept
Is the deleter valid - have a allocator.
Definition: mn_basic_deleter.hpp:68
constexpr basic_deleter(reference alloc, size_t) noexcept
Construt a basic deleter.
Definition: mn_basic_deleter.hpp:52
reference get_allocator()
Get a reference from the using allocator.
Definition: mn_basic_deleter.hpp:76
void set_allocator(reference alloc) noexcept
Set the using allocator for delete.
Definition: mn_basic_deleter.hpp:84
void operator()(value_type *pP) noexcept
Calls deallocate pArray.
Definition: mn_basic_deleter.hpp:91
TAllocator & reference
Definition: mn_basic_deleter.hpp:42
pointer m_refAllocator
Definition: mn_basic_deleter.hpp:98
struct mn::memory::detail::ptr_difference type
Definition: mn_allocator_typetraits.hpp:25
Definition: mn_functional.hpp:188
Definition: mn_typetraits.hpp:125
Definition: mn_functional.hpp:211
Definition: mn_typetraits.hpp:300
T type
Definition: mn_functional.hpp:32