mn_save_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) 2018-2020 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 _MINILIB_7bee8ff5_abe8_4176_bc96_f42b0e71632b_H_
19 #define _MINILIB_7bee8ff5_abe8_4176_bc96_f42b0e71632b_H_
20 
21 #include "../mn_config.hpp"
22 #include "../mn_def.hpp"
23 
24 
25 namespace mn {
26  namespace pointer {
27 
28  template <typename T>
30  public:
31  using value_type = T;
32  using element_type = T;
33  using pointer = T*;
34  using reference = T&;
36 
37  basic_save_ptr() : m_ptr(0) { }
38  explicit basic_save_ptr(T *v) : m_ptr(v) { }
39 
40  ~basic_save_ptr() { if(m_ptr) delete m_ptr; }
41 
42  value_type const *get() const { return m_ptr; }
43  value_type *get() { return m_ptr; }
44 
45  value_type const &operator *() const { return *m_ptr; }
46  value_type &operator *() { return *m_ptr; }
47  value_type const *operator->() const { return m_ptr; }
48  value_type *operator->() { return m_ptr; }
49 
51  value_type *tmp = m_ptr;
52  m_ptr = NULL;
53  return tmp;
54  }
55  void reset(value_type *p = NULL) {
56  if(m_ptr != NULL) delete m_ptr;
57  m_ptr = p;
58  }
59  void swap(self_type &other) {
60  value_type *tmp = other.m_ptr;
61  other.m_ptr = m_ptr;
62  m_ptr = tmp;
63  }
64 
65  basic_save_ptr(const basic_save_ptr &other) = delete;
66  basic_save_ptr const &operator=(const basic_save_ptr &other) = delete;
67 
68  private:
70  };
71 
72  template <typename T>
74 
75 
81  template<typename T, typename... Args >
82  inline save_ptr<T> make_save(Args&&... args) {
83  return save_ptr<T>(new T (mn::forward<Args>(args)...) );
84  }
85 
89  template <typename T>
90  inline void swap(save_ptr<T> & a, save_ptr<T> & b) {
91  a.swap(b);
92  }
93 
94  }
95 }
96 
97 
98 #endif
Definition: mn_save_ptr.hpp:29
basic_save_ptr()
Definition: mn_save_ptr.hpp:37
value_type const * operator->() const
Definition: mn_save_ptr.hpp:47
~basic_save_ptr()
Definition: mn_save_ptr.hpp:40
value_type const & operator*() const
Definition: mn_save_ptr.hpp:45
T & reference
Definition: mn_save_ptr.hpp:34
value_type const * get() const
Definition: mn_save_ptr.hpp:42
value_type * release()
Definition: mn_save_ptr.hpp:50
T value_type
Definition: mn_save_ptr.hpp:31
T * pointer
Definition: mn_save_ptr.hpp:33
pointer m_ptr
Definition: mn_save_ptr.hpp:69
basic_save_ptr(T *v)
Definition: mn_save_ptr.hpp:38
void reset(value_type *p=NULL)
Definition: mn_save_ptr.hpp:55
T element_type
Definition: mn_save_ptr.hpp:32
basic_save_ptr(const basic_save_ptr &other)=delete
void swap(self_type &other)
Definition: mn_save_ptr.hpp:59
value_type * get()
Definition: mn_save_ptr.hpp:43
value_type * operator->()
Definition: mn_save_ptr.hpp:48
basic_save_ptr const & operator=(const basic_save_ptr &other)=delete
struct mn::memory::detail::ptr_difference T
Definition: mn_atomic_singleton.hpp:38
void swap(basic_auto_ptr< T > &a, basic_auto_ptr< T > &b)
Definition: mn_auto_ptr.hpp:188
save_ptr< T > make_save(Args &&... args)
Make a save pointer.
Definition: mn_save_ptr.hpp:82
Definition: mn_allocator_typetraits.hpp:25