source: mainline/uspace/lib/cpp/include/impl/array.hpp@ aab972f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since aab972f was aab972f, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: added mostly functioning implementation of std::array, iterator traits, base iteratr and reverse iterator

  • Property mode set to 100644
File size: 4.7 KB
RevLine 
[aab972f]1/*
2 * Copyright (c) 2017 Jaroslav Jindrak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef LIBCPP_ARRAY
30#define LIBCPP_ARRAY
31
32#include <iterator>
33
34namespace std
35{
36 /**
37 * 23.3.2, class template array:
38 */
39
40 template<class T, size_t N>
41 struct array
42 {
43 using value_type = T;
44 using reference = T&;
45 using const_reference = const T&;
46 using size_type = size_t;
47 using difference_type = ptrdiff_t;
48 using pointer = T*;
49 using const_pointer = const T*;
50 using iterator = pointer;
51 using const_iterator = const_pointer;
52 using reverse_iterator = std::reverse_iterator<iterator>;
53 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
54
55 /**
56 * Note: In the case of N == 0 the standard mandates that
57 * begin() == end() which is achieved by setting the size
58 * to 1. Return value of data() is unspecified and front()
59 * and back() cause undefined behavior.
60 */
61 value_type elems[N ? N : 1];
62
63 void fill(const T& x)
64 {
65 // TODO: implement with fill_n when possible
66 }
67
68 void swap(array& other) noexcept(noexcept(swap(declval<T&>(), declval<T&>())))
69 {
70 // TODO: implement with swap_ranges when possible
71 }
72
73 iterator begin() noexcept
74 {
75 return &elems[0];
76 }
77
78 iterator end() noexcept
79 {
80 return &elems[0] + N;
81 }
82
83 reverse_iterator rbegin() noexcept
84 {
85 return make_reverse_iterator(end());
86 }
87
88 reverse_iterator rend() noexcept
89 {
90 return make_reverse_iterator(begin());
91 }
92
93 const_iterator cbegin() const noexcept
94 {
95 return &elems[0];
96 }
97
98 const_iterator cend() const noexcept
99 {
100 return &elems[0] + N;
101 }
102
103 const_reverse_iterator crbegin() const noexcept
104 {
105 return make_reverse_iterator(end());
106 }
107
108 const_reverse_iterator crend() const noexcept
109 {
110 return make_reverse_iterator(begin());
111 }
112
113 reference operator[](size_type idx)
114 {
115 return elems[idx];
116 }
117
118 constexpr const_reference operator[](size_type idx) const
119 {
120 return elems[idx];
121 }
122
123 // TODO: ref/cref at (with bounds checking), needs exceptions
124
125 reference front()
126 {
127 return elems[0];
128 }
129
130 constexpr const_reference front() const
131 {
132 return elems[0];
133 }
134
135 reference back()
136 {
137 return elems[N - 1];
138 }
139
140 constexpr const_reference back() const
141 {
142 return elems[N - 1];
143 }
144
145 pointer data() noexcept
146 {
147 return &elems[0];
148 }
149
150 const_pointer data() const noexcept
151 {
152 return &elems[0];
153 }
154
155 size_type size() const noexcept
156 {
157 return N;
158 }
159 };
160
161 template<class T, size_t N>
162 void swap(array<T, N>& lhs, array<T, N>& rhs) noexcept(noexcept(lhs.swap(rhs)))
163 {
164 lhs.swap(rhs);
165 }
166
167 // TODO: tuple interface for array 23.3.2.9
168}
169
170#endif
Note: See TracBrowser for help on using the repository browser.