-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathshared_ptr.h
1746 lines (1460 loc) · 58.2 KB
/
shared_ptr.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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Electronic Arts Inc. All rights reserved.
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// This class implements the C++11 shared_ptr template. A shared_ptr is like
// the C++ Standard Library unique_ptr except that it allows sharing of pointers
// between instances via reference counting. shared_ptr objects can safely be
// copied and can safely be used in containers such as vector or list.
//
// Notes regarding safe usage of shared_ptr:
// - http://www.boost.org/doc/libs/1_53_0/libs/smart_ptr/shared_ptr.htm#ThreadSafety
// - If you construct a shared_ptr with a raw pointer, you cannot construct another shared_ptr
// with just that raw pointer. Insted you need to construct additional shared_ptrs with
// the originally created shared_ptr. Otherwise you will get a crash.
// - Usage of shared_ptr is thread-safe, but what it points to isn't automatically thread safe.
// Multiple shared_ptrs that refer to the same object can be used arbitrarily by multiple threads.
// - You can use a single shared_ptr between multiple threads in all ways except one: assigment
// to that shared_ptr. The following is not thread-safe, and needs to be guarded by a mutex
// or the shared_ptr atomic functions:
// shared_ptr<Foo> pFoo;
// // Thread 1:
// shared_ptr<Foo> pFoo2 = pFoo;
// // Thread 2:
// pFoo = make_shared<Foo>();
//
// Compatibility note:
// This version of shared_ptr updates the previous version to have full C++11
// compatibility. However, in order to add C++11 compatibility there needed to
// be a few breaking changes which may affect some users. It's likely that most
// or all breaking changes can be rectified by doing the same thing in a slightly
// different way. Here's a list of the primary signficant breaking changes:
// - shared_ptr now takes just one template parameter instead of three.
// (allocator and deleter). You now specify the allocator and deleter
// as part of the shared_ptr constructor at runtime.
// - shared_ptr has thread safety, which
//
///////////////////////////////////////////////////////////////////////////////
#ifndef EASTL_SHARED_PTR_H
#define EASTL_SHARED_PTR_H
#include <eastl/internal/config.h>
#include <eastl/internal/smart_ptr.h>
#include <eastl/internal/thread_support.h>
#include <eastl/unique_ptr.h>
#include <eastl/functional.h>
#include <eastl/allocator.h>
#include <eastl/atomic.h>
#if EASTL_RTTI_ENABLED
#include <typeinfo>
#endif
#if EASTL_EXCEPTIONS_ENABLED
#include <exception>
#endif
EA_DISABLE_ALL_VC_WARNINGS()
#include <new>
#include <stddef.h>
EA_RESTORE_ALL_VC_WARNINGS()
// 4530 - C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
// 4571 - catch(...) semantics changed since Visual C++ 7.1; structured exceptions (SEH) are no longer caught.
// 4512/4626 - 'class' : assignment operator could not be generated. // This disabling would best be put elsewhere.
EA_DISABLE_VC_WARNING(4530 4571 4512 4626);
#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
{
///////////////////////////////////////////////////////////////////////////
// shared_ptr
///////////////////////////////////////////////////////////////////////////
/// EASTL_SHARED_PTR_DEFAULT_NAME
///
/// Defines a default container name in the absence of a user-provided name.
///
#ifndef EASTL_SHARED_PTR_DEFAULT_NAME
#define EASTL_SHARED_PTR_DEFAULT_NAME EASTL_DEFAULT_NAME_PREFIX " shared_ptr" // Unless the user overrides something, this is "EASTL shared_ptr".
#endif
/// EASTL_SHARED_PTR_DEFAULT_ALLOCATOR
///
#ifndef EASTL_SHARED_PTR_DEFAULT_ALLOCATOR
#define EASTL_SHARED_PTR_DEFAULT_ALLOCATOR EASTLAllocatorType(EASTL_SHARED_PTR_DEFAULT_NAME)
#endif
// Forward declarations
template <typename T, typename Deleter> class unique_ptr;
template <typename T> class weak_ptr;
template <typename T> class enable_shared_from_this;
#if EASTL_EXCEPTIONS_ENABLED
// We define eastl::bad_weak_ptr as opposed to std::bad_weak_ptr. The reason is that
// we can't easily know of std::bad_weak_ptr exists and we would have to #include <memory>
// to use it. EASTL "owns" the types that are defined in EASTL headers, and std::bad_weak_ptr
// is declared in <memory>.
struct bad_weak_ptr : std::exception
{
const char* what() const EASTL_NOEXCEPT EASTL_OVERRIDE
{ return "bad weak_ptr"; }
};
#endif
/// ref_count_sp
///
/// This is a small utility class used by shared_ptr and weak_ptr.
struct ref_count_sp
{
atomic<int32_t> mRefCount; /// Reference count on the contained pointer. Starts as 1 by default.
atomic<int32_t> mWeakRefCount; /// Reference count on contained pointer plus this ref_count_sp object itself. Starts as 1 by default.
public:
ref_count_sp(int32_t refCount = 1, int32_t weakRefCount = 1) EASTL_NOEXCEPT;
virtual ~ref_count_sp() EASTL_NOEXCEPT {}
int32_t use_count() const EASTL_NOEXCEPT;
void addref() EASTL_NOEXCEPT;
void release();
void weak_addref() EASTL_NOEXCEPT;
void weak_release();
ref_count_sp* lock() EASTL_NOEXCEPT;
virtual void free_value() EASTL_NOEXCEPT = 0; // Release the contained object.
virtual void free_ref_count_sp() EASTL_NOEXCEPT = 0; // Release this instance.
#if EASTL_RTTI_ENABLED
virtual void* get_deleter(const std::type_info& type) const EASTL_NOEXCEPT = 0;
#else
virtual void* get_deleter() const EASTL_NOEXCEPT = 0;
#endif
};
inline ref_count_sp::ref_count_sp(int32_t refCount, int32_t weakRefCount) EASTL_NOEXCEPT
: mRefCount(refCount), mWeakRefCount(weakRefCount) {}
inline int32_t ref_count_sp::use_count() const EASTL_NOEXCEPT
{
return mRefCount.load(memory_order_relaxed); // To figure out: is this right?
}
inline void ref_count_sp::addref() EASTL_NOEXCEPT
{
mRefCount.fetch_add(1, memory_order_relaxed);
mWeakRefCount.fetch_add(1, memory_order_relaxed);
}
inline void ref_count_sp::release()
{
EASTL_ASSERT((mRefCount.load(memory_order_relaxed) > 0));
if(mRefCount.fetch_sub(1, memory_order_release) == 1)
{
atomic_thread_fence(memory_order_acquire);
free_value();
}
weak_release();
}
inline void ref_count_sp::weak_addref() EASTL_NOEXCEPT
{
mWeakRefCount.fetch_add(1, memory_order_relaxed);
}
inline void ref_count_sp::weak_release()
{
EASTL_ASSERT(mWeakRefCount.load(memory_order_relaxed) > 0);
if(mWeakRefCount.fetch_sub(1, memory_order_release) == 1)
{
atomic_thread_fence(memory_order_acquire);
free_ref_count_sp();
}
}
inline ref_count_sp* ref_count_sp::lock() EASTL_NOEXCEPT
{
for(int32_t refCountTemp = mRefCount.load(memory_order_relaxed); refCountTemp != 0; )
{
if(mRefCount.compare_exchange_weak(refCountTemp, refCountTemp + 1, memory_order_relaxed))
{
mWeakRefCount.fetch_add(1, memory_order_relaxed);
return this;
}
}
return nullptr;
}
/// ref_count_sp_t
///
/// This is a version of ref_count_sp which is used to delete the contained pointer.
template <typename T, typename Allocator, typename Deleter>
class ref_count_sp_t : public ref_count_sp
{
public:
typedef ref_count_sp_t<T, Allocator, Deleter> this_type;
typedef T value_type;
typedef Allocator allocator_type;
typedef Deleter deleter_type;
value_type mValue; // This is expected to be a pointer.
deleter_type mDeleter;
allocator_type mAllocator;
ref_count_sp_t(value_type value, deleter_type deleter, allocator_type allocator)
: ref_count_sp(), mValue(value), mDeleter(eastl::move(deleter)), mAllocator(eastl::move(allocator))
{}
void free_value() EASTL_NOEXCEPT
{
mDeleter(mValue);
mValue = nullptr;
}
void free_ref_count_sp() EASTL_NOEXCEPT
{
allocator_type allocator = mAllocator;
this->~ref_count_sp_t();
EASTLFree(allocator, this, sizeof(*this));
}
#if EASTL_RTTI_ENABLED
void* get_deleter(const std::type_info& type) const EASTL_NOEXCEPT
{
return (type == typeid(deleter_type)) ? (void*)&mDeleter : nullptr;
}
#else
void* get_deleter() const EASTL_NOEXCEPT
{
return (void*)&mDeleter;
}
#endif
};
/// ref_count_sp_t_inst
///
/// This is a version of ref_count_sp which is used to actually hold an instance of
/// T (instead of a pointer). This is useful to allocate the object and ref count
/// in a single memory allocation.
template<typename T, typename Allocator>
class ref_count_sp_t_inst : public ref_count_sp
{
public:
typedef ref_count_sp_t_inst<T, Allocator> this_type;
typedef T value_type;
typedef Allocator allocator_type;
typedef typename aligned_storage<sizeof(T), eastl::alignment_of<T>::value>::type storage_type;
storage_type mMemory;
allocator_type mAllocator;
value_type* GetValue() { return static_cast<value_type*>(static_cast<void*>(&mMemory)); }
template <typename... Args>
ref_count_sp_t_inst(allocator_type allocator, Args&&... args)
: ref_count_sp(), mAllocator(eastl::move(allocator))
{
new (&mMemory) value_type(eastl::forward<Args>(args)...);
}
void free_value() EASTL_NOEXCEPT
{
GetValue()->~value_type();
}
void free_ref_count_sp() EASTL_NOEXCEPT
{
allocator_type allocator = mAllocator;
this->~ref_count_sp_t_inst();
EASTLFree(allocator, this, sizeof(*this));
}
#if EASTL_RTTI_ENABLED
void* get_deleter(const std::type_info&) const EASTL_NOEXCEPT
{
return nullptr; // Default base implementation.
}
#else
void* get_deleter() const EASTL_NOEXCEPT
{
return nullptr;
}
#endif
};
/// do_enable_shared_from_this
///
/// If a user calls this function, it sets up mWeakPtr member of
/// the enable_shared_from_this parameter to point to the ref_count_sp
/// object that's passed in. Normally, the user doesn't need to call
/// this function, as the shared_ptr constructor will do it for them.
///
template <typename T, typename U>
void do_enable_shared_from_this(const ref_count_sp* pRefCount,
const enable_shared_from_this<T>* pEnableSharedFromThis,
const U* pValue)
{
if (pEnableSharedFromThis)
pEnableSharedFromThis->mWeakPtr.assign(const_cast<U*>(pValue), const_cast<ref_count_sp*>(pRefCount));
}
inline void do_enable_shared_from_this(const ref_count_sp*, ...) {} // Empty specialization. This no-op version is
// called by shared_ptr when shared_ptr's T type
// is anything but an enabled_shared_from_this
// class.
/// shared_ptr_traits
/// This exists for the sole purpose of creating a typedef called
/// reference_type which is specialized for type void. The reason
/// for this is that shared_ptr::operator*() returns a reference
/// to T but if T is void, it needs to return void, not *void,
/// as the latter is not valid C++.
template <typename T> struct shared_ptr_traits
{ typedef T& reference_type; };
template <> struct shared_ptr_traits<void>
{ typedef void reference_type; };
template <> struct shared_ptr_traits<void const>
{ typedef void reference_type; };
template <> struct shared_ptr_traits<void volatile>
{ typedef void reference_type; };
template <> struct shared_ptr_traits<void const volatile>
{ typedef void reference_type; };
/// shared_ptr
///
/// This class implements the C++11 shared_ptr template. A shared_ptr is like the C++
/// Standard Library unique_ptr except that it allows sharing of pointers between
/// instances via reference counting. shared_ptr objects can safely be copied and
/// can safely be used in C++ Standard Library containers such as std::vector or
/// std::list.
///
/// This class is not thread safe in that you cannot use an instance of it from
/// two threads at the same time and cannot use two separate instances of it, which
/// own the same pointer, at the same time. Use standard multithread mutex techniques
/// to address the former problems and use shared_ptr_mt to address the latter.
/// Note that this is contrary to the C++11 standard.
///
/// As of this writing, arrays aren't supported, but they are planned in the future
/// based on the C++17 proposal: http://isocpp.org/files/papers/N3920.html
///
template <typename T>
class shared_ptr
{
public:
typedef shared_ptr<T> this_type;
typedef T element_type;
typedef typename shared_ptr_traits<T>::reference_type reference_type; // This defines what a reference to a T is. It's always simply T&, except for the case where T is void, whereby the reference is also just void.
typedef EASTLAllocatorType default_allocator_type;
typedef default_delete<T> default_deleter_type;
typedef weak_ptr<T> weak_type;
protected:
element_type* mpValue;
ref_count_sp* mpRefCount; /// Base pointer to Reference count for owned pointer and the owned pointer.
public:
/// Initializes and "empty" shared_ptr.
/// Postcondition: use_count() == zero and get() == 0
shared_ptr() EASTL_NOEXCEPT
: mpValue(nullptr),
mpRefCount(nullptr)
{
// Intentionally leaving mpRefCount as NULL. Can't allocate here due to noexcept.
}
/// Takes ownership of the pointer and sets the reference count
/// to the pointer to 1. It is OK if the input pointer is null.
/// The shared reference count is allocated on the heap using the
/// default eastl allocator.
/// Throws: bad_alloc, or an implementation-defined exception when
/// a resource other than memory could not be obtained.
/// Exception safety: If an exception is thrown, delete p is called.
/// Postcondition in the event of no exception: use_count() == 1 && get() == p
template <typename U>
explicit shared_ptr(U* pValue,
typename eastl::enable_if<eastl::is_convertible<U*, element_type*>::value>::type* = 0)
: mpValue(nullptr), mpRefCount(nullptr) // alloc_internal will set this.
{
// We explicitly use default_delete<U>. You can use the other version of this constructor to provide a
// custom version.
alloc_internal(pValue, default_allocator_type(),
default_delete<U>()); // Problem: We want to be able to use default_deleter_type() instead of
// default_delete<U>, but if default_deleter_type's type is void or
// otherwise mismatched then this will fail to compile. What we really
// want to be able to do is "rebind" default_allocator_type to U
// instead of its original type.
}
shared_ptr(std::nullptr_t) EASTL_NOEXCEPT
: mpValue(nullptr),
mpRefCount(nullptr)
{
// Intentionally leaving mpRefCount as NULL. Can't allocate here due to noexcept.
}
/// Takes ownership of the pointer and sets the reference count
/// to the pointer to 1. It is OK if the input pointer is null.
/// The shared reference count is allocated on the heap using the
/// default eastl allocator. The pointer will be disposed using the
/// provided deleter.
/// If an exception occurs during the allocation of the shared
/// reference count, the owned pointer is deleted and the exception
/// is rethrown.
/// Postcondition: use_count() == 1 && get() == p
template <typename U, typename Deleter>
shared_ptr(U* pValue,
Deleter deleter,
typename eastl::enable_if<eastl::is_convertible<U*, element_type*>::value>::type* = 0)
: mpValue(nullptr), mpRefCount(nullptr)
{
alloc_internal(pValue, default_allocator_type(), eastl::move(deleter));
}
template <typename Deleter>
shared_ptr(std::nullptr_t, Deleter deleter)
: mpValue(nullptr), mpRefCount(nullptr) // alloc_internal will set this.
{
alloc_internal(nullptr, default_allocator_type(), eastl::move(deleter));
}
/// Takes ownership of the pointer and sets the reference count
/// to the pointer to 1. It is OK if the input pointer is null.
/// The shared reference count is allocated on the heap using the
/// supplied allocator. The pointer will be disposed using the
/// provided deleter.
/// If an exception occurs during the allocation of the shared
/// reference count, the owned pointer is deleted and the exception
/// is rethrown.
/// Postcondition: use_count() == 1 && get() == p
template <typename U, typename Deleter, typename Allocator>
explicit shared_ptr(U* pValue,
Deleter deleter,
const Allocator& allocator,
typename eastl::enable_if<eastl::is_convertible<U*, element_type*>::value>::type* = 0)
: mpValue(nullptr), mpRefCount(nullptr) // alloc_internal will set this.
{
alloc_internal(pValue, eastl::move(allocator), eastl::move(deleter));
}
template <typename Deleter, typename Allocator>
shared_ptr(std::nullptr_t, Deleter deleter, Allocator allocator)
: mpValue(nullptr),
mpRefCount(nullptr) // alloc_internal will set this.
{
alloc_internal(nullptr, eastl::move(allocator), eastl::move(deleter));
}
/// shared_ptr
/// construction with self type.
/// If we want a shared_ptr constructor that is templated on shared_ptr<U>,
/// then we need to make it in addition to this function, as otherwise
/// the compiler will generate this function and things will go wrong.
/// To accomplish this in a thread-safe way requires use of shared_ptr atomic_store.
shared_ptr(const shared_ptr& sharedPtr) EASTL_NOEXCEPT
: mpValue(sharedPtr.mpValue),
mpRefCount(sharedPtr.mpRefCount)
{
if(mpRefCount)
mpRefCount->addref();
}
/// shared_ptr
/// Shares ownership of a pointer with another instance of shared_ptr.
/// This function increments the shared reference count on the pointer.
/// To accomplish this in a thread-safe way requires use of shared_ptr atomic_store.
template <typename U>
shared_ptr(const shared_ptr<U>& sharedPtr,
typename eastl::enable_if<eastl::is_convertible<U*, element_type*>::value>::type* = 0) EASTL_NOEXCEPT
: mpValue(sharedPtr.mpValue),
mpRefCount(sharedPtr.mpRefCount)
{
if (mpRefCount)
mpRefCount->addref();
}
/// shared_ptr
///
/// 20.7.2.2.1p13: Constructs a shared_ptr instance that stores p and shares ownership with r.
/// Postconditions: get() == pValue && use_count() == sharedPtr.use_count().
/// To avoid the possibility of a dangling pointer, the user of this constructor must
/// ensure that pValue remains valid at least until the ownership group of sharedPtr is destroyed.
/// This constructor allows creation of an empty shared_ptr instance with a non-NULL stored pointer.
///
/// Shares ownership of a pointer with another instance of shared_ptr while storing a potentially
/// different pointer. This function increments the shared reference count on the sharedPtr if it exists.
/// If sharedPtr has no shared reference then a shared reference is not created an pValue is not
/// deleted in our destructor and effectively the pointer is not actually shared.
///
/// To accomplish this in a thread-safe way requires the user to maintain the lifetime of sharedPtr
/// as described above.
///
template <typename U>
shared_ptr(const shared_ptr<U>& sharedPtr, element_type* pValue) EASTL_NOEXCEPT
: mpValue(pValue),
mpRefCount(sharedPtr.mpRefCount)
{
if(mpRefCount)
mpRefCount->addref();
}
shared_ptr(shared_ptr&& sharedPtr) EASTL_NOEXCEPT
: mpValue(sharedPtr.mpValue),
mpRefCount(sharedPtr.mpRefCount)
{
sharedPtr.mpValue = nullptr;
sharedPtr.mpRefCount = nullptr;
}
template <typename U>
shared_ptr(shared_ptr<U>&& sharedPtr,
typename eastl::enable_if<eastl::is_convertible<U*, element_type*>::value>::type* = 0) EASTL_NOEXCEPT
: mpValue(sharedPtr.mpValue),
mpRefCount(sharedPtr.mpRefCount)
{
sharedPtr.mpValue = nullptr;
sharedPtr.mpRefCount = nullptr;
}
// unique_ptr constructor
template <typename U, typename Deleter>
shared_ptr(unique_ptr<U, Deleter>&& uniquePtr,
typename eastl::enable_if<!eastl::is_array<U>::value && !is_lvalue_reference<Deleter>::value &&
eastl::is_convertible<U*, element_type*>::value>::type* = 0)
: mpValue(nullptr), mpRefCount(nullptr)
{
alloc_internal(uniquePtr.release(), default_allocator_type(), uniquePtr.get_deleter());
}
// unique_ptr constructor
// The following is not in the C++11 Standard.
template <typename U, typename Deleter, typename Allocator>
shared_ptr(unique_ptr<U, Deleter>&& uniquePtr,
const Allocator& allocator,
typename eastl::enable_if<!eastl::is_array<U>::value && !is_lvalue_reference<Deleter>::value &&
eastl::is_convertible<U*, element_type*>::value>::type* = 0)
: mpValue(nullptr), mpRefCount(nullptr)
{
alloc_internal(uniquePtr.release(), allocator, uniquePtr.get_deleter());
}
/// shared_ptr(weak_ptr)
/// Shares ownership of a pointer with an instance of weak_ptr.
/// This function increments the shared reference count on the pointer.
template <typename U>
explicit shared_ptr(const weak_ptr<U>& weakPtr,
typename eastl::enable_if<eastl::is_convertible<U*, element_type*>::value>::type* = 0)
: mpValue(weakPtr.mpValue)
, mpRefCount(weakPtr.mpRefCount ?
weakPtr.mpRefCount->lock() :
weakPtr.mpRefCount) // mpRefCount->lock() addref's the return value for us.
{
if (!mpRefCount)
{
mpValue = nullptr; // Question: Is it right for us to NULL this or not?
#if EASTL_EXCEPTIONS_ENABLED
throw eastl::bad_weak_ptr();
#else
EASTL_FAIL_MSG("eastl::shared_ptr -- bad_weak_ptr");
#endif
}
}
/// ~shared_ptr
/// Decrements the reference count for the owned pointer. If the
/// reference count goes to zero, the owned pointer is deleted and
/// the shared reference count is deleted.
~shared_ptr()
{
if (mpRefCount)
{
mpRefCount->release();
}
// else if mpValue is non-NULL then we just lose it because it wasn't actually shared (can happen with
// shared_ptr(const shared_ptr<U>& sharedPtr, element_type* pValue) constructor).
#if EASTL_DEBUG
mpValue = nullptr;
mpRefCount = nullptr;
#endif
}
// The following is disabled because it is not specified by the C++11 Standard, as it leads to
// potential collisions. Use the reset(p) and reset() functions instead.
//
// template <typename U>
// typename eastl::enable_if<eastl::is_convertible<U*, element_type*>::value, this_type&>::type
// operator=(const U* pValue) EASTL_NOEXCEPT
// {
// reset(pValue);
// return *this;
// }
//
// template <typename U>
// this_type& operator=(std::nullptr_t) EASTL_NOEXCEPT
// {
// reset();
// return *this;
// }
/// operator=
/// Assignment to self type.
/// If we want a shared_ptr operator= that is templated on shared_ptr<U>,
/// then we need to make it in addition to this function, as otherwise
/// the compiler will generate this function and things will go wrong.
shared_ptr& operator=(const shared_ptr& sharedPtr) EASTL_NOEXCEPT
{
if(&sharedPtr != this)
this_type(sharedPtr).swap(*this);
return *this;
}
/// operator=
/// Copies another shared_ptr to this object. Note that this object
/// may already own a shared pointer with another different pointer
/// (but still of the same type) before this call. In that case,
/// this function releases the old pointer, decrementing its reference
/// count and deleting it if zero, takes shared ownership of the new
/// pointer and increments its reference count.
template <typename U>
typename eastl::enable_if<eastl::is_convertible<U*, element_type*>::value, this_type&>::type
operator=(const shared_ptr<U>& sharedPtr) EASTL_NOEXCEPT
{
if(!equivalent_ownership(sharedPtr))
this_type(sharedPtr).swap(*this);
return *this;
}
/// operator=
/// Assignment to self type.
/// If we want a shared_ptr operator= that is templated on shared_ptr<U>,
/// then we need to make it in addition to this function, as otherwise
/// the compiler will generate this function and things will go wrong.
this_type& operator=(shared_ptr&& sharedPtr) EASTL_NOEXCEPT
{
if(&sharedPtr != this)
this_type(eastl::move(sharedPtr)).swap(*this);
return *this;
}
/// operator=
/// Moves another shared_ptr to this object. Note that this object
/// may already own a shared pointer with another different pointer
/// (but still of the same type) before this call. In that case,
/// this function releases the old pointer, decrementing its reference
/// count and deleting it if zero, takes shared ownership of the new
/// pointer and increments its reference count.
template <typename U>
typename eastl::enable_if<eastl::is_convertible<U*, element_type*>::value, this_type&>::type
operator=(shared_ptr<U>&& sharedPtr) EASTL_NOEXCEPT
{
if(!equivalent_ownership(sharedPtr))
shared_ptr(eastl::move(sharedPtr)).swap(*this);
return *this;
}
// unique_ptr operator=
template <typename U, typename Deleter>
typename eastl::enable_if<!eastl::is_array<U>::value && eastl::is_convertible<U*, element_type*>::value, this_type&>::type
operator=(unique_ptr<U, Deleter>&& uniquePtr)
{
// Note that this will use the default EASTL allocator
this_type(eastl::move(uniquePtr)).swap(*this);
return *this;
}
/// reset
/// Releases the owned pointer.
void reset() EASTL_NOEXCEPT
{
this_type().swap(*this);
}
/// reset
/// Releases the owned pointer and takes ownership of the
/// passed in pointer.
template <typename U>
typename eastl::enable_if<eastl::is_convertible<U*, element_type*>::value, void>::type
reset(U* pValue)
{
this_type(pValue).swap(*this);
}
/// reset
/// Releases the owned pointer and takes ownership of the
/// passed in pointer.
template <typename U, typename Deleter>
typename eastl::enable_if<eastl::is_convertible<U*, element_type*>::value, void>::type
reset(U* pValue, Deleter deleter)
{
shared_ptr(pValue, deleter).swap(*this);
}
/// reset
/// Resets the shared_ptr
template <typename U, typename Deleter, typename Allocator>
typename eastl::enable_if<eastl::is_convertible<U*, element_type*>::value, void>::type
reset(U* pValue, Deleter deleter, const Allocator& allocator)
{
shared_ptr(pValue, deleter, allocator).swap(*this);
}
/// swap
/// Exchanges the owned pointer between two shared_ptr objects.
/// This function is not intrinsically thread-safe. You must use atomic_exchange(shared_ptr<T>*, shared_ptr<T>)
/// or manually coordinate the swap.
void swap(this_type& sharedPtr) EASTL_NOEXCEPT
{
element_type* const pValue = sharedPtr.mpValue;
sharedPtr.mpValue = mpValue;
mpValue = pValue;
ref_count_sp* const pRefCount = sharedPtr.mpRefCount;
sharedPtr.mpRefCount = mpRefCount;
mpRefCount = pRefCount;
}
/// operator*
/// Returns the owner pointer dereferenced.
/// Example usage:
/// shared_ptr<int> ptr(new int(3));
/// int x = *ptr;
reference_type operator*() const EASTL_NOEXCEPT
{
return *mpValue;
}
/// operator->
/// Allows access to the owned pointer via operator->()
/// Example usage:
/// struct X{ void DoSomething(); };
/// shared_ptr<int> ptr(new X);
/// ptr->DoSomething();
element_type* operator->() const EASTL_NOEXCEPT
{
// assert(mpValue);
return mpValue;
}
/// operator[]
/// Index into the array pointed to by the owned pointer.
/// The behaviour is undefined if the owned pointer is nullptr, if the user specified index is negative, or if
/// the index is outside the referred array bounds.
///
/// When T is not an array type, it is unspecified whether this function is declared. If the function is declared,
/// it is unspecified what its return type is, except that the declaration (although not necessarily the
/// definition) of the function is guaranteed to be legal.
//
// TODO(rparolin): This is disabled because eastl::shared_ptr needs array support.
// element_type& operator[](ptrdiff_t idx)
// {
// return get()[idx];
// }
/// get
/// Returns the owned pointer. Note that this class does
/// not provide an operator T() function. This is because such
/// a thing (automatic conversion) is deemed unsafe.
/// Example usage:
/// struct X{ void DoSomething(); };
/// shared_ptr<int> ptr(new X);
/// X* pX = ptr.get();
/// pX->DoSomething();
element_type* get() const EASTL_NOEXCEPT
{
return mpValue;
}
/// use_count
/// Returns: the number of shared_ptr objects, *this included, that share ownership with *this, or 0 when *this is empty.
int use_count() const EASTL_NOEXCEPT
{
return mpRefCount ? mpRefCount->use_count() : 0;
}
/// unique
/// Returns: use_count() == 1.
bool unique() const EASTL_NOEXCEPT
{
return (mpRefCount && (mpRefCount->use_count() == 1));
}
/// owner_before
/// C++11 function for ordering.
template <typename U>
bool owner_before(const shared_ptr<U>& sharedPtr) const EASTL_NOEXCEPT
{
return (mpRefCount < sharedPtr.mpRefCount);
}
template <typename U>
bool owner_before(const weak_ptr<U>& weakPtr) const EASTL_NOEXCEPT
{
return (mpRefCount < weakPtr.mpRefCount);
}
template <typename Deleter>
Deleter* get_deleter() const EASTL_NOEXCEPT
{
#if EASTL_RTTI_ENABLED
return mpRefCount ? static_cast<Deleter*>(mpRefCount->get_deleter(typeid(typename remove_cv<Deleter>::type))) : nullptr;
#else
// This is probably unsafe but without typeid there is no way to ensure that the
// stored deleter is actually of the templated Deleter type.
return nullptr;
// Alternatively:
// return mpRefCount ? static_cast<Deleter*>(mpRefCount->get_deleter()) : nullptr;
#endif
}
#ifdef EA_COMPILER_NO_EXPLICIT_CONVERSION_OPERATORS
/// Note that below we do not use operator bool(). The reason for this
/// is that booleans automatically convert up to short, int, float, etc.
/// The result is that this: if(sharedPtr == 1) would yield true (bad).
typedef T* (this_type::*bool_)() const;
operator bool_() const EASTL_NOEXCEPT
{
if(mpValue)
return &this_type::get;
return nullptr;
}
bool operator!() const EASTL_NOEXCEPT
{
return (mpValue == nullptr);
}
#else
/// Explicit operator bool
/// Allows for using a shared_ptr as a boolean.
/// Example usage:
/// shared_ptr<int> ptr(new int(3));
/// if(ptr)
/// ++*ptr;
explicit operator bool() const EASTL_NOEXCEPT
{
return (mpValue != nullptr);
}
#endif
/// Returns true if the given shared_ptr ows the same T pointer that we do.
template <typename U>
bool equivalent_ownership(const shared_ptr<U>& sharedPtr) const
{
// We compare mpRefCount instead of mpValue, because it's feasible that there are two sets of shared_ptr
// objects that are unconnected to each other but happen to own the same value pointer.
return (mpRefCount == sharedPtr.mpRefCount);
}
protected:
// Friend declarations.
template <typename U> friend class shared_ptr;
template <typename U> friend class weak_ptr;
template <typename U> friend void allocate_shared_helper(shared_ptr<U>&, ref_count_sp*, U*);
// Handles the allocating of mpRefCount, while assigning mpValue.
// The provided pValue may be NULL, as with constructing with a deleter and allocator but NULL pointer.
template <typename U, typename Allocator, typename Deleter>
void alloc_internal(U pValue, Allocator allocator, Deleter deleter)
{
typedef ref_count_sp_t<U, Allocator, Deleter> ref_count_type;
#if EASTL_EXCEPTIONS_ENABLED
try
{
void* const pMemory = EASTLAlloc(allocator, sizeof(ref_count_type));
if(!pMemory)
throw std::bad_alloc();
mpRefCount = ::new(pMemory) ref_count_type(pValue, eastl::move(deleter), eastl::move(allocator));
mpValue = pValue;
do_enable_shared_from_this(mpRefCount, pValue, pValue);
}
catch(...) // The exception would usually be std::bad_alloc.
{
deleter(pValue); // 20.7.2.2.1 p7: If an exception is thrown, delete p is called.
throw; // Throws: bad_alloc, or an implementation-defined exception when a resource other than memory could not be obtained.
}
#else
void* const pMemory = EASTLAlloc(allocator, sizeof(ref_count_type));
if(pMemory)
{
mpRefCount = ::new(pMemory) ref_count_type(pValue, eastl::move(deleter), eastl::move(allocator));
mpValue = pValue;
do_enable_shared_from_this(mpRefCount, pValue, pValue);
}
else
{
deleter(pValue); // We act the same as we do above with exceptions enabled.
}
#endif
}
}; // class shared_ptr
/// get_pointer
/// returns shared_ptr::get() via the input shared_ptr.
template <typename T>
inline typename shared_ptr<T>::element_type* get_pointer(const shared_ptr<T>& sharedPtr) EASTL_NOEXCEPT
{
return sharedPtr.get();
}
/// get_deleter
/// returns the deleter in the input shared_ptr.
template <typename Deleter, typename T>
Deleter* get_deleter(const shared_ptr<T>& sharedPtr) EASTL_NOEXCEPT
{
return sharedPtr.template get_deleter<Deleter>();
}
/// swap
/// Exchanges the owned pointer beween two shared_ptr objects.
/// This non-member version is useful for compatibility of shared_ptr
/// objects with the C++ Standard Library and other libraries.
template <typename T>
inline void swap(shared_ptr<T>& a, shared_ptr<T>& b) EASTL_NOEXCEPT
{
a.swap(b);
}
/// shared_ptr comparison operators
template <typename T, typename U>
inline bool operator==(const shared_ptr<T>& a, const shared_ptr<U>& b) EASTL_NOEXCEPT
{
// assert((a.get() != b.get()) || (a.use_count() == b.use_count()));
return (a.get() == b.get());
}
#if defined(EA_COMPILER_HAS_THREE_WAY_COMPARISON)
template <typename T, typename U>
std::strong_ordering operator<=>(const shared_ptr<T>& a, const shared_ptr<U>& b) EASTL_NOEXCEPT
{
return a.get() <=> b.get();
}
#else
template <typename T, typename U>
inline bool operator!=(const shared_ptr<T>& a, const shared_ptr<U>& b) EASTL_NOEXCEPT
{
// assert((a.get() != b.get()) || (a.use_count() == b.use_count()));
return (a.get() != b.get());
}
template <typename T, typename U>
inline bool operator<(const shared_ptr<T>& a, const shared_ptr<U>& b) EASTL_NOEXCEPT
{
//typedef typename eastl::common_type<T*, U*>::type CPointer;
//return less<CPointer>()(a.get(), b.get());