Embedded Template Library 1.0
Loading...
Searching...
No Matches
initializer_list.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Documentation: https://www.etlcpp.com/initializer_list.html
11
12Copyright(c) 2022 John Wellbelove
13
14Permission is hereby granted, free of charge, to any person obtaining a copy
15of this software and associated documentation files(the "Software"), to deal
16in the Software without restriction, including without limitation the rights
17to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
18copies of the Software, and to permit persons to whom the Software is
19furnished to do so, subject to the following conditions :
20
21The above copyright notice and this permission notice shall be included in all
22copies or substantial portions of the Software.
23
24THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
27AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30SOFTWARE.
31******************************************************************************/
32
33#ifndef ETL_INITIALIZER_LIST_INCLUDED
34#define ETL_INITIALIZER_LIST_INCLUDED
35
36#include "platform.h"
37
38#if ETL_HAS_INITIALIZER_LIST
39
40#if (ETL_USING_CPP11 && !defined(ETL_NO_INITIALIZER_LIST))
41
42#include <stddef.h>
43
44// Use the compiler's std::initializer_list?
45#if (ETL_USING_STL && ETL_NOT_USING_STLPORT && !defined(ETL_FORCE_ETL_INITIALIZER_LIST)) || defined(ETL_IN_UNIT_TEST) || defined(ETL_FORCE_STD_INITIALIZER_LIST)
46
47 #include <initializer_list>
48
49#else
50
51// Use the ETL's std::initializer_list
52namespace std
53{
54#if defined(ETL_COMPILER_MICROSOFT)
55
59 template <typename T>
60 class initializer_list
61 {
62 public:
63
64 using value_type = T;
65 using reference = const T&;
66 using const_reference = const T&;
67 using size_type = size_t;
68 using iterator = const T*;
69 using const_iterator = const T*;
70
71 //*************************************************************************
73 //*************************************************************************
74 constexpr initializer_list() noexcept
75 : pfirst(nullptr), plast(nullptr)
76 {
77 }
78
79 //*************************************************************************
81 //*************************************************************************
82 constexpr initializer_list(const T* pfirst_, const T* plast_) noexcept
83 : pfirst(pfirst_), plast(plast_)
84 {
85 }
86
87 //*************************************************************************
89 //*************************************************************************
90 constexpr const T* begin() const noexcept
91 {
92 return pfirst;
93 }
94
95 //*************************************************************************
97 //*************************************************************************
98 constexpr const T* end() const noexcept
99 {
100 return plast;
101 }
102
103 //*************************************************************************
105 //*************************************************************************
106 constexpr size_t size() const noexcept
107 {
108 return static_cast<size_t>(plast - pfirst);
109 }
110
111 private:
112
113 const T* pfirst;
114 const T* plast;
115 };
116
117 //*************************************************************************
119 //*************************************************************************
120 template<typename T>
121 constexpr const T* begin(initializer_list<T> init) noexcept
122 {
123 return init.begin();
124 }
125
126 //*************************************************************************
128 //*************************************************************************
129 template <typename T>
130 constexpr const T* end(initializer_list<T> init) noexcept
131 {
132 return init.end();
133 }
134
135#elif defined(ETL_COMPILER_GCC) || defined(ETL_COMPILER_CLANG) || defined(ETL_COMPILER_ARM6) || \
136 defined(ETL_COMPILER_ARM7) || defined(ETL_COMPILER_IAR) || defined(ETL_COMPILER_TEXAS_INSTRUMENTS) || \
137 defined(ETL_COMPILER_INTEL)
138
142 template<class T>
143 class initializer_list
144 {
145 public:
146
147 using value_type = T;
148 using reference = const T&;
149 using const_reference = const T&;
150 using size_type = size_t;
151 using iterator = const T*;
152 using const_iterator = const T*;
153
154 //*************************************************************************
156 //*************************************************************************
157 constexpr initializer_list() noexcept
158 : pfirst(nullptr), length(0)
159 {
160 }
161
162 //*************************************************************************
164 //*************************************************************************
165 constexpr const T* begin() const noexcept
166 {
167 return pfirst;
168 }
169
170 //*************************************************************************
172 //*************************************************************************
173 constexpr const T* end() const noexcept
174 {
175 return pfirst + length;
176 }
177
178 //*************************************************************************
180 //*************************************************************************
181 constexpr size_t size() const noexcept
182 {
183 return length;
184 }
185
186 private:
187
188 //*************************************************************************
190 //*************************************************************************
191 constexpr initializer_list(const T* pfirst_, size_t length_) noexcept
192 : pfirst(pfirst_)
193 , length(length_)
194 {
195 }
196
197 const T* pfirst;
198 size_t length;
199 };
200
201 //*************************************************************************
203 //*************************************************************************
204 template<class T>
205 constexpr const T* begin(initializer_list<T> init) noexcept
206 {
207 return init.begin();
208 }
209
210 //*************************************************************************
212 //*************************************************************************
213 template<class T>
214 constexpr const T* end(initializer_list<T> init) noexcept
215 {
216 return init.end();
217 }
218#else
219
220 #error No definition for initializer_list is currently available for your compiler. Visit https://github.com/ETLCPP/etl/issues to request support.
221
222#endif // Compiler tests
223}
224
225#endif // (ETL_USING_STL && ETL_NOT_USING_STLPORT && !defined(ETL_FORCE_ETL_INITIALIZER_LIST)) || defined(ETL_IN_UNIT_TEST) || defined(ETL_FORCE_STD_INITIALIZER_LIST)
226#endif // ETL_USING_CPP11 && !defined(ETL_NO_INITIALIZER_LIST)
227#endif // ETL_HAS_INITIALIZER_LIST
228#endif // ETL_INITIALIZER_LIST_INCLUDED
ETL_CONSTEXPR TContainer::size_type size(const TContainer &container)
Definition iterator.h:1187
ETL_CONSTEXPR TContainer::iterator begin(TContainer &container)
Definition iterator.h:962
ETL_CONSTEXPR TContainer::iterator end(TContainer &container)
Definition iterator.h:992