Build system improvements
[ustl.git] / umemory.h
blob880d7a347a2c9078989d5e93000443b1f44f749a
1 // This file is part of the ustl library, an STL implementation.
2 //
3 // Copyright (C) 2005 by Mike Sharov <msharov@users.sourceforge.net>
4 // This file is free software, distributed under the MIT License.
5 //
6 // umemory.h
7 //
9 #ifndef UMEMORY_H_4AB5B0DB5BF09140541409CC47BCD17A
10 #define UMEMORY_H_4AB5B0DB5BF09140541409CC47BCD17A
12 #include "unew.h"
13 #ifdef HAVE_ALLOCA_H
14 #include <alloca.h>
15 #else
16 #include <stdlib.h>
17 #endif
18 #include "upair.h"
19 #include "uiterator.h"
20 #include "ulimits.h"
22 namespace ustl {
24 /// \class auto_ptr umemory.h ustl.h
25 /// \ingroup MemoryManagement
26 ///
27 /// \brief A smart pointer.
28 ///
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.
32 ///
33 template <typename T>
34 class auto_ptr {
35 public:
36 typedef T value_type;
37 typedef T* pointer;
38 typedef T& reference;
39 public:
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); }
61 private:
62 pointer m_p;
65 /// Calls the placement new on \p p.
66 /// \ingroup RawStorageAlgorithms
67 ///
68 template <typename T>
69 inline void construct (T* p)
71 new (p) T;
74 /// Calls the placement new on \p p.
75 /// \ingroup RawStorageAlgorithms
76 ///
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) {
83 construct (&*first);
84 ++ first;
89 /// Calls the placement new on \p p.
90 /// \ingroup RawStorageAlgorithms
91 ///
92 template <typename T>
93 inline void construct (T* p, const T& value)
95 new (p) 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()
104 p->~T();
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>
112 struct Sdtorsr {
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)));
144 #ifdef HAVE_ALLOCA_H
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)
149 #else
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
152 #endif
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);
162 ++ result;
163 ++ first;
165 return (result);
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)
174 while (n--) {
175 construct (&*result, *first);
176 ++ result;
177 ++ first;
179 return (result);
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);
190 ++ first;
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)
200 while (n--) {
201 construct (&*first, v);
202 ++ first;
204 return (first);
207 } // namespace ustl
209 #endif