1 /* ///////////////////////////////////////////////////////////////////////
7 * Brief: stack_buffer class
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
21 # error stack_buffer.h need be supported by c++.
24 /* ///////////////////////////////////////////////////////////////////////
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 /* ///////////////////////////////////////////////////////////////////////
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
58 : public buffer_base
< stack_buffer
< T
, MAX_SPACE
, M
>
59 , typename_type_k allocator_selector
<T
>::allocator_type
66 typedef stack_buffer class_type
;
67 typedef buffer_base
< class_type
68 , typename_type_k allocator_selector
<T
>::allocator_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
;
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
103 friend class buffer_base
< class_type
104 , typename_type_k allocator_selector
<T
>::allocator_type
108 /// \name Constructors
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)
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)
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)
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)
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)
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)
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)
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)
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)
179 base_type::assign(first
, last
);
184 ~stack_buffer() EXTL_THROW_0()
186 EXTL_STATIC_ASSERT(0 != MAX_SPACE
);
193 /// Returns the pointer to the buffer
196 if(0 == m_size
) return NULL
;
199 /// Returns the const pointer to the buffer
200 const_pointer
data() const
202 if(0 == m_size
) return NULL
;
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
; }
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
); }
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());
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());
252 bool_type
is_valid() const
254 if (capacity() < size())
257 if ((0 < size()) && (NULL
== data()))
267 /* ///////////////////////////////////////////////////////////////////////
272 /* //////////////////////////////////////////////////////////////////// */
273 #endif /* EXTL_MEMORY_STACK_BUFFER_H */
274 /* //////////////////////////////////////////////////////////////////// */