Library of Assembled Shared Sources
static_vector.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2022 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43/** @class lass::stde::static_vector
44 * @brief it looks like a vector, it smells like a vector, but it only uses a fixed amout of memory
45 * @author Bram de Greve [Bramz]
46 */
47
48#ifndef LASS_GUARDIAN_OF_INCLUSION_STDE_STATIC_VECTOR_H
49#define LASS_GUARDIAN_OF_INCLUSION_STDE_STATIC_VECTOR_H
50
51#include "stde_common.h"
52#include "extended_io.h"
53#include "../meta/bool.h"
54#include "../meta/wrap.h"
55
56#include <cstddef>
57
58namespace lass
59{
60namespace stde
61{
62
63template <typename T, size_t maxsize>
64class alignas(alignof(T) > 8 ? alignof(T) : 8) static_vector
65{
66public:
67
68 typedef T& reference;
69 typedef const T& const_reference;
70 typedef T* iterator;
71 typedef const T* const_iterator;
72 typedef size_t size_type;
73 typedef std::ptrdiff_t difference_type;
74 typedef T value_type;
75 typedef T* pointer;
76 typedef const T* const_pointer;
77 typedef std::reverse_iterator<iterator> reverse_iterator;
78 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
79
80 static_vector();
81 explicit static_vector(size_type count);
82 static_vector(size_type count, const value_type& value);
83 template <typename InputIterator> static_vector(InputIterator first, InputIterator last);
84 static_vector(const static_vector<T, maxsize>& other);
85 static_vector(static_vector<T, maxsize>&& other);
86 static_vector(std::initializer_list<value_type> init);
87 ~static_vector();
88
89 static_vector<T, maxsize>& operator=(const static_vector<T, maxsize>& other);
90 static_vector<T, maxsize>& operator=(static_vector<T, maxsize>&& other);
91
92 void assign(size_type count, const value_type& value);
93 template <typename InputIterator> void assign(InputIterator first, InputIterator last);
94 void assign(std::initializer_list<value_type> init);
95
96 iterator begin() noexcept;
97 const_iterator begin() const noexcept;
98 const_iterator cbegin() const noexcept;
99 iterator end() noexcept;
100 const_iterator end() const noexcept;
101 const_iterator cend() const noexcept;
102 reverse_iterator rbegin() noexcept;
103 const_reverse_iterator rbegin() const noexcept;
104 const_reverse_iterator crbegin() const noexcept;
105 reverse_iterator rend() noexcept;
106 const_reverse_iterator rend() const noexcept;
107 const_reverse_iterator crend() const noexcept;
108
109 size_type size() const noexcept;
110 constexpr size_type max_size() const noexcept;
111 void resize(size_type n, const value_type& value = value_type());
112 constexpr size_type capacity() const noexcept;
113 bool empty() const noexcept;
114 void reserve(size_type n);
115
116 reference operator[](size_type i);
117 const_reference operator[](size_type i) const;
118 reference at(size_type i);
119 const_reference at(size_type i) const;
120 reference front();
121 const_reference front() const;
122 reference back();
123 const_reference back() const;
124
125 void push_back(const value_type& value);
126 void push_back(value_type&& value);
127 template <typename... Args> reference emplace_back(Args&&... args);
128 void pop_back();
129 iterator insert(const_iterator position, const value_type& value);
130 iterator insert(const_iterator position, value_type&& value);
131 iterator insert(const_iterator position, size_type n, const value_type& value);
132 template <typename InputIterator> iterator insert(const_iterator position, InputIterator first, InputIterator last);
133 iterator insert(const_iterator position, std::initializer_list<value_type> init);
134 template <typename... Args> iterator emplace(const_iterator pos, Args&&... args);
135 iterator erase(iterator position);
136 iterator erase(iterator first, iterator last);
137 void clear();
138 void swap(static_vector<T, maxsize>& other);
139
140private:
141
142 enum { max_size_ = maxsize };
143
144 pointer get_element(size_type i) { return reinterpret_cast<pointer>(data_) + i; }
145 const_pointer get_element(size_type i) const { return reinterpret_cast<const_pointer>(data_) + i; }
146 template <typename IntegerType> iterator insert(const_iterator position, IntegerType n, IntegerType value,
147 meta::Wrap<meta::True> parameter_is_integral);
148 template <typename InputIterator> iterator insert(const_iterator position, InputIterator first,
149 InputIterator last, meta::Wrap<meta::False> parameter_is_iterator);
150 void assign(size_type count, value_type value, meta::Wrap<meta::True> /*parameter_is_integral*/);
151 template <typename InputIterator> void assign(InputIterator first, InputIterator last,
152 meta::Wrap<meta::False> /*parameter_is_iterator*/);
153 void move_to_back(iterator first, iterator last, size_type step);
154 void move_to_front(iterator first, iterator last, size_type step);
155 void enforce_valid_size(size_type new_size) const;
156
157 char data_[max_size_ * sizeof(T)];
158 size_type size_;
159};
160
161template <typename T, size_t max_size>
162bool operator==(const static_vector<T, max_size>& x, const static_vector<T, max_size>& y);
163template <typename T, size_t max_size>
164bool operator!=(const static_vector<T, max_size>& x, const static_vector<T, max_size>& y);
165template <typename T, size_t max_size>
166bool operator<(const static_vector<T, max_size>& x, const static_vector<T, max_size>& y);
167template <typename T, size_t max_size>
168bool operator>(const static_vector<T, max_size>& x, const static_vector<T, max_size>& y);
169template <typename T, size_t max_size>
170bool operator<=(const static_vector<T, max_size>& x, const static_vector<T, max_size>& y);
171template <typename T, size_t max_size>
172bool operator>=(const static_vector<T, max_size>& x, const static_vector<T, max_size>& y);
173
174template <typename T, size_t max_size, typename Char, typename Traits>
175std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& o_stream,
176 const static_vector<T, max_size>& container);
177
178template <typename T, size_t max_size, typename Char, typename Traits>
179std::basic_istream<Char, Traits>& operator>>(std::basic_istream<Char, Traits>& i_stream,
180 static_vector<T, max_size>& container);
181
182}
183
184}
185
186#include "static_vector.inl"
187
188#ifdef LASS_GUARDIAN_OF_INCLUSION_UTIL_PYSEQUENCE_H
189# include "../python/pysequence.h"
190#endif
191
192#endif
193
194// EOF
it looks like a vector, it smells like a vector, but it only uses a fixed amout of memory
lass extensions to the standard library
Library for Assembled Shared Sources.
Definition config.h:53