1 |
786 |
skrzyp |
// This file is part of the uSTL library, an STL implementation.
|
2 |
|
|
//
|
3 |
|
|
// Copyright (c) 2005-2009 by Mike Sharov <msharov@users.sourceforge.net>
|
4 |
|
|
// This file is free software, distributed under the MIT License.
|
5 |
|
|
|
6 |
|
|
#ifndef UMEMORY_H_4AB5B0DB5BF09140541409CC47BCD17A
|
7 |
|
|
#define UMEMORY_H_4AB5B0DB5BF09140541409CC47BCD17A
|
8 |
|
|
|
9 |
|
|
#include "unew.h"
|
10 |
|
|
#if HAVE_ALLOCA_H
|
11 |
|
|
#include <alloca.h>
|
12 |
|
|
#else
|
13 |
|
|
#include <stdlib.h>
|
14 |
|
|
#endif
|
15 |
|
|
#include "upair.h"
|
16 |
|
|
#include "uiterator.h"
|
17 |
|
|
#include "ulimits.h"
|
18 |
|
|
|
19 |
|
|
namespace ustl {
|
20 |
|
|
|
21 |
|
|
/// \class auto_ptr umemory.h ustl.h
|
22 |
|
|
/// \ingroup MemoryManagement
|
23 |
|
|
///
|
24 |
|
|
/// \brief A smart pointer.
|
25 |
|
|
///
|
26 |
|
|
/// Calls delete in the destructor; assignment transfers ownership.
|
27 |
|
|
/// This class does not work with void pointers due to the absence
|
28 |
|
|
/// of the required dereference operator.
|
29 |
|
|
///
|
30 |
|
|
template <typename T>
|
31 |
|
|
class auto_ptr {
|
32 |
|
|
public:
|
33 |
|
|
typedef T value_type;
|
34 |
|
|
typedef T* pointer;
|
35 |
|
|
typedef T& reference;
|
36 |
|
|
public:
|
37 |
|
|
/// Takes ownership of \p p.
|
38 |
|
|
inline explicit auto_ptr (pointer p = NULL) : m_p (p) {}
|
39 |
|
|
/// Takes ownership of pointer in \p p. \p p relinquishes ownership.
|
40 |
|
|
inline auto_ptr (auto_ptr<T>& p) : m_p (p.release()) {}
|
41 |
|
|
/// Deletes the owned pointer.
|
42 |
|
|
inline ~auto_ptr (void) { delete m_p; }
|
43 |
|
|
/// Returns the pointer without relinquishing ownership.
|
44 |
|
|
inline pointer get (void) const { return (m_p); }
|
45 |
|
|
/// Returns the pointer and gives up ownership.
|
46 |
|
|
inline pointer release (void) { pointer rv (m_p); m_p = NULL; return (rv); }
|
47 |
|
|
/// Deletes the pointer and sets it equal to \p p.
|
48 |
|
|
inline void reset (pointer p) { if (p != m_p) { delete m_p; m_p = p; } }
|
49 |
|
|
/// Takes ownership of \p p.
|
50 |
|
|
inline auto_ptr<T>& operator= (pointer p) { reset (p); return (*this); }
|
51 |
|
|
/// Takes ownership of pointer in \p p. \p p relinquishes ownership.
|
52 |
|
|
inline auto_ptr<T>& operator= (auto_ptr<T>& p) { reset (p.release()); return (*this); }
|
53 |
|
|
inline reference operator* (void) const { return (*m_p); }
|
54 |
|
|
inline pointer operator-> (void) const { return (m_p); }
|
55 |
|
|
inline bool operator== (const pointer p) const { return (m_p == p); }
|
56 |
|
|
inline bool operator== (const auto_ptr<T>& p) const { return (m_p == p.m_p); }
|
57 |
|
|
inline bool operator< (const auto_ptr<T>& p) const { return (p.m_p < m_p); }
|
58 |
|
|
private:
|
59 |
|
|
pointer m_p;
|
60 |
|
|
};
|
61 |
|
|
|
62 |
|
|
/// Calls the placement new on \p p.
|
63 |
|
|
/// \ingroup RawStorageAlgorithms
|
64 |
|
|
///
|
65 |
|
|
template <typename T>
|
66 |
|
|
inline void construct (T* p)
|
67 |
|
|
{
|
68 |
|
|
new (p) T;
|
69 |
|
|
}
|
70 |
|
|
|
71 |
|
|
/// Calls the placement new on \p p.
|
72 |
|
|
/// \ingroup RawStorageAlgorithms
|
73 |
|
|
///
|
74 |
|
|
template <typename ForwardIterator>
|
75 |
|
|
inline void construct (ForwardIterator first, ForwardIterator last)
|
76 |
|
|
{
|
77 |
|
|
typedef typename iterator_traits<ForwardIterator>::value_type value_type;
|
78 |
|
|
if (numeric_limits<value_type>::is_integral)
|
79 |
|
|
memset (first, 0, distance(first,last)*sizeof(value_type));
|
80 |
|
|
else
|
81 |
|
|
for (--last; intptr_t(first) <= intptr_t(last); ++first)
|
82 |
|
|
construct (&*first);
|
83 |
|
|
}
|
84 |
|
|
|
85 |
|
|
/// Calls the placement new on \p p.
|
86 |
|
|
/// \ingroup RawStorageAlgorithms
|
87 |
|
|
///
|
88 |
|
|
template <typename T>
|
89 |
|
|
inline void construct (T* p, const T& value)
|
90 |
|
|
{
|
91 |
|
|
new (p) T (value);
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
/// Calls the destructor of \p p without calling delete.
|
95 |
|
|
/// \ingroup RawStorageAlgorithms
|
96 |
|
|
///
|
97 |
|
|
template <typename T>
|
98 |
|
|
inline void destroy (T* p) throw()
|
99 |
|
|
{
|
100 |
|
|
p->~T();
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
// Helper templates to not instantiate anything for integral types.
|
104 |
|
|
template <typename T>
|
105 |
|
|
void dtors (T first, T last) throw()
|
106 |
|
|
{ for (--last; intptr_t(first) <= intptr_t(last); ++first) destroy (&*first); }
|
107 |
|
|
template <typename T, bool bIntegral>
|
108 |
|
|
struct Sdtorsr {
|
109 |
|
|
inline void operator()(T first, T last) throw() { dtors (first, last); }
|
110 |
|
|
};
|
111 |
|
|
template <typename T>
|
112 |
|
|
struct Sdtorsr<T,true> {
|
113 |
|
|
inline void operator()(T, T) throw() {}
|
114 |
|
|
};
|
115 |
|
|
|
116 |
|
|
/// Calls the destructor on elements in range [first, last) without calling delete.
|
117 |
|
|
/// \ingroup RawStorageAlgorithms
|
118 |
|
|
///
|
119 |
|
|
template <typename ForwardIterator>
|
120 |
|
|
inline void destroy (ForwardIterator first, ForwardIterator last) throw()
|
121 |
|
|
{
|
122 |
|
|
typedef typename iterator_traits<ForwardIterator>::value_type value_type;
|
123 |
|
|
Sdtorsr<ForwardIterator,numeric_limits<value_type>::is_integral>()(first, last);
|
124 |
|
|
}
|
125 |
|
|
|
126 |
|
|
/// Casts \p p to the type of the second pointer argument.
|
127 |
|
|
template <typename T> inline T* cast_to_type (void* p, const T*) { return ((T*) p); }
|
128 |
|
|
|
129 |
|
|
/// \brief Creates a temporary buffer pair from \p p and \p n
|
130 |
|
|
/// This is intended to be used with alloca to create temporary buffers.
|
131 |
|
|
/// The size in the returned pair is set to 0 if the allocation is unsuccessful.
|
132 |
|
|
/// \ingroup RawStorageAlgorithms
|
133 |
|
|
///
|
134 |
|
|
template <typename T>
|
135 |
|
|
inline pair<T*, ptrdiff_t> make_temporary_buffer (void* p, size_t n, const T* ptype)
|
136 |
|
|
{
|
137 |
|
|
return (make_pair (cast_to_type(p,ptype), ptrdiff_t(p ? n : 0)));
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
#if HAVE_ALLOCA_H
|
141 |
|
|
/// \brief Allocates a temporary buffer, if possible.
|
142 |
|
|
/// \ingroup RawStorageAlgorithms
|
143 |
|
|
#define get_temporary_buffer(size, ptype) make_temporary_buffer (alloca(size_of_elements(size, ptype)), size, ptype)
|
144 |
|
|
#define return_temporary_buffer(p)
|
145 |
|
|
#else
|
146 |
|
|
#define get_temporary_buffer(size, ptype) make_temporary_buffer (malloc(size_of_elements(size, ptype)), size, ptype)
|
147 |
|
|
#define return_temporary_buffer(p) if (p) free (p), p = NULL
|
148 |
|
|
#endif
|
149 |
|
|
|
150 |
|
|
/// Copies [first, last) into result by calling copy constructors in result.
|
151 |
|
|
/// \ingroup RawStorageAlgorithms
|
152 |
|
|
///
|
153 |
|
|
template <typename InputIterator, typename ForwardIterator>
|
154 |
|
|
ForwardIterator uninitialized_copy (InputIterator first, InputIterator last, ForwardIterator result)
|
155 |
|
|
{
|
156 |
|
|
for (; first < last; ++result, ++first)
|
157 |
|
|
construct (&*result, *first);
|
158 |
|
|
return (result);
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
/// Copies [first, first + n) into result by calling copy constructors in result.
|
162 |
|
|
/// \ingroup RawStorageAlgorithms
|
163 |
|
|
///
|
164 |
|
|
template <typename InputIterator, typename ForwardIterator>
|
165 |
|
|
ForwardIterator uninitialized_copy_n (InputIterator first, size_t n, ForwardIterator result)
|
166 |
|
|
{
|
167 |
|
|
for (++n; --n; ++result, ++first)
|
168 |
|
|
construct (&*result, *first);
|
169 |
|
|
return (result);
|
170 |
|
|
}
|
171 |
|
|
|
172 |
|
|
/// Calls construct on all elements in [first, last) with value \p v.
|
173 |
|
|
/// \ingroup RawStorageAlgorithms
|
174 |
|
|
///
|
175 |
|
|
template <typename ForwardIterator, typename T>
|
176 |
|
|
void uninitialized_fill (ForwardIterator first, ForwardIterator last, const T& v)
|
177 |
|
|
{
|
178 |
|
|
for (; first < last; ++first)
|
179 |
|
|
construct (&*first, v);
|
180 |
|
|
}
|
181 |
|
|
|
182 |
|
|
/// Calls construct on all elements in [first, first + n) with value \p v.
|
183 |
|
|
/// \ingroup RawStorageAlgorithms
|
184 |
|
|
///
|
185 |
|
|
template <typename ForwardIterator, typename T>
|
186 |
|
|
ForwardIterator uninitialized_fill_n (ForwardIterator first, size_t n, const T& v)
|
187 |
|
|
{
|
188 |
|
|
for (++n; --n; ++first)
|
189 |
|
|
construct (&*first, v);
|
190 |
|
|
return (first);
|
191 |
|
|
}
|
192 |
|
|
|
193 |
|
|
} // namespace ustl
|
194 |
|
|
|
195 |
|
|
#endif
|