forked from questor/eastl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixed_vector.h
644 lines (493 loc) · 27.1 KB
/
fixed_vector.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Electronic Arts Inc. All rights reserved.
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// This file implements a vector which uses a fixed size memory pool.
// The bEnableOverflow template parameter allows the container to resort to
// heap allocations if the memory pool is exhausted.
///////////////////////////////////////////////////////////////////////////////
#ifndef EASTL_FIXED_VECTOR_H
#define EASTL_FIXED_VECTOR_H
#include <eastl/vector.h>
#include <eastl/internal/fixed_pool.h>
#if defined(EASTL_PRAGMA_ONCE_SUPPORTED)
#pragma once // Some compilers (e.g. VC++) benefit significantly from using this. We've measured 3-4% build speed improvements in apps as a result.
#endif
namespace eastl
{
/// EASTL_FIXED_VECTOR_DEFAULT_NAME
///
/// Defines a default container name in the absence of a user-provided name.
/// In the case of fixed-size containers, the allocator name always refers
/// to overflow allocations.
///
#ifndef EASTL_FIXED_VECTOR_DEFAULT_NAME
#define EASTL_FIXED_VECTOR_DEFAULT_NAME EASTL_DEFAULT_NAME_PREFIX " fixedVector" // Unless the user overrides something, this is "EASTL fixedVector".
#endif
/// EASTL_FIXED_VECTOR_DEFAULT_ALLOCATOR
///
#ifndef EASTL_FIXED_VECTOR_DEFAULT_ALLOCATOR
#define EASTL_FIXED_VECTOR_DEFAULT_ALLOCATOR overflow_allocator_type(EASTL_FIXED_VECTOR_DEFAULT_NAME)
#endif
/// fixedVector
///
/// A fixedVector with bEnableOverflow == true is identical to a regular
/// vector in terms of its behavior. All the expectations of regular vector
/// apply to it and no additional expectations come from it. When bEnableOverflow
/// is false, fixedVector behaves like regular vector with the exception that
/// its capacity can never increase. All operations you do on such a fixedVector
/// which require a capacity increase will result in undefined behavior or an
/// C++ allocation exception, depending on the configuration of EASTL.
///
/// Template parameters:
/// T The type of object the vector holds.
/// nodeCount The max number of objects to contain.
/// bEnableOverflow Whether or not we should use the overflow heap if our object pool is exhausted.
/// OverflowAllocator Overflow allocator, which is only used if bEnableOverflow == true. Defaults to the global heap.
///
/// Note: The nodeCount value must be at least 1.
///
/// Example usage:
/// fixedVector<Widget, 128, true> fixedVector);
///
/// fixedVector.pushBack(Widget());
/// fixedVector.resize(200);
/// fixedVector.clear();
///
template <typename T, size_t nodeCount, bool bEnableOverflow = true, typename OverflowAllocator = typename eastl::type_select<bEnableOverflow, EASTLAllocatorType, EASTLDummyAllocatorType>::type>
class fixedVector : public vector<T, fixedVector_allocator<sizeof(T), nodeCount, EASTL_ALIGN_OF(T), 0, bEnableOverflow, OverflowAllocator> >
{
public:
typedef fixedVector_allocator<sizeof(T), nodeCount, EASTL_ALIGN_OF(T),
0, bEnableOverflow, OverflowAllocator> fixedAllocator_type;
typedef OverflowAllocator overflow_allocator_type;
typedef vector<T, fixedAllocator_type> base_type;
typedef fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator> this_type;
typedef typename base_type::size_type size_type;
typedef typename base_type::value_type value_type;
typedef typename base_type::reference reference;
typedef typename base_type::iterator iterator;
typedef typename base_type::const_iterator const_iterator;
typedef aligned_buffer<nodeCount * sizeof(T), EASTL_ALIGN_OF(T)> aligned_buffer_type;
enum { kMaxSize = nodeCount };
using base_type::mAllocator;
using base_type::mpBegin;
using base_type::mpEnd;
using base_type::mpCapacity;
using base_type::resize;
using base_type::clear;
using base_type::size;
using base_type::assign;
using base_type::npos;
using base_type::DoAllocate;
using base_type::DoFree;
using base_type::DoAssign;
using base_type::DoAssignFromIterator;
protected:
aligned_buffer_type mBuffer;
public:
fixedVector();
explicit fixedVector(const overflow_allocator_type& overflowAllocator); // Only applicable if bEnableOverflow is true.
explicit fixedVector(size_type n); // Currently we don't support overflowAllocator specification for other constructors, for simplicity.
fixedVector(size_type n, const value_type& value);
fixedVector(const this_type& x);
#if EASTL_MOVE_SEMANTICS_ENABLED
fixedVector(this_type&& x);
fixedVector(this_type&& x, const overflow_allocator_type& overflowAllocator);
#endif
fixedVector(std::initializer_list<T> ilist, const overflow_allocator_type& overflowAllocator = EASTL_FIXED_VECTOR_DEFAULT_ALLOCATOR);
template <typename InputIterator>
fixedVector(InputIterator first, InputIterator last);
this_type& operator=(const this_type& x);
this_type& operator=(std::initializer_list<T> ilist);
#if EASTL_MOVE_SEMANTICS_ENABLED
this_type& operator=(this_type&& x);
#endif
void swap(this_type& x);
void setCapacity(size_type n);
void clear(bool freeOverflow);
void reset_lose_memory(); // This is a unilateral reset to an initially empty state. No destructors are called, no deallocation occurs.
size_type maxSize() const; // Returns the max fixed size, which is the user-supplied nodeCount parameter.
bool full() const; // Returns true if the fixed space has been fully allocated. Note that if overflow is enabled, the container size can be greater than nodeCount but full() could return true because the fixed space may have a recently freed slot.
bool hasOverflowed() const; // Returns true if the allocations spilled over into the overflow allocator. Meaningful only if overflow is enabled.
bool can_overflow() const; // Returns the value of the bEnableOverflow template parameter.
void* pushBackUninitialized();
void pushBack(const value_type& value); // We implement pushBack here because we have a specialization that's
reference pushBack(); // smaller for the case of overflow being disabled.
#if EASTL_MOVE_SEMANTICS_ENABLED
void pushBack(value_type&& value);
#endif
// OverflowAllocator
const overflow_allocator_type& getOverflowAllocator() const EASTL_NOEXCEPT;
overflow_allocator_type& getOverflowAllocator() EASTL_NOEXCEPT;
void setOverflowAllocator(const overflow_allocator_type& allocator);
// Deprecated:
#if EASTL_RESET_ENABLED
void reset(); // This function name is deprecated; use reset_lose_memory instead.
#endif
protected:
void* DoPushBackUninitialized(true_type);
void* DoPushBackUninitialized(false_type);
void DoPushBack(true_type, const value_type& value);
void DoPushBack(false_type, const value_type& value);
#if EASTL_MOVE_SEMANTICS_ENABLED
void DoPushBackMove(true_type, value_type&& value);
void DoPushBackMove(false_type, value_type&& value);
#endif
reference DoPushBack(false_type);
reference DoPushBack(true_type);
}; // fixedVector
///////////////////////////////////////////////////////////////////////
// fixedVector
///////////////////////////////////////////////////////////////////////
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::fixedVector()
: base_type(fixedAllocator_type(mBuffer.buffer))
{
#if EASTL_NAME_ENABLED
mAllocator.setName(EASTL_FIXED_VECTOR_DEFAULT_NAME);
#endif
mpBegin = mpEnd = (value_type*)&mBuffer.buffer[0];
mpCapacity = mpBegin + nodeCount;
}
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::fixedVector(const overflow_allocator_type& overflowAllocator)
: base_type(fixedAllocator_type(mBuffer.buffer, overflowAllocator))
{
#if EASTL_NAME_ENABLED
mAllocator.setName(EASTL_FIXED_VECTOR_DEFAULT_NAME);
#endif
mpBegin = mpEnd = (value_type*)&mBuffer.buffer[0];
mpCapacity = mpBegin + nodeCount;
}
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::fixedVector(size_type n)
: base_type(fixedAllocator_type(mBuffer.buffer))
{
#if EASTL_NAME_ENABLED
mAllocator.setName(EASTL_FIXED_VECTOR_DEFAULT_NAME);
#endif
mpBegin = mpEnd = (value_type*)&mBuffer.buffer[0];
mpCapacity = mpBegin + nodeCount;
resize(n);
}
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::fixedVector(size_type n, const value_type& value)
: base_type(fixedAllocator_type(mBuffer.buffer))
{
#if EASTL_NAME_ENABLED
mAllocator.setName(EASTL_FIXED_VECTOR_DEFAULT_NAME);
#endif
mpBegin = mpEnd = (value_type*)&mBuffer.buffer[0];
mpCapacity = mpBegin + nodeCount;
resize(n, value);
}
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::fixedVector(const this_type& x)
: base_type(fixedAllocator_type(mBuffer.buffer))
{
mAllocator.copy_overflow_allocator(x.mAllocator);
#if EASTL_NAME_ENABLED
mAllocator.setName(x.mAllocator.getName());
#endif
mpBegin = mpEnd = (value_type*)&mBuffer.buffer[0];
mpCapacity = mpBegin + nodeCount;
base_type::template DoAssign<const_iterator, false>(x.begin(), x.end(), false_type());
}
#if EASTL_MOVE_SEMANTICS_ENABLED
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::fixedVector(this_type&& x)
: base_type(fixedAllocator_type(mBuffer.buffer))
{
// Since we are a fixedVector, we can't swap pointers. We can possibly so something like fixedSwap or
// we can just do an assignment from x. If we want to do the former then we need to have some complicated
// code to deal with overflow or no overflow, and whether the memory is in the fixed-size buffer or in
// the overflow allocator. 90% of the time the memory should be in the fixed buffer, in which case
// a simple assignment is no worse than the fancy pathway.
// Since we are a fixedList, we can't normally swap pointers unless both this and
// x are using using overflow and the overflow allocators are equal. To do:
//if(hasOverflowed() && x.hasOverflowed() && (getOverflowAllocator() == x.getOverflowAllocator()))
//{
// We can swap contents and may need to swap the allocators as well.
//}
// The following is currently identical to the fixedVector(const this_type& x) code above. If it stays that
// way then we may want to make a shared implementation.
mAllocator.copy_overflow_allocator(x.mAllocator);
#if EASTL_NAME_ENABLED
mAllocator.setName(x.mAllocator.getName());
#endif
mpBegin = mpEnd = (value_type*)&mBuffer.buffer[0];
mpCapacity = mpBegin + nodeCount;
base_type::template DoAssign<iterator, true>(x.begin(), x.end(), false_type());
}
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::fixedVector(this_type&& x, const overflow_allocator_type& overflowAllocator)
: base_type(fixedAllocator_type(mBuffer.buffer, overflowAllocator))
{
// See the discussion above.
// The following is currently identical to the fixedVector(const this_type& x) code above. If it stays that
// way then we may want to make a shared implementation.
mAllocator.copy_overflow_allocator(x.mAllocator);
#if EASTL_NAME_ENABLED
mAllocator.setName(x.mAllocator.getName());
#endif
mpBegin = mpEnd = (value_type*)&mBuffer.buffer[0];
mpCapacity = mpBegin + nodeCount;
base_type::template DoAssign<iterator, true>(x.begin(), x.end(), false_type());
}
#endif
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::fixedVector(std::initializer_list<T> ilist, const overflow_allocator_type& overflowAllocator)
: base_type(fixedAllocator_type(mBuffer.buffer, overflowAllocator))
{
typedef typename std::initializer_list<value_type>::iterator InputIterator;
typedef typename eastl::iterator_traits<InputIterator>::iterator_category IC;
mpBegin = mpEnd = (value_type*)&mBuffer.buffer[0];
mpCapacity = mpBegin + nodeCount;
base_type::template DoAssignFromIterator<InputIterator, false>(ilist.begin(), ilist.end(), IC());
}
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
template <typename InputIterator>
fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::fixedVector(InputIterator first, InputIterator last)
: base_type(fixedAllocator_type(mBuffer.buffer))
{
#if EASTL_NAME_ENABLED
mAllocator.setName(EASTL_FIXED_VECTOR_DEFAULT_NAME);
#endif
mpBegin = mpEnd = (value_type*)&mBuffer.buffer[0];
mpCapacity = mpBegin + nodeCount;
base_type::template DoAssign<InputIterator, false>(first, last, is_integral<InputIterator>());
}
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline typename fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::this_type&
fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::operator=(const this_type& x)
{
if(this != &x)
{
clear();
#if EASTL_ALLOCATOR_COPY_ENABLED
mAllocator = x.mAllocator; // The primary effect of this is to copy the overflow allocator.
#endif
base_type::template DoAssign<const_iterator, false>(x.begin(), x.end(), false_type()); // Shorter route.
}
return *this;
}
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline typename fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::this_type&
fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::operator=(std::initializer_list<T> ilist)
{
typedef typename std::initializer_list<value_type>::iterator InputIterator;
typedef typename eastl::iterator_traits<InputIterator>::iterator_category IC;
clear();
base_type::template DoAssignFromIterator<InputIterator, false>(ilist.begin(), ilist.end(), IC());
return *this;
}
#if EASTL_MOVE_SEMANTICS_ENABLED
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline typename fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::this_type&
fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::operator=(this_type&& x)
{
// Since we are a fixedVector, we can't swap pointers. We can possibly so something like fixedSwap or
// we can just do an assignment from x. If we want to do the former then we need to have some complicated
// code to deal with overflow or no overflow, and whether the memory is in the fixed-size buffer or in
// the overflow allocator. 90% of the time the memory should be in the fixed buffer, in which case
// a simple assignment is no worse than the fancy pathway.
return operator=(x);
}
#endif
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline void fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::swap(this_type& x)
{
if((hasOverflowed() && x.hasOverflowed()) && (getOverflowAllocator() == x.getOverflowAllocator())) // If both containers are using the heap instead of local memory
{ // then we can do a fast pointer swap instead of content swap.
eastl::swap(mpBegin, x.mpBegin);
eastl::swap(mpEnd, x.mpEnd);
eastl::swap(mpCapacity, x.mpCapacity);
}
else
{
// Fixed containers use a special swap that can deal with excessively large buffers.
eastl::fixedSwap(*this, x);
}
}
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline void fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::setCapacity(size_type n)
{
const size_type nPrevSize = (size_type)(mpEnd - mpBegin);
const size_type nPrevCapacity = (size_type)(mpCapacity - mpBegin);
if(n == npos) // If the user means to set the capacity so that it equals the size (i.e. free excess capacity)...
n = nPrevSize;
if(n != nPrevCapacity) // If the request results in a capacity change...
{
if(can_overflow() && (((uintptr_t)mpBegin != (uintptr_t)mBuffer.buffer) || (n > kMaxSize))) // If we are or would be using dynamically allocated memory instead of our fixed-size member buffer...
{
T* const pNewData = (n <= kMaxSize) ? (T*)&mBuffer.buffer[0] : DoAllocate(n);
T* const pCopyEnd = (n < nPrevSize) ? (mpBegin + n) : mpEnd;
eastl::uninitializedMove_ptr(mpBegin, pCopyEnd, pNewData); // Move [mpBegin, pCopyEnd) to p.
eastl::destruct(mpBegin, mpEnd);
if((uintptr_t)mpBegin != (uintptr_t)mBuffer.buffer)
DoFree(mpBegin, (size_type)(mpCapacity - mpBegin));
mpEnd = pNewData + (pCopyEnd - mpBegin);
mpBegin = pNewData;
mpCapacity = mpBegin + n;
} // Else the new capacity would be within our fixed buffer.
else if(n < nPrevSize) // If the newly requested capacity is less than our size, we do what vector::setCapacity does and resize, even though we actually aren't reducing the capacity.
resize(n);
}
}
template <typename T, size_t nodeCount, bool bEnableOverflow, typename Allocator>
inline void fixedVector<T, nodeCount, bEnableOverflow, Allocator>::clear(bool freeOverflow)
{
base_type::clear();
if (freeOverflow && mpBegin != (value_type*)&mBuffer.buffer[0])
{
EASTLFree(mAllocator, mpBegin, (mpCapacity - mpBegin) * sizeof(T));
mpBegin = mpEnd = (value_type*)&mBuffer.buffer[0];
mpCapacity = mpBegin + nodeCount;
}
}
#if EASTL_RESET_ENABLED
// This function name is deprecated; use reset_lose_memory instead.
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline void fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::reset()
{
reset_lose_memory();
}
#endif
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline void fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::reset_lose_memory()
{
mpBegin = mpEnd = (value_type*)&mBuffer.buffer[0];
mpCapacity = mpBegin + nodeCount;
}
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline typename fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::size_type
fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::maxSize() const
{
return kMaxSize;
}
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline bool fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::full() const
{
// If size >= capacity, then we are definitely full.
// Also, if our size is smaller but we've switched away from mBuffer due to a previous overflow, then we are considered full.
return ((size_t)(mpEnd - mpBegin) >= kMaxSize) || ((void*)mpBegin != (void*)mBuffer.buffer);
}
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline bool fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::hasOverflowed() const
{
// This will be incorrect for the case that bOverflowEnabled is true and the container was resized
// down to a small size where the fixed buffer could take over ownership of the data again.
// The only simple fix for this is to take on another member variable which tracks whether this overflow
// has occurred at some point in the past.
return ((void*)mpBegin != (void*)mBuffer.buffer);
}
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline bool fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::can_overflow() const
{
return bEnableOverflow;
}
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline void* fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::pushBackUninitialized()
{
return DoPushBackUninitialized(typename type_select<bEnableOverflow, true_type, false_type>::type());
}
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline void* fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::DoPushBackUninitialized(true_type)
{
return base_type::pushBackUninitialized();
}
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline void* fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::DoPushBackUninitialized(false_type)
{
EASTL_ASSERT(mpEnd < mpCapacity);
return mpEnd++;
}
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline void fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::pushBack(const value_type& value)
{
DoPushBack(typename type_select<bEnableOverflow, true_type, false_type>::type(), value);
}
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline void fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::DoPushBack(true_type, const value_type& value)
{
base_type::pushBack(value);
}
// This template specializes for overflow NOT enabled.
// In this configuration, there is no need for the heavy weight pushBack() which tests to see if the container should grow (it never will)
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline void fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::DoPushBack(false_type, const value_type& value)
{
EASTL_ASSERT(mpEnd < mpCapacity);
::new((void*)mpEnd++) value_type(value);
}
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline typename fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::reference fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::pushBack()
{
return DoPushBack(typename type_select<bEnableOverflow, true_type, false_type>::type());
}
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline typename fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::reference fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::DoPushBack(true_type)
{
return base_type::pushBack();
}
// This template specializes for overflow NOT enabled.
// In this configuration, there is no need for the heavy weight pushBack() which tests to see if the container should grow (it never will)
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline typename fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::reference fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::DoPushBack(false_type)
{
EASTL_ASSERT(mpEnd < mpCapacity);
::new((void*)mpEnd++) value_type; // Note that this isn't value_type() as that syntax doesn't work on all compilers for POD types.
return *(mpEnd - 1); // Same as return back();
}
#if EASTL_MOVE_SEMANTICS_ENABLED
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline void fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::pushBack(value_type&& value)
{
DoPushBackMove(typename type_select<bEnableOverflow, true_type, false_type>::type(), eastl::move(value));
}
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline void fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::DoPushBackMove(true_type, value_type&& value)
{
base_type::pushBack(eastl::move(value)); // This will call vector::pushBack(value_type &&), and possibly swap value with *mpEnd.
}
// This template specializes for overflow NOT enabled.
// In this configuration, there is no need for the heavy weight pushBack() which tests to see if the container should grow (it never will)
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline void fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::DoPushBackMove(false_type, value_type&& value)
{
EASTL_ASSERT(mpEnd < mpCapacity);
::new((void*)mpEnd++) value_type(eastl::move(value)); // This will call the value_type(value_type&&) constructor, and possibly swap value with *mpEnd.
}
#endif
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline const typename fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::overflow_allocator_type&
fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::getOverflowAllocator() const EASTL_NOEXCEPT
{
return mAllocator.getOverflowAllocator();
}
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline typename fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::overflow_allocator_type&
fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::getOverflowAllocator() EASTL_NOEXCEPT
{
return mAllocator.getOverflowAllocator();
}
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline void
fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>::setOverflowAllocator(const overflow_allocator_type& allocator)
{
mAllocator.setOverflowAllocator(allocator);
}
///////////////////////////////////////////////////////////////////////
// global operators
///////////////////////////////////////////////////////////////////////
// operator ==, !=, <, >, <=, >= come from the vector implementations.
template <typename T, size_t nodeCount, bool bEnableOverflow, typename OverflowAllocator>
inline void swap(fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>& a,
fixedVector<T, nodeCount, bEnableOverflow, OverflowAllocator>& b)
{
// Fixed containers use a special swap that can deal with excessively large buffers.
eastl::fixedSwap(a, b);
}
} // namespace eastl
#endif // Header include guard