-
Notifications
You must be signed in to change notification settings - Fork 7
/
any.h
652 lines (547 loc) · 18.9 KB
/
any.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
645
646
647
648
649
650
651
652
/////////////////////////////////////////////////////////////////////////////
// Copyright (c) Electronic Arts Inc. All rights reserved.
/////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// This file implements the eastl::any which is part of the C++ standard STL
// library specification.
//
// eastl::any is a type-safe container for single values of any type. Our
// implementation makes use of the "small local buffer" optimization to avoid
// unnecessary dynamic memory allocation if the specified type is eligible to
// be stored in its local buffer. The user type must satisfy the size
// requirements and must be no-throw move-constructible to qualify for the local
// buffer optimization.
//
// To consider: Implement a fixed_any<SIZE> variant to allow users to customize
// the size of the "small local buffer" optimization.
//
// http://en.cppreference.com/w/cpp/utility/any
///////////////////////////////////////////////////////////////////////////////
#ifndef EASTL_ANY_H
#define EASTL_ANY_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
#include <eastl/internal/config.h>
#include <eastl/internal/in_place_t.h>
#if EASTL_RTTI_ENABLED
#include <typeinfo>
#endif
#if EASTL_EXCEPTIONS_ENABLED
#include <exception>
#endif
namespace eastl
{
///////////////////////////////////////////////////////////////////////////////
// bad_any_cast
//
// The type thrown by any_cast on failure.
//
// http://en.cppreference.com/w/cpp/utility/any/bad_any_cast
//
#if EASTL_EXCEPTIONS_ENABLED
struct bad_cast : std::exception
{
const char* what() const EASTL_NOEXCEPT EASTL_OVERRIDE
{ return "bad cast"; }
};
struct bad_any_cast : public bad_cast
{
const char* what() const EASTL_NOEXCEPT EASTL_OVERRIDE
{ return "bad_any_cast"; }
};
#endif
namespace Internal
{
// utility to switch between exceptions and asserts
inline void DoBadAnyCast()
{
#if EASTL_EXCEPTIONS_ENABLED
throw bad_any_cast();
#else
EASTL_ASSERT_MSG(false, "bad_any_cast\n");
// NOTE(rparolin): CRASH!
// You crashed here because you requested a type that was not contained in the object.
// We choose to intentionally crash here instead of returning invalid data to the calling
// code which could cause hard to track down bugs.
*((volatile int*)0) = 0xDEADC0DE;
#endif
}
template<typename T, typename... Args>
void* DefaultConstruct(Args&&... args)
{
auto* pMem = EASTLAllocatorDefault()->allocate(sizeof(T), alignof(T), 0);
return ::new(pMem) T(eastl::forward<Args>(args)...);
}
template<typename T>
void DefaultDestroy(T* p)
{
p->~T();
EASTLAllocatorDefault()->deallocate(static_cast<void*>(p), sizeof(T));
}
}
///////////////////////////////////////////////////////////////////////////////
// 20.7.3, class any
//
class any
{
//////////////////////////////////////////////////////////////////////////////////////////
// storage_operation
//
// operations supported by the storage handler
//
enum class storage_operation
{
GET,
DESTROY,
COPY,
MOVE,
TYPE_INFO
};
//////////////////////////////////////////////////////////////////////////////////////////
// storage
//
// the underlying storage type which enables the switching between objects stored in
// the heap and objects stored within the any type.
//
union storage
{
typedef aligned_storage_t<4 * sizeof(void*), alignment_of<void*>::value> internal_storage_t;
void* external_storage = nullptr;
internal_storage_t internal_storage;
};
//////////////////////////////////////////////////////////////////////////////////////////
// use_internal_storage
//
// determines when the "local buffer optimization" is used
//
template <typename T>
using use_internal_storage = bool_constant
<
is_nothrow_move_constructible<T>::value
&& (sizeof(T) <= sizeof(storage)) &&
(alignment_of<storage>::value % alignment_of<T>::value == 0)
>;
//////////////////////////////////////////////////////////////////////////////////////////
// non-member friend functions
//
template <class ValueType> friend const ValueType* any_cast(const any* pAny) EASTL_NOEXCEPT;
template <class ValueType> friend ValueType* any_cast(any* pAny) EASTL_NOEXCEPT;
template <class ValueType> friend ValueType any_cast(const any& operand);
template <class ValueType> friend ValueType any_cast(any& operand);
template <class ValueType> friend ValueType any_cast(any&& operand);
//Adding Unsafe any cast operations
template <class ValueType> friend const ValueType* unsafe_any_cast(const any* pAny) EASTL_NOEXCEPT;
template <class ValueType> friend ValueType* unsafe_any_cast(any* pAny) EASTL_NOEXCEPT;
//////////////////////////////////////////////////////////////////////////////////////////
// internal storage handler
//
template <typename T>
struct storage_handler_internal
{
template <typename V>
static void construct(storage& s, V&& v)
{
::new(&s.internal_storage) T(eastl::forward<V>(v));
}
template <typename... Args>
static void construct_inplace(storage& s, Args... args)
{
::new(&s.internal_storage) T(eastl::forward<Args>(args)...);
}
template <class NT, class U, class... Args>
static void construct_inplace(storage& s, std::initializer_list<U> il, Args&&... args)
{
::new(&s.internal_storage) NT(il, eastl::forward<Args>(args)...);
}
static inline void destroy(any& refAny)
{
T& t = *static_cast<T*>(static_cast<void*>(&refAny.m_storage.internal_storage));
EA_UNUSED(t);
t.~T();
refAny.m_handler = nullptr;
}
static void* handler_func(storage_operation op, const any* pThis, any* pOther)
{
switch (op)
{
case storage_operation::GET:
{
EASTL_ASSERT(pThis);
return (void*)(&pThis->m_storage.internal_storage);
}
break;
case storage_operation::DESTROY:
{
EASTL_ASSERT(pThis);
destroy(const_cast<any&>(*pThis));
}
break;
case storage_operation::COPY:
{
EASTL_ASSERT(pThis);
EASTL_ASSERT(pOther);
construct(pOther->m_storage, *(T*)(&pThis->m_storage.internal_storage));
}
break;
case storage_operation::MOVE:
{
EASTL_ASSERT(pThis);
EASTL_ASSERT(pOther);
construct(pOther->m_storage, eastl::move(*(T*)(&pThis->m_storage.internal_storage)));
destroy(const_cast<any&>(*pThis));
}
break;
case storage_operation::TYPE_INFO:
{
#if EASTL_RTTI_ENABLED
return (void*)&typeid(T);
#endif
}
break;
default:
{
EASTL_ASSERT_MSG(false, "unknown storage operation\n");
}
break;
};
return nullptr;
}
};
//////////////////////////////////////////////////////////////////////////////////////////
// external storage handler
//
template <typename T>
struct storage_handler_external
{
template <typename V>
static inline void construct(storage& s, V&& v)
{
s.external_storage = Internal::DefaultConstruct<T>(eastl::forward<V>(v));
}
template <typename... Args>
static inline void construct_inplace(storage& s, Args... args)
{
s.external_storage = Internal::DefaultConstruct<T>(eastl::forward<Args>(args)...);
}
template <class NT, class U, class... Args>
static inline void construct_inplace(storage& s, std::initializer_list<U> il, Args&&... args)
{
s.external_storage = Internal::DefaultConstruct<NT>(il, eastl::forward<Args>(args)...);
}
static inline void destroy(any& refAny)
{
Internal::DefaultDestroy(static_cast<T*>(refAny.m_storage.external_storage));
refAny.m_handler = nullptr;
}
static void* handler_func(storage_operation op, const any* pThis, any* pOther)
{
switch (op)
{
case storage_operation::GET:
{
EASTL_ASSERT(pThis);
EASTL_ASSERT(pThis->m_storage.external_storage);
return static_cast<void*>(pThis->m_storage.external_storage);
}
break;
case storage_operation::DESTROY:
{
EASTL_ASSERT(pThis);
destroy(*const_cast<any*>(pThis));
}
break;
case storage_operation::COPY:
{
EASTL_ASSERT(pThis);
EASTL_ASSERT(pOther);
construct(pOther->m_storage, *static_cast<T*>(pThis->m_storage.external_storage));
}
break;
case storage_operation::MOVE:
{
EASTL_ASSERT(pThis);
EASTL_ASSERT(pOther);
construct(pOther->m_storage, eastl::move(*(T*)(pThis->m_storage.external_storage)));
destroy(const_cast<any&>(*pThis));
}
break;
case storage_operation::TYPE_INFO:
{
#if EASTL_RTTI_ENABLED
return (void*)&typeid(T);
#endif
}
break;
default:
{
EASTL_ASSERT_MSG(false, "unknown storage operation\n");
}
break;
};
return nullptr;
}
};
//////////////////////////////////////////////////////////////////////////////////////////
// storage_handler_ptr
//
// defines the function signature of the storage handler that both the internal and
// external storage handlers must implement to retrieve the underlying type of the any
// object.
//
using storage_handler_ptr = void* (*)(storage_operation, const any*, any*);
//////////////////////////////////////////////////////////////////////////////////////////
// storage_handler
//
// based on the specified type T we select the appropriate underlying storage handler
// based on the 'use_internal_storage' trait.
//
template <typename T>
using storage_handler = typename conditional<use_internal_storage<T>::value,
storage_handler_internal<T>,
storage_handler_external<T>>::type;
//////////////////////////////////////////////////////////////////////////////////////////
// data layout
//
storage m_storage;
storage_handler_ptr m_handler;
public:
#ifndef EA_COMPILER_GNUC
// TODO(rparolin): renable constexpr for GCC
EA_CONSTEXPR
#endif
any() EASTL_NOEXCEPT
: m_storage(), m_handler(nullptr) {}
any(const any& other) : m_handler(nullptr)
{
if (other.m_handler)
{
// NOTE(rparolin): You can not simply copy the underlying
// storage because it could hold a pointer to an object on the
// heap which breaks the copy semantics of the language.
other.m_handler(storage_operation::COPY, &other, this);
m_handler = other.m_handler;
}
}
any(any&& other) EASTL_NOEXCEPT : m_handler(nullptr)
{
if(other.m_handler)
{
// NOTE(rparolin): You can not simply move the underlying
// storage because because the storage class has effectively
// type erased user type so we have to defer to the handler
// function to get the type back and pass on the move request.
m_handler = eastl::move(other.m_handler);
other.m_handler(storage_operation::MOVE, &other, this);
}
}
~any() { reset(); }
template <class ValueType>
any(ValueType&& value,
typename eastl::enable_if<!eastl::is_same<typename eastl::decay<ValueType>::type, any>::value>::type* = 0)
{
typedef decay_t<ValueType> DecayedValueType;
static_assert(is_copy_constructible<DecayedValueType>::value, "ValueType must be copy-constructible");
storage_handler<DecayedValueType>::construct(m_storage, eastl::forward<ValueType>(value));
m_handler = &storage_handler<DecayedValueType>::handler_func;
}
template <class T, class... Args>
explicit any(in_place_type_t<T>, Args&&... args)
{
typedef storage_handler<decay_t<T>> StorageHandlerT;
static_assert(eastl::is_constructible<T, Args...>::value, "T must be constructible with Args...");
StorageHandlerT::construct_inplace(m_storage, eastl::forward<Args>(args)...);
m_handler = &StorageHandlerT::handler_func;
}
template <class T, class U, class... Args>
explicit any(in_place_type_t<T>,
std::initializer_list<U> il,
Args&&... args,
typename eastl::enable_if<eastl::is_constructible<T, std::initializer_list<U>&, Args...>::value,
void>::type* = 0)
{
typedef storage_handler<decay_t<T>> StorageHandlerT;
StorageHandlerT::construct_inplace(m_storage, il, eastl::forward<Args>(args)...);
m_handler = &StorageHandlerT::handler_func;
}
// 20.7.3.2, assignments
template <class ValueType>
any& operator=(ValueType&& value)
{
static_assert(is_copy_constructible<decay_t<ValueType>>::value, "ValueType must be copy-constructible");
any(eastl::forward<ValueType>(value)).swap(*this);
return *this;
}
any& operator=(const any& other)
{
any(other).swap(*this);
return *this;
}
any& operator=(any&& other) EASTL_NOEXCEPT
{
any(eastl::move(other)).swap(*this);
return *this;
}
// 20.7.3.3, modifiers
#if EASTL_VARIADIC_TEMPLATES_ENABLED
template <class T, class... Args>
void emplace(Args&&... args)
{
typedef storage_handler<decay_t<T>> StorageHandlerT;
static_assert(eastl::is_constructible<T, Args...>::value, "T must be constructible with Args...");
reset();
StorageHandlerT::construct_inplace(m_storage, eastl::forward<Args>(args)...);
m_handler = &StorageHandlerT::handler_func;
}
template <class NT, class U, class... Args>
typename eastl::enable_if<eastl::is_constructible<NT, std::initializer_list<U>&, Args...>::value, void>::type
emplace(std::initializer_list<U> il, Args&&... args)
{
typedef storage_handler<decay_t<NT>> StorageHandlerT;
reset();
StorageHandlerT::construct_inplace(m_storage, il, eastl::forward<Args>(args)...);
m_handler = &StorageHandlerT::handler_func;
}
#endif
void reset() EASTL_NOEXCEPT
{
if(m_handler)
m_handler(storage_operation::DESTROY, this, nullptr);
}
void swap(any& other) EASTL_NOEXCEPT
{
if(this == &other)
return;
if(m_handler && other.m_handler)
{
any tmp;
tmp.m_handler = other.m_handler;
other.m_handler(storage_operation::MOVE, &other, &tmp);
other.m_handler = m_handler;
m_handler(storage_operation::MOVE, this, &other);
m_handler = tmp.m_handler;
tmp.m_handler(storage_operation::MOVE, &tmp, this);
}
else if (m_handler == nullptr && other.m_handler)
{
eastl::swap(m_handler, other.m_handler);
m_handler(storage_operation::MOVE, &other, this);
}
else if(m_handler && other.m_handler == nullptr)
{
eastl::swap(m_handler, other.m_handler);
other.m_handler(storage_operation::MOVE, this, &other);
}
//else if (m_handler == nullptr && other.m_handler == nullptr)
//{
// // nothing to swap
//}
}
// 20.7.3.4, observers
bool has_value() const EASTL_NOEXCEPT { return m_handler != nullptr; }
#if EASTL_RTTI_ENABLED
inline const std::type_info& type() const EASTL_NOEXCEPT
{
if(m_handler)
{
auto* pTypeInfo = m_handler(storage_operation::TYPE_INFO, this, nullptr);
return *static_cast<const std::type_info*>(pTypeInfo);
}
else
{
return typeid(void);
}
}
#endif
};
//////////////////////////////////////////////////////////////////////////////////////////
// 20.7.4, non-member functions
//
inline void swap(any& rhs, any& lhs) EASTL_NOEXCEPT { rhs.swap(lhs); }
//////////////////////////////////////////////////////////////////////////////////////////
// 20.7.4, The non-member any_cast functions provide type-safe access to the contained object.
//
template <class ValueType>
inline ValueType any_cast(const any& operand)
{
static_assert(eastl::is_reference<ValueType>::value || eastl::is_copy_constructible<ValueType>::value,
"ValueType must be a reference or copy constructible");
auto* p = any_cast<typename add_const<typename remove_reference<ValueType>::type>::type>(&operand);
if(p == nullptr)
Internal::DoBadAnyCast();
return *p;
}
template <class ValueType>
inline ValueType any_cast(any& operand)
{
static_assert(eastl::is_reference<ValueType>::value || eastl::is_copy_constructible<ValueType>::value,
"ValueType must be a reference or copy constructible");
auto* p = any_cast<typename remove_reference<ValueType>::type>(&operand);
if(p == nullptr)
Internal::DoBadAnyCast();
return *p;
}
template <class ValueType>
inline ValueType any_cast(any&& operand)
{
static_assert(eastl::is_reference<ValueType>::value || eastl::is_copy_constructible<ValueType>::value,
"ValueType must be a reference or copy constructible");
auto* p = any_cast<typename remove_reference<ValueType>::type>(&operand);
if (p == nullptr)
Internal::DoBadAnyCast();
return *p;
}
// NOTE(rparolin): The runtime type check was commented out because in DLL builds the templated function pointer
// value will be different -- completely breaking the validation mechanism. Due to the fact that eastl::any uses
// type erasure we can't refresh (on copy/move) the cached function pointer to the internal handler function because
// we don't statically know the type.
template <class ValueType>
inline const ValueType* any_cast(const any* pAny) EASTL_NOEXCEPT
{
return (pAny && pAny->m_handler EASTL_IF_NOT_DLL(== &any::storage_handler<decay_t<ValueType>>::handler_func)
#if EASTL_RTTI_ENABLED
&& pAny->type() == typeid(typename remove_reference<ValueType>::type)
#endif
) ?
static_cast<const ValueType*>(pAny->m_handler(any::storage_operation::GET, pAny, nullptr)) :
nullptr;
}
template <class ValueType>
inline ValueType* any_cast(any* pAny) EASTL_NOEXCEPT
{
return (pAny && pAny->m_handler EASTL_IF_NOT_DLL(== &any::storage_handler<decay_t<ValueType>>::handler_func)
#if EASTL_RTTI_ENABLED
&& pAny->type() == typeid(typename remove_reference<ValueType>::type)
#endif
) ?
static_cast<ValueType*>(pAny->m_handler(any::storage_operation::GET, pAny, nullptr)) :
nullptr;
}
//Unsafe operations - use with caution
template <class ValueType>
inline const ValueType* unsafe_any_cast(const any* pAny) EASTL_NOEXCEPT
{
return unsafe_any_cast<ValueType>(const_cast<any*>(pAny));
}
template <class ValueType>
inline ValueType* unsafe_any_cast(any* pAny) EASTL_NOEXCEPT
{
return static_cast<ValueType*>(pAny->m_handler(any::storage_operation::GET, pAny, nullptr));
}
//////////////////////////////////////////////////////////////////////////////////////////
// make_any
//
#if EASTL_VARIADIC_TEMPLATES_ENABLED
template <class T, class... Args>
inline any make_any(Args&&... args)
{
return any(eastl::in_place<T>, eastl::forward<Args>(args)...);
}
template <class T, class U, class... Args>
inline any make_any(std::initializer_list<U> il, Args&&... args)
{
return any(eastl::in_place<T>, il, eastl::forward<Args>(args)...);
}
#endif
} // namespace eastl
#endif // EASTL_ANY_H