mn_value_ptr.hpp
Go to the documentation of this file.
1 
21 #ifndef __MINILIB_BASIC_VALUE_PTR_H__
22 #define __MINILIB_BASIC_VALUE_PTR_H__
23 
24 #include "../mn_config.hpp"
25 #include "../mn_hash.hpp"
26 
27 #include "mn_clone_ptr.hpp"
28 
29 
30 namespace mn {
31  namespace pointer {
32 
33  template <typename T, class TCloner = basic_default_clone<T>, class TDeleter = memory::default_delete<T>>
35  public:
36  using value_type = T;
37  using const_type = const T;
38  using reference = T&;
39  using const_reference = const T&;
40  using pointer = T*;
41  using const_pointer = const T*;
42 
43  using cloner_type = TCloner;
44  using deleter_type = TDeleter;
45 
47 
48  basic_value_ptr() noexcept
49  : m_ptr( cloner_type(), deleter_type() ) {}
50 
52  : m_ptr( cloner_type(), deleter_type() ) {}
53 
54  basic_value_ptr( pointer p ) noexcept
55  : m_ptr( p ) { }
56 
57  basic_value_ptr( const cloner_type& cloner )
58  : m_ptr( cloner ) { }
59 
60  basic_value_ptr(const deleter_type& deleter )
61  : m_ptr( deleter ) { }
62 
63  basic_value_ptr(const cloner_type& cloner, const deleter_type& deleter )
64  : m_ptr( cloner, deleter ) { }
65 
66  basic_value_ptr(const cloner_type&& cloner )
67  : m_ptr( mn::move( cloner ) ) { }
68 
69  basic_value_ptr(const deleter_type&& deleter )
70  : m_ptr( mn::move( deleter ) ) { }
71 
72  basic_value_ptr(const cloner_type&& cloner, const deleter_type&& deleter )
73  : m_ptr( mn::move( cloner ), mn::move( deleter ) ) { }
74 
75 
76  basic_value_ptr(const self_type& other )
77  : m_ptr( other.m_ptr ) {}
78 
80  : m_ptr( value ) { }
81 
82  basic_value_ptr(const self_type&& other )
83  : m_ptr( mn::move(other.m_ptr) ) {}
84 
86  : m_ptr( mn::move(value )) { }
87 
88  template< class... Args>
89  basic_value_ptr(Args&&... args)
90  : m_ptr( mn::forward<Args>(args)...) { }
91 
92  template< typename U, class... Args>
94  : m_ptr(il, std::forward<Args>(args)...) { }
95 
96  template <typename V, class VClonerTDeleter>
97  basic_value_ptr( const V& value, const VClonerTDeleter & obj )
98  : m_ptr( value, obj ) {}
99 
100  template< typename V, class VCloner, class VDeleter >
101  basic_value_ptr(const V& value, const VCloner& cloner, const VDeleter& deleter )
102  : m_ptr(value, cloner, deleter) { }
103 
104  ~basic_value_ptr() = default;
105 
106  pointer get() const noexcept { return m_ptr.get(); }
107  cloner_type& get_cloner() noexcept { return m_ptr.get_cloner(); }
108  deleter_type& get_deleter() noexcept { return m_ptr.get_deleter(); }
109 
110  bool has_value() const noexcept { return !! get(); }
111  const_reference value() const { assert(has_value()); return *get(); }
112  reference value() { assert(has_value()); return *get(); }
113 
114  pointer release() noexcept { return m_ptr.release(); }
115 
116  void reset(pointer p = pointer() ) noexcept {
117  m_ptr.reset( p );
118  }
119 
120  void swap( basic_value_ptr & other ) noexcept {
121  m_ptr.swap( other.m_ptr );
122  }
123 
124  template< class U >
126  return has_value() ? *(m_ptr.get()) : static_cast<value_type>(mn::forward<U>( v ) );
127  }
128 
129 
130  template< class... Args >
131  void emplace( Args&&... args ) {
132  m_ptr.reset( T( mn::forward<Args>(args)...) );
133  }
134 
135  template< class U, class... Args >
136  void emplace( mn::initializer_list<U> il, Args&&... args ) {
137  m_ptr.reset( T( il, std::forward<Args>(args)...) );
138  }
139 
140  reference operator*() const { assert( get() != nullptr ); return *get(); }
141  pointer operator->() const noexcept { assert( get() != nullptr ); return get(); }
142 
143  explicit operator bool() const noexcept { return has_value(); }
144 
146  m_ptr.reset(); return *this;
147  }
148 
150  m_ptr.reset(value); return *this;
151  }
152 
154  if ( this == &rhs ) return *this;
155 
156  if ( rhs ) { m_ptr.reset( *rhs ); }
157  else { m_ptr.reset( nullptr ); }
158 
159  return *this;
160  }
161 
162  self_type& operator = (self_type&& rhs ) noexcept {
163  if ( this == &rhs ) return *this;
164 
165  swap(rhs);
166 
167  return *this;
168  }
169 
170  template< class U,
173  m_ptr.reset( mn::forward<U>( value ) ); return *this;
174  }
175  private:
177  };
178 
179  template< class T >
181  return basic_value_ptr< typename mn::decay<T>::type >( mn::forward<T>( v ) );
182  }
183 
184  template< class T, class... Args >
185  inline basic_value_ptr<T> make_value( Args&&... args ) {
186  return basic_value_ptr<T>(mn::forward<Args>(args)...);
187  }
188 
189  template< class T, class U, class... Args >
191  return basic_value_ptr<T>(il, mn::forward<Args>(args)...);
192  }
193 
194  template< class T, class D, class C >
195  inline void swap( basic_value_ptr<T, D, C> & a, basic_value_ptr<T, D, C> & b ) noexcept {
196  a.swap( b );
197  }
198 
199  template <typename T, class TCloner = basic_default_clone<T>, class TDeleter = memory::default_delete<T>>
201  }
202 }
203 
204 namespace mn {
205 
206  template< class T, class D, class C >
207  struct hash< mn::pointer::value_ptr<T, D, C> > {
209  using pointer = typename argument_type::pointer;
210 
211  result_type operator()( const argument_type& p ) const noexcept {
212  return hash<pointer>()( p.get() );
213  }
214  };
215 
216 } // namespace std
217 
218 
219 #endif // __MINILIB_BASIC_VALUE_PTR_H__
Definition: mn_initializer_list.hpp:26
Definition: mn_clone_ptr.hpp:61
Definition: mn_value_ptr.hpp:34
pointer release() noexcept
Definition: mn_value_ptr.hpp:114
basic_value_ptr(mn::nullptr_t) noexcept
Definition: mn_value_ptr.hpp:51
pointer get() const noexcept
Definition: mn_value_ptr.hpp:106
self_type & operator=(mn::nullptr_t) noexcept
Definition: mn_value_ptr.hpp:145
T & reference
Definition: mn_value_ptr.hpp:38
bool has_value() const noexcept
Definition: mn_value_ptr.hpp:110
basic_value_ptr(Args &&... args)
Definition: mn_value_ptr.hpp:89
mn::pointer::clone_ptr< value_type, cloner_type, deleter_type > m_ptr
Definition: mn_value_ptr.hpp:176
pointer operator->() const noexcept
Definition: mn_value_ptr.hpp:141
basic_value_ptr(pointer p) noexcept
Definition: mn_value_ptr.hpp:54
const T & const_reference
Definition: mn_value_ptr.hpp:39
basic_value_ptr(const deleter_type &deleter)
Definition: mn_value_ptr.hpp:60
void reset(pointer p=pointer()) noexcept
Definition: mn_value_ptr.hpp:116
basic_value_ptr(mn::initializer_list< U > il, Args &&... args)
Definition: mn_value_ptr.hpp:93
TDeleter deleter_type
Definition: mn_value_ptr.hpp:44
T value_type
Definition: mn_value_ptr.hpp:36
basic_value_ptr(const self_type &other)
Definition: mn_value_ptr.hpp:76
void emplace(Args &&... args)
Definition: mn_value_ptr.hpp:131
void emplace(mn::initializer_list< U > il, Args &&... args)
Definition: mn_value_ptr.hpp:136
TCloner cloner_type
Definition: mn_value_ptr.hpp:43
deleter_type & get_deleter() noexcept
Definition: mn_value_ptr.hpp:108
basic_value_ptr(const V &value, const VCloner &cloner, const VDeleter &deleter)
Definition: mn_value_ptr.hpp:101
basic_value_ptr(const_reference value)
Definition: mn_value_ptr.hpp:79
cloner_type & get_cloner() noexcept
Definition: mn_value_ptr.hpp:107
basic_value_ptr() noexcept
Definition: mn_value_ptr.hpp:48
reference operator*() const
Definition: mn_value_ptr.hpp:140
basic_value_ptr(const cloner_type &cloner, const deleter_type &deleter)
Definition: mn_value_ptr.hpp:63
basic_value_ptr(const cloner_type &&cloner)
Definition: mn_value_ptr.hpp:66
basic_value_ptr(const V &value, const VClonerTDeleter &obj)
Definition: mn_value_ptr.hpp:97
T * pointer
Definition: mn_value_ptr.hpp:40
const_reference value() const
Definition: mn_value_ptr.hpp:111
basic_value_ptr(const cloner_type &cloner)
Definition: mn_value_ptr.hpp:57
value_type value_or(U &&v)
Definition: mn_value_ptr.hpp:125
basic_value_ptr(const self_type &&other)
Definition: mn_value_ptr.hpp:82
basic_value_ptr(const value_type &&value)
Definition: mn_value_ptr.hpp:85
basic_value_ptr(const cloner_type &&cloner, const deleter_type &&deleter)
Definition: mn_value_ptr.hpp:72
basic_value_ptr(const deleter_type &&deleter)
Definition: mn_value_ptr.hpp:69
const T const_type
Definition: mn_value_ptr.hpp:37
reference value()
Definition: mn_value_ptr.hpp:112
void swap(basic_value_ptr &other) noexcept
Definition: mn_value_ptr.hpp:120
const T * const_pointer
Definition: mn_value_ptr.hpp:41
struct mn::memory::detail::ptr_difference T
Definition: mn_atomic_singleton.hpp:38
struct mn::memory::detail::ptr_difference type
void swap(basic_auto_ptr< T > &a, basic_auto_ptr< T > &b)
Definition: mn_auto_ptr.hpp:188
basic_value_ptr< typename mn::decay< T >::type > make_value(T &&v)
Definition: mn_value_ptr.hpp:180
Definition: mn_allocator_typetraits.hpp:25
mn::size_t result_type
Definition: mn_hash.hpp:30
decltype(nullptr) nullptr_t
Definition: mn_def.hpp:45
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_functional.hpp:188
result_type operator()(const argument_type &p) const noexcept
Definition: mn_value_ptr.hpp:211
typename argument_type::pointer pointer
Definition: mn_value_ptr.hpp:209
Default implementation of hasher.
Definition: mn_hash.hpp:72