• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //
8 
9 #ifndef UMEMORY_H_4AB5B0DB5BF09140541409CC47BCD17A
10 #define UMEMORY_H_4AB5B0DB5BF09140541409CC47BCD17A
11 
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"
21 
22 namespace ustl {
23 
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.
m_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.
auto_ptr(auto_ptr<T> & p)43     inline		auto_ptr (auto_ptr<T>& p)	: m_p (p.release()) {}
44     /// Deletes the owned pointer.
~auto_ptr(void)45     inline	       ~auto_ptr (void)			{ delete m_p; }
46     /// Returns the pointer without relinquishing ownership.
get(void)47     inline pointer	get (void) const		{ return (m_p); }
48     /// Returns the pointer and gives up ownership.
release(void)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.
reset(pointer 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;
63 };
64 
65 /// Calls the placement new on \p p.
66 /// \ingroup RawStorageAlgorithms
67 ///
68 template <typename T>
construct(T * p)69 inline void construct (T* p)
70 {
71     new (p) T;
72 }
73 
74 /// Calls the placement new on \p p.
75 /// \ingroup RawStorageAlgorithms
76 ///
77 template <typename ForwardIterator>
construct(ForwardIterator first,ForwardIterator last)78 inline void construct (ForwardIterator first, ForwardIterator last)
79 {
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;
85 	}
86     }
87 }
88 
89 /// Calls the placement new on \p p.
90 /// \ingroup RawStorageAlgorithms
91 ///
92 template <typename T>
construct(T * p,const T & value)93 inline void construct (T* p, const T& value)
94 {
95     new (p) T (value);
96 }
97 
98 /// Calls the destructor of \p p without calling delete.
99 /// \ingroup RawStorageAlgorithms
100 ///
101 template <typename T>
destroy(T * p)102 inline void destroy (T* p) throw()
103 {
104     p->~T();
105 }
106 
107 /// Calls the destructor on elements in range [first, last) without calling delete.
108 /// \ingroup RawStorageAlgorithms
109 ///
110 template <typename ForwardIterator>
destroy(ForwardIterator first,ForwardIterator last)111 inline void destroy (ForwardIterator first, ForwardIterator last) throw()
112 {
113     typedef typename iterator_traits<ForwardIterator>::value_type value_type;
114     if (!numeric_limits<value_type>::is_integral)
115 	for (; first < last; ++ first)
116 	    destroy (&*first);
117 }
118 
119 /// Casts \p p to the type of the second pointer argument.
cast_to_type(void * p,const T *)120 template <typename T> inline T* cast_to_type (void* p, const T*) { return ((T*) p); }
121 
122 /// \brief Creates a temporary buffer pair from \p p and \p n
123 /// This is intended to be used with alloca to create temporary buffers.
124 /// The size in the returned pair is set to 0 if the allocation is unsuccessful.
125 /// \ingroup RawStorageAlgorithms
126 ///
127 template <typename T>
make_temporary_buffer(void * p,size_t n,const T * ptype)128 inline pair<T*, ptrdiff_t> make_temporary_buffer (void* p, size_t n, const T* ptype)
129 {
130     return (make_pair (cast_to_type(p,ptype), ptrdiff_t(p ? n : 0)));
131 }
132 
133 #ifdef HAVE_ALLOCA_H
134     /// \brief Allocates a temporary buffer, if possible.
135     /// \ingroup RawStorageAlgorithms
136     #define get_temporary_buffer(size, ptype)	make_temporary_buffer (alloca(size_of_elements(size, ptype)), size, ptype)
137     #define return_temporary_buffer(p)
138 #else
139     #define get_temporary_buffer(size, ptype)	make_temporary_buffer (malloc(size_of_elements(size, ptype)), size, ptype)
140     #define return_temporary_buffer(p)		if (p) free (p), p = NULL
141 #endif
142 
143 /// Copies [first, last) into result by calling copy constructors in result.
144 /// \ingroup RawStorageAlgorithms
145 ///
146 template <typename InputIterator, typename ForwardIterator>
uninitialized_copy(InputIterator first,InputIterator last,ForwardIterator result)147 ForwardIterator uninitialized_copy (InputIterator first, InputIterator last, ForwardIterator result)
148 {
149     while (first < last) {
150 	construct (&*result, *first);
151 	++ result;
152 	++ first;
153     }
154     return (result);
155 }
156 
157 /// Copies [first, first + n) into result by calling copy constructors in result.
158 /// \ingroup RawStorageAlgorithms
159 ///
160 template <typename InputIterator, typename ForwardIterator>
uninitialized_copy_n(InputIterator first,size_t n,ForwardIterator result)161 ForwardIterator uninitialized_copy_n (InputIterator first, size_t n, ForwardIterator result)
162 {
163     while (n--) {
164 	construct (&*result, *first);
165 	++ result;
166 	++ first;
167     }
168     return (result);
169 }
170 
171 /// Calls construct on all elements in [first, last) with value \p v.
172 /// \ingroup RawStorageAlgorithms
173 ///
174 template <typename ForwardIterator, typename T>
uninitialized_fill(ForwardIterator first,ForwardIterator last,const T & v)175 void uninitialized_fill (ForwardIterator first, ForwardIterator last, const T& v)
176 {
177     while (first < last) {
178 	construct (&*first, v);
179 	++ first;
180     }
181 }
182 
183 /// Calls construct on all elements in [first, first + n) with value \p v.
184 /// \ingroup RawStorageAlgorithms
185 ///
186 template <typename ForwardIterator, typename T>
uninitialized_fill_n(ForwardIterator first,size_t n,const T & v)187 ForwardIterator uninitialized_fill_n (ForwardIterator first, size_t n, const T& v)
188 {
189     while (n--) {
190 	construct (&*first, v);
191 	++ first;
192     }
193     return (first);
194 }
195 
196 } // namespace ustl
197 
198 #endif
199 
200