remove \r
[extl.git] / extl / container / array_base.h
blob4f1e47cbf1eb8e3ac4cd856f2496ce1eae91c22b
1 /* ///////////////////////////////////////////////////////////////////////
2 * File: array_base.h
4 * Created: 08.08.07
5 * Updated: 08.08.07
7 * Brief: The array_base class
9 * [<Home>]
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
13 #ifndef EXTL_CONTAINER_ARRAY_BASE_H
14 #define EXTL_CONTAINER_ARRAY_BASE_H
16 /*!\file array_base.h
17 * \brief array_base class
20 /* ///////////////////////////////////////////////////////////////////////
21 * Includes
23 #include "prefix.h"
24 /* ///////////////////////////////////////////////////////////////////////
25 * ::extl namespace
27 EXTL_BEGIN_NAMESPACE
29 /*!brief array_base
31 * \param D The derived type
32 * \param B The buffer type
34 * \ingroup extl_group_container
36 template< typename_param_k D
37 , typename_param_k B
39 class array_base
41 /// \name Types
42 /// @{
43 public:
44 typedef array_base class_type;
45 typedef D derived_type;
46 typedef B buffer_type;
47 typedef typename_type_k buffer_type::allocator_type allocator_type;
48 typedef typename_type_k buffer_type::value_type value_type;
49 typedef typename_type_k buffer_type::pointer pointer;
50 typedef typename_type_k buffer_type::const_pointer const_pointer;
51 typedef typename_type_k buffer_type::reference reference;
52 typedef typename_type_k buffer_type::const_reference const_reference;
53 typedef typename_type_k buffer_type::iterator iterator;
54 typedef typename_type_k buffer_type::const_iterator const_iterator;
55 typedef typename_type_k buffer_type::reverse_iterator reverse_iterator;
56 typedef typename_type_k buffer_type::const_reverse_iterator const_reverse_iterator;
57 typedef typename_type_k buffer_type::size_type size_type;
58 typedef typename_type_k buffer_type::bool_type bool_type;
59 typedef typename_type_k buffer_type::difference_type difference_type;
60 typedef e_int_t int_type;
61 typedef size_type index_type;
62 /// @}
64 public:
65 /// The dimension of the matrix
66 EXTL_STATIC_MEMBER_CONST(size_type, dimension = 1);
68 /// \name Members
69 /// @{
70 private:
71 buffer_type m_buffer;
72 /// @}
74 private:
75 /*!\brief Prohibit the following case:
77 * \code
78 D da, db;
79 B &ba = da, &bb = db;
80 ba = bb;
81 B b(ba);
82 * \endcode
84 array_base(class_type const&);
85 class_type& operator=(class_type const&);
87 /// \name Constructors
88 /// @{
89 public:
90 array_base()
91 : m_buffer()
94 explicit_k array_base(derived_type const& rhs)
95 : m_buffer(static_cast<class_type const&>(rhs).buffer())
98 array_base(const_pointer ar, size_type n)
99 : m_buffer(ar, n)
102 array_base(const_reference value, size_type n)
103 : m_buffer(value, n)
106 /// @}
108 /// \name Attributes
109 /// @{
110 public:
111 allocator_type allocator() const { return buffer().allocator(); }
112 index_type dim0() const { return buffer().size(); }
113 size_type size() const { return buffer().size(); }
114 size_type capacity() const { return buffer().capacity(); }
115 bool_type is_empty() const { return buffer().is_empty(); }
116 bool_type empty() const { return buffer().is_empty(); }
117 size_type max_size() const { return buffer().max_size(); }
118 /// @}
120 /// \name Accessors
121 /// @{
122 public:
123 pointer data() { return buffer().data(); }
124 const_pointer data() const { return buffer().data(); }
126 reference front() { return buffer().front(); }
127 const_reference front() const { return buffer().front(); }
129 reference back() { return buffer().back(); }
130 const_reference back() const { return buffer().back(); }
132 reference operator[](index_type index) { return derive().at_unchecked(index); }
133 const_reference operator[](index_type index) const { return derive().at_unchecked(index); }
135 reference at(index_type index)
137 return const_cast<reference>(static_cast<derived_type const&>(*this).at(index));
139 const_reference at(index_type index) const
141 EXTL_ASSERT_THROW(index < derive().dim0(), index_error("out of range"));
142 return derive().at_unchecked(index);
145 reference at_unchecked(index_type index)
147 return const_cast<reference>(static_cast<derived_type const&>(*this).at(index));
149 const_reference at_unchecked(index_type index) const
151 EXTL_ASSERT(NULL != derive().data());
152 EXTL_ASSERT(index < derive().dim0());
153 return derive().data()[index];
155 /// @}
157 /// \name Mutators
158 /// @{
159 public:
160 derived_type& operator =(derived_type const& rhs)
162 if (this != static_cast<class_type const*>(&rhs))
163 buffer().assign(static_cast<class_type const&>(rhs).buffer());
164 return derive();
166 derived_type& operator =(const_reference value)
168 buffer().assign(value, buffer().size());
169 return derive();
172 void push_back(const_reference value) { buffer().push_back(value); }
173 void pop_back() { buffer().pop_back(); }
174 void swap(derived_type& rhs) { buffer().swap(static_cast<class_type&>(rhs).buffer()); }
175 void clear() { buffer().clear(); }
176 void resize(size_type n) { buffer().resize(n); }
177 /// @}
179 /// \name Iterators
180 /// @{
181 public:
182 const_iterator begin() const { return const_iterator(derive().data()); }
183 iterator begin() { return iterator(derive().data()); }
185 const_iterator end() const { return const_iterator(derive().data() + derive().size()); }
186 iterator end() { return iterator(derive().data() + derive().size()); }
188 const_reverse_iterator rbegin() const { return const_reverse_iterator(derive().end()); }
189 reverse_iterator rbegin() { return reverse_iterator(derive().end()); }
191 const_reverse_iterator rend() const { return const_reverse_iterator(derive().begin()); }
192 reverse_iterator rend() { return reverse_iterator(derive().begin()); }
193 /// @}
195 /// \name Others
196 /// @{
197 protected:
198 derived_type& derive() { return static_cast<derived_type&>(*this); }
199 derived_type const& derive() const { return static_cast<derived_type const&>(*this); }
201 buffer_type& buffer() { return m_buffer; }
202 buffer_type const& buffer() const { return m_buffer; }
203 /// @}
206 /* /////////////////////////////////////////////////////////////////////////
207 * swapping
209 template< typename_param_k D
210 , typename_param_k B
212 EXTL_INLINE void swap(array_base<D, B>& lhs, array_base<D, B>& rhs)
214 static_cast<D&>(lhs).swap(static_cast<D&>(rhs));
216 /* /////////////////////////////////////////////////////////////////////////
217 * shims
219 template< typename_param_k D
220 , typename_param_k B
222 EXTL_INLINE typename_type_ret_k array_base<D, B>::
223 const_pointer get_ptr(array_base<D, B> const& ar)
225 return static_cast<D const&>(ar).data();
228 template< typename_param_k D
229 , typename_param_k B
231 EXTL_INLINE typename_type_ret_k array_base<D, B>::
232 size_type get_size(array_base<D, B> const& ar)
234 return static_cast<D const&>(ar).size();
237 /* ///////////////////////////////////////////////////////////////////////
238 * ::extl namespace
240 EXTL_END_NAMESPACE
242 /* ///////////////////////////////////////////////////////////////////////
243 * std::swap
245 #if !defined(EXTL_NO_STL) && \
246 !defined(EXTL_NO_NAMESPACE)
247 /* ::std namespace */
248 EXTL_STD_BEGIN_NAMESPACE
250 template< typename_param_k D
251 , typename_param_k B
253 EXTL_INLINE void swap(EXTL_NS(array_base)<D, B>& lhs,
254 EXTL_NS(array_base)<D, B>& rhs)
256 static_cast<D&>(lhs).swap(static_cast<D&>(rhs));
258 /* ::std namespace */
259 EXTL_STD_END_NAMESPACE
260 #endif
262 /* //////////////////////////////////////////////////////////////////// */
263 #endif /* EXTL_CONTAINER_ARRAY_BASE_H */
264 /* //////////////////////////////////////////////////////////////////// */