VirtualBox

source: vbox/trunk/include/iprt/nocrt/vector@ 96205

最後變更 在這個檔案從96205是 95975,由 vboxsync 提交於 3 年 前

include/iprt/nocrt: Prototyped a whole bunch of things to make mesa (almost) compile. bugref:10261

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 11.1 KB
 
1/** @file
2 * IPRT / No-CRT - Minimal C++ std::vector.
3 */
4
5/*
6 * Copyright (C) 2022 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.alldomusa.eu.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef IPRT_INCLUDED_nocrt_vector
27#define IPRT_INCLUDED_nocrt_vector
28#ifndef RT_WITHOUT_PRAGMA_ONCE
29# pragma once
30#endif
31
32#include <iprt/nocrt/memory>
33
34namespace std
35{
36 template<typename a_Type, class a_Container>
37 class RTCNoCrtVectorIterator
38 {
39 public:
40 typedef a_Type &reference;
41 typedef a_Type *pointer;
42 typedef typename a_Container::difference_type difference_type;
43
44 protected:
45 a_Type *m_pItem;
46
47 public:
48 RTCNoCrtVectorIterator() RT_NOEXCEPT
49 : m_pItem(NULL)
50 { }
51
52 RTCNoCrtVectorIterator(a_Type *a_pItem) RT_NOEXCEPT
53 : m_pItem(a_pItem)
54 { }
55
56 ~RTCNoCrtVectorIterator()
57 {
58 m_pItem = NULL;
59 }
60
61 /** @name Moving the iterator.
62 * @{ */
63
64 RTCNoCrtVectorIterator &operator++() RT_NOEXCEPT
65 {
66 ++m_pItem;
67 return *this;
68 }
69
70 RTCNoCrtVectorIterator &operator--() RT_NOEXCEPT
71 {
72 --m_pItem;
73 return *this;
74 }
75
76 RTCNoCrtVectorIterator operator++(int) RT_NOEXCEPT
77 {
78 return RTCNoCrtVectorIterator(m_pItem++);
79 }
80
81 RTCNoCrtVectorIterator operator--(int) RT_NOEXCEPT
82 {
83 return RTCNoCrtVectorIterator(m_pItem--);
84 }
85
86 RTCNoCrtVectorIterator &operator+=(difference_type cItems) RT_NOEXCEPT
87 {
88 m_pItem += cItems;
89 return *this;
90 }
91
92 RTCNoCrtVectorIterator &operator-=(difference_type cItems) RT_NOEXCEPT
93 {
94 m_pItem -= cItems;
95 return *this;
96 }
97
98 RTCNoCrtVectorIterator operator+(difference_type cItems) const RT_NOEXCEPT
99 {
100 return RTCNoCrtVectorIterator(m_pItem + cItems);
101 }
102
103 RTCNoCrtVectorIterator operator-(difference_type cItems) const RT_NOEXCEPT
104 {
105 return RTCNoCrtVectorIterator(m_pItem - cItems);
106 }
107
108 /** @} */
109
110 /** @name Item access
111 * @{ */
112 typename reference operator*() const RT_NOEXCEPT
113 {
114 return *m_pItem;
115 }
116
117 pointer operator->() const RT_NOEXCEPT
118 {
119 return m_pItem;
120 }
121
122 reference operator[](difference_type iItem) const RT_NOEXCEPT
123 {
124 return m_pItem[iItem];
125 }
126
127 /** @} */
128
129 /** Helper for const/non-const iterator comparisons: */
130 inline typename a_Container::const_pointer getConst() const RT_NOEXCEPT
131 {
132 return m_pItem;
133 }
134 };
135
136 template<typename a_TypeLeft, typename a_TypeRight, class a_Container>
137 inline bool operator==(const RTCNoCrtVectorIterator<a_TypeLeft, a_Container> &a_rLeft,
138 const RTCNoCrtVectorIterator<a_TypeRight, a_Container> &a_rRight) RT_NOEXCEPT
139 {
140 return a_rLeft.getConst() == a_rRight.getConst();
141 }
142
143 template<typename a_TypeLeft, typename a_TypeRight, class a_Container>
144 inline bool operator!=(const RTCNoCrtVectorIterator<a_TypeLeft, a_Container> &a_rLeft,
145 const RTCNoCrtVectorIterator<a_TypeRight, a_Container> &a_rRight) RT_NOEXCEPT
146 {
147 return a_rLeft.getConst() == a_rRight.getConst();
148 }
149
150 template<typename a_TypeLeft, typename a_TypeRight, class a_Container>
151 inline bool operator<(const RTCNoCrtVectorIterator<a_TypeLeft, a_Container> &a_rLeft,
152 const RTCNoCrtVectorIterator<a_TypeRight, a_Container> &a_rRight) RT_NOEXCEPT
153 {
154 return (uintptr_t)a_rLeft.getConst() < (uintptr_t)a_rRight.getConst();
155 }
156
157 template<typename a_TypeLeft, typename a_TypeRight, class a_Container>
158 inline bool operator<=(const RTCNoCrtVectorIterator<a_TypeLeft, a_Container> &a_rLeft,
159 const RTCNoCrtVectorIterator<a_TypeRight, a_Container> &a_rRight) RT_NOEXCEPT
160 {
161 return (uintptr_t)a_rLeft.getConst() <= (uintptr_t)a_rRight.getConst();
162 }
163
164 template<typename a_TypeLeft, typename a_TypeRight, class a_Container>
165 inline bool operator>(const RTCNoCrtVectorIterator<a_TypeLeft, a_Container> &a_rLeft,
166 const RTCNoCrtVectorIterator<a_TypeRight, a_Container> &a_rRight) RT_NOEXCEPT
167 {
168 return (uintptr_t)a_rLeft.getConst() > (uintptr_t)a_rRight.getConst();
169 }
170
171 template<typename a_TypeLeft, typename a_TypeRight, class a_Container>
172 inline bool operator>=(const RTCNoCrtVectorIterator<a_TypeLeft, a_Container> &a_rLeft,
173 const RTCNoCrtVectorIterator<a_TypeRight, a_Container> &a_rRight) RT_NOEXCEPT
174 {
175 return (uintptr_t)a_rLeft.getConst() >= (uintptr_t)a_rRight.getConst();
176 }
177
178
179
180 template<class a_Type, class a_Allocator = std::allocator<a_Type> >
181 class vector
182 {
183 public:
184 typedef a_Type value_type;
185 typedef a_Type &reference;
186 typedef a_Type const &const_reference;
187 typedef a_Allocator allocator_type;
188 typedef typename a_Allocator::size_type size_type;
189 typedef typename a_Allocator::difference_type difference_type;
190 typedef typename a_Allocator::pointer pointer;
191 typedef typename a_Allocator::const_pointer const_pointer;
192
193 typedef RTCNoCrtVectorIterator<a_Type, vector> iterator;
194 typedef RTCNoCrtVectorIterator<const a_Type, vector> const_iterator;
195
196 protected:
197 pointer m_paItems;
198 size_t m_cItems;
199 size_t m_cAllocated;
200 allocator_type m_Allocator;
201
202 public:
203 vector() RT_NOEXCEPT
204 : m_paItems(NULL)
205 , m_cItems(0)
206 , m_cAllocated(0)
207 { }
208
209 vector(size_type a_cAllocate)
210 : m_paItems(NULL)
211 , m_cItems(0)
212 , m_cAllocated(0)
213 {
214 m_paItems = m_Allocator.allocate(a_cAllocate);
215 if (m_paItems)
216 m_cAllocated = a_cAllocate;
217 }
218
219 ~vector()
220 {
221 size_type i = m_cItems;
222 while (i-- > 0)
223 {
224 m_Allocator.destroy(&m_paItems[i]);
225 m_cItems = i;
226 }
227 m_Allocator.deallocate(m_paItems, m_cAllocated);
228 m_paItems = NULL;
229 m_cAllocated = 0;
230 }
231
232
233 /** @name Iterators
234 * @{ */
235 iterator begin() RT_NOEXCEPT
236 {
237 return iterator(m_paItems);
238 }
239
240 const_iterator begin() const RT_NOEXCEPT
241 {
242 return const_iterator(m_paItems);
243 }
244
245 const_iterator cbegin() const RT_NOEXCEPT
246 {
247 return const_iterator(m_paItems);
248 }
249
250 iterator end() RT_NOEXCEPT
251 {
252 return iterator(&m_paItems[m_cItems]);
253 }
254
255 const_iterator end() const RT_NOEXCEPT
256 {
257 return const_iterator(&m_paItems[m_cItems]);
258 }
259
260 const_iterator cend() const RT_NOEXCEPT
261 {
262 return const_iterator(&m_paItems[m_cItems]);
263 }
264 /** @} */
265
266 /** @name Element access
267 * @{ */
268 reference operator[](size_type iItem) RT_NOEXCEPT
269 {
270 Assert(iItem < m_cAllocated);
271 return m_paItems[iItem];
272 }
273
274 const_reference operator[](size_type iItem) const RT_NOEXCEPT
275 {
276 Assert(iItem < m_cAllocated);
277 return m_paItems[iItem];
278 }
279
280 reference front() RT_NOEXCEPT
281 {
282 return m_paItems[0];
283 }
284
285 const_reference front() const RT_NOEXCEPT
286 {
287 return m_paItems[0];
288 }
289
290 reference back() RT_NOEXCEPT
291 {
292 return m_paItems[m_cItems - 1];
293 }
294
295 const_reference back() const RT_NOEXCEPT
296 {
297 return m_paItems[m_cItems - 1];
298 }
299
300 pointer data() RT_NOEXCEPT
301 {
302 return m_paItem;
303 }
304
305 const_pointer data() const RT_NOEXCEPT
306 {
307 return m_paItem;
308 }
309
310 /** @} */
311
312 /** @name Capacity
313 * @{ */
314 bool empty() const RT_NOEXCEPT
315 {
316 return m_cItems == 0;
317 }
318
319 size_type size() const RT_NOEXCEPT
320 {
321 return m_cItems;
322 }
323
324 size_type max_size() const RT_NOEXCEPT
325 {
326 return m_Allocator.max_size();
327 }
328
329 void reserve(size_type a_cNewAllocated)
330 {
331 Assert(a_cNewAllocated <= max_size());
332
333 if (a_cNewAllocated > m_cAllocated)
334 {
335 vector Temp(a_cNewAllocated);
336 if (Temp.m_paItems)
337 {
338 /* Copy over the data: */
339 size_type const cItems = m_cItems;
340 const_pointer paSrc = m_paItems;
341 pointer paDst = Temp.m_paItems;
342 for (size_type i = 0; i < cItems; Temp.m_cItems = ++i)
343 m_Allocator.construct(&paDst[i], paSrc[i]);
344
345 /* Swap the data. */
346 size_type const cOldAllocated = m_cAllocated;
347 Temp.m_paItems = m_paItems;
348 m_paItems = paDst;
349 m_cAllocated = Temp.m_cAllocated;
350 Temp.m_cAllocated = cOldAllocated;
351 }
352 }
353 }
354
355 /** @} */
356
357 /** @name Modifiers
358 * @{ */
359 void push_back(const_reference a_rValue)
360 {
361 if (m_cItems < m_cAllocated)
362 { }
363 else
364 {
365 Assert(m_cItems * 2 >= m_cItems);
366 reserve(m_cItems < 8 ? 8 : m_cItems * 2); /* This might be non-standard. */
367 AssertReturnVoid(m_cItems < m_cAllocated);
368 }
369 m_paItems[m_cItems] = a_rValue;
370 m_cItems++;
371 }
372
373 void pop_back() RT_NOEXCEPT
374 {
375 if (m_cItems > 0)
376 m_cItems -= 1;
377 }
378 /** @} */
379
380 };
381
382}
383
384#endif /* !IPRT_INCLUDED_nocrt_vector */
385
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette