mn_clone_ptr.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) 2021 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_ASTL_CLONE_PTR_
19 #define MINLIB_ASTL_CLONE_PTR_
20 
21 #include "../mn_config.hpp"
22 
23 #include "../mn_algorithm.hpp"
24 #include "../mn_deleter.hpp"
25 #include <stdlib.h>
26 
27 
28 
29 namespace mn {
30  namespace pointer {
31 
32  template<typename T>
34  protected:
35  using pointer = T*;
36  using value_type = T;
37  using reference = T&;
38  using const_reference = const T&;
39 
40  basic_default_clone() = default;
41 
43  static_assert( ! mn::is_void<T>::value, "basic_default_clone cannot clone incomplete type");
44 
45  return new T( x );
46  }
47  pointer operator()(T&& x ) const {
48  return new T( mn::move( x ) );
49  }
50  template< class... Args >
51  pointer operator()(Args&&... args ) const {
52  return new T( mn::forward<Args>(args)...);
53  }
54  template< class U, class... Args >
55  pointer operator()(mn::initializer_list<U> il, Args&&... args ) const {
56  return new T( il, mn::forward<Args>(args)...);
57  }
58  };
59 
60  template<typename T, class TCloner = basic_default_clone<T>, class TDeleter = memory::default_delete<T> >
61  class basic_clone_ptr : TCloner, TDeleter {
62  public:
63  using value_type = T;
64  using element_type = T;
65  using pointer = T*;
66  using reference = T&;
68  using cloner_type = TCloner;
69  using deleter_type = TDeleter;
70 
71  basic_clone_ptr() noexcept
72  : m_ptr( nullptr ) {}
73 
74  basic_clone_ptr( pointer p ) noexcept
75  : m_ptr( p ) {}
76 
77  basic_clone_ptr(const cloner_type& cloner )
78  : cloner_type( cloner ), m_ptr( nullptr ) {}
79 
80  basic_clone_ptr(const deleter_type& deleter )
81  : deleter_type( deleter ), m_ptr( nullptr ) {}
82 
83  basic_clone_ptr(const cloner_type& cloner, const deleter_type& deleter )
84  : cloner_type( cloner ), deleter_type( deleter ), m_ptr( nullptr ) { }
85 
86  basic_clone_ptr(const cloner_type&& cloner )
87  : cloner_type( mn::move(cloner) ), m_ptr( nullptr ) {}
88 
89  basic_clone_ptr(const deleter_type&& deleter )
90  : deleter_type( mn::move(deleter) ), m_ptr( nullptr ) {}
91 
92  basic_clone_ptr(const cloner_type&& cloner, const deleter_type&& deleter )
93  : cloner_type( mn::move(cloner) ), deleter_type( mn::move(deleter) ), m_ptr( nullptr ) { }
94 
95  basic_clone_ptr( const self_type& other )
96  : cloner_type ( other ),
97  deleter_type( other ),
98  m_ptr( other.m_ptr ? cloner_type()( *other.m_ptr ) : nullptr ) {}
99 
100  basic_clone_ptr(const self_type&& other )
101  : cloner_type ( mn::move(other) ),
102  deleter_type( mn::move(other) ),
103  m_ptr( mn::move( other.m_ptr ) ) { }
104 
106  : m_ptr( cloner_type()( value ) ) {}
107 
108  basic_clone_ptr(const value_type&& value )
109  : m_ptr( cloner_type()( mn::move(value )) ) {}
110 
111  template< class... Args >
112  explicit basic_clone_ptr( Args&&... args )
113  : m_ptr( cloner_type()( mn::forward<Args>(args)...) ) {}
114 
115  template< class U, class... Args >
116  explicit basic_clone_ptr( mn::initializer_list<U> il, Args&&... args )
117  : m_ptr( cloner_type()(il, std::forward<Args>(args)...) ) { }
118 
119  basic_clone_ptr( const value_type& value, const cloner_type & cloner )
120  : cloner_type ( cloner ) , m_ptr( cloner_type()( value ) ) {}
121 
122  basic_clone_ptr( const value_type&& value, const cloner_type && cloner )
123  : cloner_type ( mn::move(cloner) ) , m_ptr( cloner_type()( mn::move(value ) ) ) {}
124 
125  basic_clone_ptr(const value_type& value, const cloner_type& cloner, const deleter_type& deleter )
126  : cloner_type ( cloner ), deleter_type( deleter ), m_ptr( cloner_type()( value ) ) {}
127 
128  basic_clone_ptr(const value_type&& value, const cloner_type&& cloner, const deleter_type&& deleter )
129  : cloner_type ( mn::move(cloner) ),
130  deleter_type( mn::move(deleter) ),
131  m_ptr( cloner_type()( mn::move(value) ) ) {}
132 
133 
134 
136 
137  pointer get() { return m_ptr; }
138 
139  cloner_type& get_cloner() noexcept { return *this; }
140  deleter_type& get_deleter() noexcept { return *this; }
141 
142  value_type operator *() { return *m_ptr; }
143  pointer operator->() { return m_ptr; }
144 
145 
146  pointer release() noexcept {
147  pointer result = nullptr;
148  mn::swap( result, m_ptr );
149  return result;
150  }
151 
152  void reset(pointer p ) noexcept {
153  get_deleter()( m_ptr );
154  m_ptr = p;
155  }
156 
157  void reset(const value_type& v ) {
158  reset( get_cloner()( v ) );
159  }
160  void reset(const value_type&& v ) {
161  reset( get_cloner()( mn::move(v) ) );
162  }
163 
164  void swap(self_type& other ) noexcept {
165  mn::swap( m_ptr, other.m_ptr );
166  }
167  private:
169  };
170 
171  template<typename T, class TCloner = basic_default_clone<T>, class TDeleter = memory::default_delete<T> >
173  a.swap(b);
174  }
175 
176  template<typename T, class TCloner = basic_default_clone<T>, class TDeleter = memory::default_delete<T> >
178 
179  template<typename T>
181 
187  template<typename T, typename... Args >
188  inline clone_ptr<T> make_clone(Args&&... args) {
189  return clone_ptr<T>(new T (mn::forward<Args>(args)...) );
190  }
191  }
192 }
193 
194 #endif
Definition: mn_initializer_list.hpp:26
Type value_type
Definition: mn_basic_deleter.hpp:39
TAllocator * pointer
Definition: mn_basic_deleter.hpp:41
TAllocator & reference
Definition: mn_basic_deleter.hpp:42
Definition: mn_clone_ptr.hpp:61
TDeleter deleter_type
Definition: mn_clone_ptr.hpp:69
basic_clone_ptr(const value_type &&value)
Definition: mn_clone_ptr.hpp:108
cloner_type & get_cloner() noexcept
Definition: mn_clone_ptr.hpp:139
~basic_clone_ptr()
Definition: mn_clone_ptr.hpp:135
basic_clone_ptr(Args &&... args)
Definition: mn_clone_ptr.hpp:112
basic_clone_ptr(const value_type &&value, const cloner_type &&cloner, const deleter_type &&deleter)
Definition: mn_clone_ptr.hpp:128
basic_clone_ptr(const value_type &value, const cloner_type &cloner)
Definition: mn_clone_ptr.hpp:119
basic_clone_ptr(const value_type &&value, const cloner_type &&cloner)
Definition: mn_clone_ptr.hpp:122
pointer get()
Definition: mn_clone_ptr.hpp:137
pointer release() noexcept
Definition: mn_clone_ptr.hpp:146
basic_clone_ptr(mn::initializer_list< U > il, Args &&... args)
Definition: mn_clone_ptr.hpp:116
TCloner cloner_type
Definition: mn_clone_ptr.hpp:68
void reset(pointer p) noexcept
Definition: mn_clone_ptr.hpp:152
basic_clone_ptr(const self_type &&other)
Definition: mn_clone_ptr.hpp:100
value_type operator*()
Definition: mn_clone_ptr.hpp:142
T element_type
Definition: mn_clone_ptr.hpp:64
basic_clone_ptr(const value_type &value)
Definition: mn_clone_ptr.hpp:105
void swap(self_type &other) noexcept
Definition: mn_clone_ptr.hpp:164
void reset(const value_type &&v)
Definition: mn_clone_ptr.hpp:160
pointer operator->()
Definition: mn_clone_ptr.hpp:143
basic_clone_ptr(const cloner_type &&cloner)
Definition: mn_clone_ptr.hpp:86
basic_clone_ptr(const cloner_type &cloner)
Definition: mn_clone_ptr.hpp:77
basic_clone_ptr(const value_type &value, const cloner_type &cloner, const deleter_type &deleter)
Definition: mn_clone_ptr.hpp:125
basic_clone_ptr(const deleter_type &&deleter)
Definition: mn_clone_ptr.hpp:89
basic_clone_ptr() noexcept
Definition: mn_clone_ptr.hpp:71
void reset(const value_type &v)
Definition: mn_clone_ptr.hpp:157
pointer m_ptr
Definition: mn_clone_ptr.hpp:168
basic_clone_ptr(const self_type &other)
Definition: mn_clone_ptr.hpp:95
basic_clone_ptr(const cloner_type &cloner, const deleter_type &deleter)
Definition: mn_clone_ptr.hpp:83
basic_clone_ptr(const deleter_type &deleter)
Definition: mn_clone_ptr.hpp:80
deleter_type & get_deleter() noexcept
Definition: mn_clone_ptr.hpp:140
basic_clone_ptr(const cloner_type &&cloner, const deleter_type &&deleter)
Definition: mn_clone_ptr.hpp:92
basic_clone_ptr(pointer p) noexcept
Definition: mn_clone_ptr.hpp:74
struct mn::memory::detail::ptr_difference T
Definition: mn_atomic_singleton.hpp:38
clone_ptr< T > make_clone(Args &&... args)
Make a clone pointer.
Definition: mn_clone_ptr.hpp:188
void swap(basic_auto_ptr< T > &a, basic_auto_ptr< T > &b)
Definition: mn_auto_ptr.hpp:188
Definition: mn_allocator_typetraits.hpp:25
void swap(TAssignable &a, TAssignable &b)
Definition: mn_algorithm.hpp:312
void move(const T *src, const T *last, T *dest)
Definition: mn_algorithm.hpp:100
T && forward(remove_reference_t< T > &t)
Definition: mn_functional.hpp:74
Definition: mn_typetraits.hpp:300
Definition: mn_clone_ptr.hpp:33
pointer operator()(Args &&... args) const
Definition: mn_clone_ptr.hpp:51
T * pointer
Definition: mn_clone_ptr.hpp:35
const T & const_reference
Definition: mn_clone_ptr.hpp:38
T & reference
Definition: mn_clone_ptr.hpp:37
T value_type
Definition: mn_clone_ptr.hpp:36
pointer operator()(T &&x) const
Definition: mn_clone_ptr.hpp:47
pointer operator()(mn::initializer_list< U > il, Args &&... args) const
Definition: mn_clone_ptr.hpp:55
pointer operator()(const_reference x) const
Definition: mn_clone_ptr.hpp:42