OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2014 Google Inc. All rights reserved. | 2 * Copyright (C) 2014 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 851 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
862 this->m_raw = nullptr; | 862 this->m_raw = nullptr; |
863 return *this; | 863 return *this; |
864 } | 864 } |
865 | 865 |
866 private: | 866 private: |
867 T** cell() const { return const_cast<T**>(&this->m_raw); } | 867 T** cell() const { return const_cast<T**>(&this->m_raw); } |
868 | 868 |
869 template<typename Derived> friend class VisitorHelper; | 869 template<typename Derived> friend class VisitorHelper; |
870 }; | 870 }; |
871 | 871 |
872 // UnsafePtr is actually a raw pointer. | |
873 // It is allowed to store a pointer to an object on oilpan heap, | |
874 // and it is also allowed to store UnsafePtr in off heap collections. | |
875 // UnsafePtr does not keep the pointee object alive, so if you use | |
876 // UnsafePtr, you must guarantee that the pointee object is alive in | |
877 // some reason. | |
878 template<typename T> | |
879 class UnsafePtr { | |
haraken
2015/10/14 06:00:21
I'd rename UnsafePtr to UnsafeMember or UntracedMe
haraken
2015/10/14 06:00:21
Add conversions between UnsafePtr <=> Member, Pers
peria
2015/10/14 09:36:28
OK. Both names look better than UnsafePtr.
peria
2015/10/14 09:36:28
Done.
| |
880 public: | |
881 UnsafePtr() : m_raw(nullptr) | |
882 { | |
883 } | |
884 | |
885 UnsafePtr(std::nullptr_t) : m_raw(nullptr) | |
886 { | |
887 } | |
888 | |
889 UnsafePtr(T* raw) : m_raw(raw) | |
890 { | |
891 checkPointer(); | |
892 } | |
893 | |
894 template<typename U> | |
895 UnsafePtr(const RawPtr<U>& other) : m_raw(other.get()) | |
896 { | |
897 checkPointer(); | |
898 } | |
899 | |
900 bool operator!() const { return !m_raw; } | |
901 | |
902 operator T*() const { return m_raw; } | |
903 | |
904 T* operator->() const { return m_raw; } | |
905 T& operator*() const { return *m_raw; } | |
906 template<typename U> | |
907 operator RawPtr<U>() const { return m_raw; } | |
908 | |
909 template<typename U> | |
910 UnsafePtr& operator=(const UnsafePtr<U>& other) | |
911 { | |
912 m_raw = other; | |
913 checkPointer(); | |
914 return *this; | |
915 } | |
916 | |
917 template<typename U> | |
918 UnsafePtr& operator=(U* other) | |
919 { | |
920 m_raw = other; | |
921 checkPointer(); | |
922 return *this; | |
923 } | |
924 | |
925 template<typename U> | |
926 UnsafePtr& operator=(RawPtr<U> other) | |
927 { | |
928 m_raw = other; | |
929 checkPointer(); | |
930 return *this; | |
931 } | |
932 | |
933 UnsafePtr& operator=(std::nullptr_t) | |
934 { | |
935 m_raw = nullptr; | |
936 return *this; | |
937 } | |
938 | |
939 void swap(UnsafePtr<T>& other) | |
940 { | |
941 std::swap(m_raw, other.m_raw); | |
942 checkPointer(); | |
943 } | |
944 | |
945 T* get() const { return m_raw; } | |
946 | |
947 void clear() { m_raw = nullptr; } | |
948 | |
949 | |
950 protected: | |
951 void checkPointer() | |
952 { | |
953 #if ENABLE(ASSERT) | |
954 if (!m_raw) | |
955 return; | |
956 | |
957 // TODO(haraken): What we really want to check here is that the pointer | |
958 // is a traceable object. In other words, the pointer is either of: | |
959 // | |
960 // (a) a pointer to the head of an on-heap object. | |
961 // (b) a pointer to the head of an on-heap mixin object. | |
962 // | |
963 // We can check it by calling Heap::isHeapObjectAlive(m_raw), | |
964 // but we cannot call it here because it requires to include T.h. | |
965 // So we currently only try to implement the check for (a), but do | |
966 // not insist that T's definition is in scope. | |
967 if (IsFullyDefined<T>::value && !IsGarbageCollectedMixin<T>::value) | |
968 ASSERT(HeapObjectHeader::fromPayload(m_raw)->checkHeader()); | |
969 #endif | |
970 } | |
971 | |
972 T* m_raw; | |
973 }; | |
974 | |
872 // Comparison operators between (Weak)Members and Persistents | 975 // Comparison operators between (Weak)Members and Persistents |
873 template<typename T, typename U> inline bool operator==(const Member<T>& a, cons t Member<U>& b) { return a.get() == b.get(); } | 976 template<typename T, typename U> inline bool operator==(const Member<T>& a, cons t Member<U>& b) { return a.get() == b.get(); } |
874 template<typename T, typename U> inline bool operator!=(const Member<T>& a, cons t Member<U>& b) { return a.get() != b.get(); } | 977 template<typename T, typename U> inline bool operator!=(const Member<T>& a, cons t Member<U>& b) { return a.get() != b.get(); } |
875 template<typename T, typename U> inline bool operator==(const Member<T>& a, cons t Persistent<U>& b) { return a.get() == b.get(); } | 978 template<typename T, typename U> inline bool operator==(const Member<T>& a, cons t Persistent<U>& b) { return a.get() == b.get(); } |
876 template<typename T, typename U> inline bool operator!=(const Member<T>& a, cons t Persistent<U>& b) { return a.get() != b.get(); } | 979 template<typename T, typename U> inline bool operator!=(const Member<T>& a, cons t Persistent<U>& b) { return a.get() != b.get(); } |
877 template<typename T, typename U> inline bool operator==(const Persistent<T>& a, const Member<U>& b) { return a.get() == b.get(); } | 980 template<typename T, typename U> inline bool operator==(const Persistent<T>& a, const Member<U>& b) { return a.get() == b.get(); } |
878 template<typename T, typename U> inline bool operator!=(const Persistent<T>& a, const Member<U>& b) { return a.get() != b.get(); } | 981 template<typename T, typename U> inline bool operator!=(const Persistent<T>& a, const Member<U>& b) { return a.get() != b.get(); } |
879 template<typename T, typename U> inline bool operator==(const Persistent<T>& a, const Persistent<U>& b) { return a.get() == b.get(); } | 982 template<typename T, typename U> inline bool operator==(const Persistent<T>& a, const Persistent<U>& b) { return a.get() == b.get(); } |
880 template<typename T, typename U> inline bool operator!=(const Persistent<T>& a, const Persistent<U>& b) { return a.get() != b.get(); } | 983 template<typename T, typename U> inline bool operator!=(const Persistent<T>& a, const Persistent<U>& b) { return a.get() != b.get(); } |
881 | 984 |
(...skipping 499 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1381 // TODO(sof): extend WTF::FunctionWrapper call overloading to also handle (C rossThread)WeakPersistent. | 1484 // TODO(sof): extend WTF::FunctionWrapper call overloading to also handle (C rossThread)WeakPersistent. |
1382 static T* unwrap(const StorageType& value) { return value.get(); } | 1485 static T* unwrap(const StorageType& value) { return value.get(); } |
1383 }; | 1486 }; |
1384 | 1487 |
1385 template<typename T> | 1488 template<typename T> |
1386 PassRefPtr<T> adoptRef(blink::RefCountedGarbageCollected<T>*) = delete; | 1489 PassRefPtr<T> adoptRef(blink::RefCountedGarbageCollected<T>*) = delete; |
1387 | 1490 |
1388 } // namespace WTF | 1491 } // namespace WTF |
1389 | 1492 |
1390 #endif | 1493 #endif |
OLD | NEW |