summaryrefslogtreecommitdiff
path: root/runtime/obj_ptr.h
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/obj_ptr.h')
-rw-r--r--runtime/obj_ptr.h26
1 files changed, 22 insertions, 4 deletions
diff --git a/runtime/obj_ptr.h b/runtime/obj_ptr.h
index beb4d33a11..74be44eb23 100644
--- a/runtime/obj_ptr.h
+++ b/runtime/obj_ptr.h
@@ -18,6 +18,7 @@
#define ART_RUNTIME_OBJ_PTR_H_
#include <ostream>
+#include <type_traits>
#include "base/mutex.h" // For Locks::mutator_lock_.
#include "globals.h"
@@ -45,14 +46,23 @@ class ObjPtr {
template <typename Type>
ALWAYS_INLINE ObjPtr(Type* ptr) REQUIRES_SHARED(Locks::mutator_lock_)
- : reference_(Encode(static_cast<MirrorType*>(ptr))) {}
+ : reference_(Encode(static_cast<MirrorType*>(ptr))) {
+ static_assert(std::is_base_of<MirrorType, Type>::value,
+ "Input type must be a subtype of the ObjPtr type");
+ }
template <typename Type>
- ALWAYS_INLINE ObjPtr(const ObjPtr<Type>& other) REQUIRES_SHARED(Locks::mutator_lock_)
- : reference_(Encode(static_cast<MirrorType*>(other.Ptr()))) {}
+ ALWAYS_INLINE ObjPtr(const ObjPtr<Type, kPoison>& other) REQUIRES_SHARED(Locks::mutator_lock_)
+ : reference_(Encode(static_cast<MirrorType*>(other.Ptr()))) {
+ static_assert(std::is_base_of<MirrorType, Type>::value,
+ "Input type must be a subtype of the ObjPtr type");
+ }
template <typename Type>
- ALWAYS_INLINE ObjPtr& operator=(const ObjPtr& other) {
+ ALWAYS_INLINE ObjPtr& operator=(const ObjPtr<Type, kPoison>& other)
+ REQUIRES_SHARED(Locks::mutator_lock_) {
+ static_assert(std::is_base_of<MirrorType, Type>::value,
+ "Input type must be a subtype of the ObjPtr type");
reference_ = Encode(static_cast<MirrorType*>(other.Ptr()));
return *this;
}
@@ -122,6 +132,14 @@ class ObjPtr {
}
}
+ // Static function to be friendly with null pointers.
+ template <typename SourceType>
+ static ObjPtr<MirrorType> DownCast(ObjPtr<SourceType> ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
+ static_assert(std::is_base_of<SourceType, MirrorType>::value,
+ "Target type must be a subtype of source type");
+ return static_cast<MirrorType*>(ptr.Ptr());
+ }
+
private:
// Trim off high bits of thread local cookie.
ALWAYS_INLINE static uintptr_t TrimCookie(uintptr_t cookie) {