1 // This file is part of the ustl library, an STL implementation.
3 // Copyright (C) 2005 by Mike Sharov <msharov@users.sourceforge.net>
4 // This file is free software, distributed under the MIT License.
9 #ifndef UMEMORY_H_4AB5B0DB5BF09140541409CC47BCD17A
10 #define UMEMORY_H_4AB5B0DB5BF09140541409CC47BCD17A
19 #include "uiterator.h"
24 /// \class auto_ptr umemory.h ustl.h
25 /// \ingroup MemoryManagement
27 /// \brief A smart pointer.
29 /// Calls delete in the destructor; assignment transfers ownership.
30 /// This class does not work with void pointers due to the absence
31 /// of the required dereference operator.
40 /// Takes ownership of \p p.
41 inline explicit auto_ptr (pointer p
= NULL
) : m_p (p
) {}
42 /// Takes ownership of pointer in \p p. \p p relinquishes ownership.
43 inline auto_ptr (auto_ptr
<T
>& p
) : m_p (p
.release()) {}
44 /// Deletes the owned pointer.
45 inline ~auto_ptr (void) { delete m_p
; }
46 /// Returns the pointer without relinquishing ownership.
47 inline pointer
get (void) const { return (m_p
); }
48 /// Returns the pointer and gives up ownership.
49 inline pointer
release (void) { pointer
rv (m_p
); m_p
= NULL
; return (rv
); }
50 /// Deletes the pointer and sets it equal to \p p.
51 inline void reset (pointer p
) { if (p
!= m_p
) { delete m_p
; m_p
= p
; } }
52 /// Takes ownership of \p p.
53 inline auto_ptr
<T
>& operator= (pointer p
) { reset (p
); return (*this); }
54 /// Takes ownership of pointer in \p p. \p p relinquishes ownership.
55 inline auto_ptr
<T
>& operator= (auto_ptr
<T
>& p
) { reset (p
.release()); return (*this); }
56 inline reference
operator* (void) const { return (*m_p
); }
57 inline pointer
operator-> (void) const { return (m_p
); }
58 inline bool operator== (const pointer p
) const { return (m_p
== p
); }
59 inline bool operator== (const auto_ptr
<T
>& p
) const { return (m_p
== p
.m_p
); }
60 inline bool operator< (const auto_ptr
<T
>& p
) const { return (p
.m_p
< m_p
); }
65 /// Calls the placement new on \p p.
66 /// \ingroup RawStorageAlgorithms
69 inline void construct (T
* p
)
74 /// Calls the placement new on \p p.
75 /// \ingroup RawStorageAlgorithms
77 template <typename ForwardIterator
>
78 inline void construct (ForwardIterator first
, ForwardIterator last
)
80 typedef typename iterator_traits
<ForwardIterator
>::value_type value_type
;
81 if (!numeric_limits
<value_type
>::is_integral
) {
82 while (first
< last
) {
89 /// Calls the placement new on \p p.
90 /// \ingroup RawStorageAlgorithms
93 inline void construct (T
* p
, const T
& value
)
98 /// Calls the destructor of \p p without calling delete.
99 /// \ingroup RawStorageAlgorithms
101 template <typename T
>
102 inline void destroy (T
* p
) throw()
107 // Helper templates to not instantiate anything for integral types.
108 template <typename T
>
109 void dtors (T first
, T last
) throw()
110 { for (; first
< last
; ++ first
) destroy (&*first
); }
111 template <typename T
, bool bIntegral
>
113 inline void operator()(T first
, T last
) throw() { dtors (first
, last
); }
115 template <typename T
>
116 struct Sdtorsr
<T
,true> {
117 inline void operator()(T
, T
) throw() {}
120 /// Calls the destructor on elements in range [first, last) without calling delete.
121 /// \ingroup RawStorageAlgorithms
123 template <typename ForwardIterator
>
124 inline void destroy (ForwardIterator first
, ForwardIterator last
) throw()
126 typedef typename iterator_traits
<ForwardIterator
>::value_type value_type
;
127 Sdtorsr
<ForwardIterator
,numeric_limits
<value_type
>::is_integral
>()(first
, last
);
130 /// Casts \p p to the type of the second pointer argument.
131 template <typename T
> inline T
* cast_to_type (void* p
, const T
*) { return ((T
*) p
); }
133 /// \brief Creates a temporary buffer pair from \p p and \p n
134 /// This is intended to be used with alloca to create temporary buffers.
135 /// The size in the returned pair is set to 0 if the allocation is unsuccessful.
136 /// \ingroup RawStorageAlgorithms
138 template <typename T
>
139 inline pair
<T
*, ptrdiff_t> make_temporary_buffer (void* p
, size_t n
, const T
* ptype
)
141 return (make_pair (cast_to_type(p
,ptype
), ptrdiff_t(p
? n
: 0)));
145 /// \brief Allocates a temporary buffer, if possible.
146 /// \ingroup RawStorageAlgorithms
147 #define get_temporary_buffer(size, ptype) make_temporary_buffer (alloca(size_of_elements(size, ptype)), size, ptype)
148 #define return_temporary_buffer(p)
150 #define get_temporary_buffer(size, ptype) make_temporary_buffer (malloc(size_of_elements(size, ptype)), size, ptype)
151 #define return_temporary_buffer(p) if (p) free (p), p = NULL
154 /// Copies [first, last) into result by calling copy constructors in result.
155 /// \ingroup RawStorageAlgorithms
157 template <typename InputIterator
, typename ForwardIterator
>
158 ForwardIterator
uninitialized_copy (InputIterator first
, InputIterator last
, ForwardIterator result
)
160 while (first
< last
) {
161 construct (&*result
, *first
);
168 /// Copies [first, first + n) into result by calling copy constructors in result.
169 /// \ingroup RawStorageAlgorithms
171 template <typename InputIterator
, typename ForwardIterator
>
172 ForwardIterator
uninitialized_copy_n (InputIterator first
, size_t n
, ForwardIterator result
)
175 construct (&*result
, *first
);
182 /// Calls construct on all elements in [first, last) with value \p v.
183 /// \ingroup RawStorageAlgorithms
185 template <typename ForwardIterator
, typename T
>
186 void uninitialized_fill (ForwardIterator first
, ForwardIterator last
, const T
& v
)
188 while (first
< last
) {
189 construct (&*first
, v
);
194 /// Calls construct on all elements in [first, first + n) with value \p v.
195 /// \ingroup RawStorageAlgorithms
197 template <typename ForwardIterator
, typename T
>
198 ForwardIterator
uninitialized_fill_n (ForwardIterator first
, size_t n
, const T
& v
)
201 construct (&*first
, v
);