bit::memory
allocator_deleter.hpp
1 /*****************************************************************************
2  * \file
3  * \brief This header contains the definition of an allocator deleter for use
4  * with smart pointer types
5  *****************************************************************************/
6 
7 /*
8  The MIT License (MIT)
9 
10  Copyright (c) 2018 Matthew Rodusek
11 
12  Permission is hereby granted, free of charge, to any person obtaining a copy
13  of this software and associated documentation files (the "Software"), to deal
14  in the Software without restriction, including without limitation the rights
15  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16  copies of the Software, and to permit persons to whom the Software is
17  furnished to do so, subject to the following conditions:
18 
19  The above copyright notice and this permission notice shall be included in
20  all copies or substantial portions of the Software.
21 
22  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28  SOFTWARE.
29 */
30 #ifndef BIT_MEMORY_ADAPTERS_ALLOCATOR_DELETER_HPP
31 #define BIT_MEMORY_ADAPTERS_ALLOCATOR_DELETER_HPP
32 
33 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
34 # pragma once
35 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
36 
37 #include "../utilities/ebo_storage.hpp"
38 #include "../utilities/pointer_utilities.hpp" // to_raw_pointer
39 
40 #include "../concepts/AllocatorStorage.hpp"
41 
42 #include "../traits/allocator_traits.hpp"
43 
44 #include <cstddef> // std::size_t, std::ptrdiff_t
45 #include <tuple> // std::forward_as_tuple
46 #include <type_traits> // std::is_reference, std::is_const, etc
47 #include <utility> // std::move
48 #include <memory> // std::pointer_traits
49 
50 namespace bit {
51  namespace memory {
52 
72  template<typename T, typename AllocatorStorage>
74  : private ebo_storage<AllocatorStorage>
75  {
77  using allocator_type = typename AllocatorStorage::allocator_type;
78 
80  using pointer_traits = std::pointer_traits<typename alloc_traits::pointer>;
81 
82  static_assert( !std::is_reference<T>::value, "Unable to delete reference type" );
83  static_assert( !std::is_const<T>::value, "Unable to delete const type" );
85  "AllocatorStorage must satisfy AllocatorStorage requirements" );
86 
87  //-----------------------------------------------------------------------
88  // Public Member Types
89  //-----------------------------------------------------------------------
90  public:
91 
92  using void_pointer = typename alloc_traits::pointer;
93  using pointer = typename pointer_traits::template rebind<T>;
94  using size_type = typename alloc_traits::size_type;
95  using difference_type = typename alloc_traits::difference_type;
96 
97  //-----------------------------------------------------------------------
98  // Constructor
99  //-----------------------------------------------------------------------
100  public:
101 
106  explicit allocator_deleter( AllocatorStorage storage );
107 
111  allocator_deleter( allocator_deleter&& other ) noexcept = default;
112 
116  allocator_deleter( const allocator_deleter& other ) noexcept = default;
117 
118  //-----------------------------------------------------------------------
119 
124  allocator_deleter& operator=( allocator_deleter&& other ) noexcept = default;
125 
130  allocator_deleter& operator=( const allocator_deleter& other ) noexcept = default;
131 
132  //-----------------------------------------------------------------------
133  // Deallocation
134  //-----------------------------------------------------------------------
135  public:
136 
140  void operator()( pointer p );
141  };
142 
143  //-------------------------------------------------------------------------
144 
145  template<typename T, typename AllocatorStorage>
146  class allocator_deleter<T[],AllocatorStorage>
147  : public ebo_storage<AllocatorStorage>
148  {
150  using allocator_type = typename AllocatorStorage::allocator_type;
151 
153  using pointer_traits = std::pointer_traits<typename alloc_traits::pointer>;
154 
155  //-----------------------------------------------------------------------
156  // Public Member Types
157  //-----------------------------------------------------------------------
158  public:
159 
160  using void_pointer = typename alloc_traits::pointer;
161  using pointer = typename pointer_traits::template rebind<T>;
162  using size_type = typename alloc_traits::size_type;
163  using difference_type = typename alloc_traits::difference_type;
164 
165  //-----------------------------------------------------------------------
166  // Constructor
167  //-----------------------------------------------------------------------
168  public:
169 
175  allocator_deleter( AllocatorStorage storage, size_type size );
176 
180  allocator_deleter( allocator_deleter&& other ) noexcept = default;
181 
185  allocator_deleter( const allocator_deleter& other ) noexcept = default;
186 
187  //-----------------------------------------------------------------------
188 
193  allocator_deleter& operator=( allocator_deleter&& other ) noexcept = default;
194 
199  allocator_deleter& operator=( const allocator_deleter& other ) noexcept = default;
200 
201  //-----------------------------------------------------------------------
202  // Deallocation
203  //-----------------------------------------------------------------------
204  public:
205 
209  void operator()( pointer p );
210 
211  //-----------------------------------------------------------------------
212  // Private Members
213  //-----------------------------------------------------------------------
214  private:
215 
216  std::size_t m_size;
217  };
218 
219  } // namespace memory
220 } // namespace bit
221 
222 #include "detail/allocator_deleter.inl"
223 
224 #endif /* BIT_MEMORY_ADAPTERS_ALLOCATOR_DELETER_HPP */
Type trait to determine whether T is a AllocatorStorage.
The allocator_traits class template provides a standardized way to access allocator functionality...
allocator_deleter & operator=(allocator_deleter &&other) noexcept=default
Move-assigns an allocator_deleter from an existing one.
A deleter for custom allocators in the library.
void operator()(pointer p)
Deletes the underlying pointer p.
allocator_deleter(AllocatorStorage storage)
Constructs an allocator_deleter that uses the referenced allocator for deletion.
A utility class used for leveraging empty-base optimization for a generic sequence of types...
Definition: ebo_storage.hpp:62