remove \r
[extl.git] / extl / memory / stack_buffer.h
blob67884f2da7ba07e03eab2ba8cc1e26f2f4743289
1 /* ///////////////////////////////////////////////////////////////////////
2 * File: stack_buffer.h
4 * Created: 08.06.06
5 * Updated: 08.06.06
7 * Brief: stack_buffer class
9 * [<Home>]
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
13 #ifndef EXTL_MEMORY_STACK_BUFFER_H
14 #define EXTL_MEMORY_STACK_BUFFER_H
16 /*!\file stack_buffer.h
17 * \brief stack_buffer class
20 #ifndef __cplusplus
21 # error stack_buffer.h need be supported by c++.
22 #endif
24 /* ///////////////////////////////////////////////////////////////////////
25 * Includes
27 #include "buffer_base.h"
28 #include "memory_traits_selector.h"
29 #include "allocator_selector.h"
30 #include "../error/error.h"
32 /// The maximum number of the stack storage space
33 #define EXTL_MEMORY_STACK_BUFFER_MAX_SPACE 256
35 /* ///////////////////////////////////////////////////////////////////////
36 * ::extl namespace
38 EXTL_BEGIN_NAMESPACE
40 /*!\brief: stack_buffer class
42 * \param T The element type
43 * \param MAX_SPACE The static storage space
44 * \param M The memory traits type
46 * \ingroup extl_group_memory
48 template< typename_param_k T
49 #ifdef EXTL_TEMPLATE_CLASS_DEFAULT_ARGUMENT_SUPPORT
50 , e_size_t MAX_SPACE = EXTL_MEMORY_STACK_BUFFER_MAX_SPACE
51 , typename_param_k M = typename_type_def_k memory_traits_selector<T>::memory_traits_type
52 #else
53 , e_size_t MAX_SPACE
54 , typename_param_k M
55 #endif
57 class stack_buffer
58 : public buffer_base< stack_buffer< T, MAX_SPACE, M >
59 , typename_type_k allocator_selector<T>::allocator_type
60 , M
63 /// \name Types
64 /// @{
65 public:
66 typedef stack_buffer class_type;
67 typedef buffer_base< class_type
68 , typename_type_k allocator_selector<T>::allocator_type
69 , M
70 > base_type;
71 typedef typename_type_k base_type::value_type value_type;
72 typedef typename_type_k base_type::memory_traits_type memory_traits_type;
73 typedef typename_type_k base_type::allocator_type allocator_type;
74 typedef typename_type_k base_type::pointer pointer;
75 typedef typename_type_k base_type::const_pointer const_pointer;
76 typedef typename_type_k base_type::reference reference;
77 typedef typename_type_k base_type::const_reference const_reference;
78 typedef typename_type_k base_type::size_type size_type;
79 typedef typename_type_k base_type::difference_type difference_type;
80 typedef typename_type_k base_type::iterator iterator;
81 typedef typename_type_k base_type::const_iterator const_iterator;
82 typedef typename_type_k base_type::reverse_iterator reverse_iterator;
83 typedef typename_type_k base_type::const_reverse_iterator const_reverse_iterator;
84 typedef typename_type_k base_type::bool_type bool_type;
85 typedef typename_type_k base_type::index_type index_type;
86 typedef typename_type_k memory_traits_type::initialiser_type initialiser_type;
87 /// @}
90 /// \name Members
91 /// @{
92 private:
93 /// The data type
94 struct data_type
96 value_type data[MAX_SPACE]; //!< The static memory storage
98 data_type m_data; //!< The static memory storage
99 size_type m_size; //!< The size of the used storage
100 /// @}
102 private:
103 friend class buffer_base< class_type
104 , typename_type_k allocator_selector<T>::allocator_type
108 /// \name Constructors
109 /// @{
110 public:
111 /// Constructs a stack_buffer with the given number of elements
112 explicit_k stack_buffer(size_type const& n = 0)
113 : base_type(), m_size(0)
115 resize(n);
118 /// Constructs a stack_buffer with n values from the given buffer at the specified position
119 stack_buffer(const_pointer buf, size_type n, size_type pos = 0)
120 : base_type(), m_size(0)
122 this->clear();
123 base_type::assign(buf, n, pos);
126 /// Constructs a stack_buffer from the given buffer
127 stack_buffer(class_type const& buf)
128 : base_type(), m_size(0)
130 this->clear();
131 base_type::assign(buf, buf.size());
134 /// Constructs a stack_buffer with n values from the given buffer at the specified position
135 stack_buffer(class_type const& buf, size_type n, size_type pos = 0)
136 : base_type(), m_size(0)
138 this->clear();
139 base_type::assign(buf, n, pos);
142 /// Constructs with n values
143 explicit_k stack_buffer(const_reference v, size_type n)
144 : base_type(), m_size(0)
146 this->clear();
147 base_type::assign(v, n);
150 /// Constructs from range [first, last) with the const pointer
151 stack_buffer(const_pointer first, const_pointer last)
152 : base_type(), m_size(0)
154 this->clear();
155 base_type::assign(first, last);
157 /// Constructs from range [first, last) with the const iterator
158 stack_buffer(const_iterator first, const_iterator last)
159 : base_type(), m_size(0)
161 this->clear();
162 base_type::assign(first, last);
164 /// Constructs from range [first, last) with the const reverse iterator
165 stack_buffer(const_reverse_iterator first, const_reverse_iterator last)
166 : base_type(), m_size(0)
168 this->clear();
169 base_type::assign(first, last);
171 #if defined(EXTL_MEMBER_TEMPLATE_CTOR_OVERLOAD_DISCRIMINATED_SUPPORT) \
172 && !defined(EXTL_COMPILER_IS_DMC)
173 /// Constructs from range [first, last) with the input iterator
174 template < typename_param_k _InIt >
175 stack_buffer(_InIt first, _InIt last)
176 : base_type(), m_size(0)
178 this->clear();
179 base_type::assign(first, last);
181 #endif
183 /// Destructor
184 ~stack_buffer() EXTL_THROW_0()
186 EXTL_STATIC_ASSERT(0 != MAX_SPACE);
188 /// @}
190 /// \name Accessors
191 /// @{
192 public:
193 /// Returns the pointer to the buffer
194 pointer data()
196 if(0 == m_size) return NULL;
197 return m_data.data;
199 /// Returns the const pointer to the buffer
200 const_pointer data() const
202 if(0 == m_size) return NULL;
203 return m_data.data;
205 /// Returns the size of the buffer
206 size_type size() const { return m_size; }
207 /// Returns the capacity size of the buffer
208 size_type capacity() const { return MAX_SPACE; }
209 /// @}
211 /// \name Operators
212 /// @{
213 public:
214 class_type& operator=(class_type const& buf) { return base_type::operator=(buf); }
215 class_type& operator=(const_reference v) { return base_type::operator=(v); }
216 /// @}
218 /// \name Mutators
219 /// @{
220 public:
221 /*!\brief Resizes the size of buffer
223 * \note Only resize the capacity of the storage when is_update_size is e_false_v
225 void resize(size_type size, bool_type is_update_size = e_true_v)
227 EXTL_ASSERT(is_valid());
228 EXTL_MESSAGE_ASSERT(size <= MAX_SPACE, "the buffer is too long");
229 EXTL_ASSERT_THROW(size <= MAX_SPACE, std_length_error("the buffer is too long"));
230 if (is_update_size) m_size = size;
231 EXTL_ASSERT(is_valid());
234 /// Swaps storage
235 void swap(class_type& rhs) EXTL_THROW_0()
237 EXTL_ASSERT(is_valid());
239 if (this == &rhs) return ;
241 std_swap(m_data, rhs.m_data);
242 std_swap(m_size, rhs.m_size);
244 EXTL_ASSERT(is_valid());
246 /// @}
248 /// \name Others
249 /// @{
250 private:
251 /// Invariance
252 bool_type is_valid() const
254 if (capacity() < size())
255 return e_false_v;
257 if ((0 < size()) && (NULL == data()))
259 return e_false_v;
262 return e_true_v;
264 /// @}
267 /* ///////////////////////////////////////////////////////////////////////
268 * ::extl namespace
270 EXTL_END_NAMESPACE
272 /* //////////////////////////////////////////////////////////////////// */
273 #endif /* EXTL_MEMORY_STACK_BUFFER_H */
274 /* //////////////////////////////////////////////////////////////////// */