aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTreeHugger Robot <treehugger-gerrit@google.com>2017-08-30 01:53:07 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2017-08-30 01:53:07 +0000
commit6966f60eba095d15e409b7c8c80dda46b6aa6ae4 (patch)
tree88dabdd321e93cb01577c31f9fd6d38302ed8d9c
parent2e4298afead1e8f6aa51f6a453ace3edee1e4165 (diff)
parentc67f6f671f586d57f5c836fadc163ad562c75c55 (diff)
downloadchre-6966f60eba095d15e409b7c8c80dda46b6aa6ae4.tar.gz
Merge "Adds a conversion constructor to UniquePtr" into oc-mr1-dev
-rw-r--r--util/include/chre/util/unique_ptr.h15
-rw-r--r--util/include/chre/util/unique_ptr_impl.h7
2 files changed, 22 insertions, 0 deletions
diff --git a/util/include/chre/util/unique_ptr.h b/util/include/chre/util/unique_ptr.h
index 3fca47eb..8af2e9a0 100644
--- a/util/include/chre/util/unique_ptr.h
+++ b/util/include/chre/util/unique_ptr.h
@@ -52,6 +52,16 @@ class UniquePtr : public NonCopyable {
UniquePtr(UniquePtr<ObjectType>&& other);
/**
+ * Constructs a new UniquePtr via moving the Object from another UniquePtr.
+ * This constructor allows conversion (ie: upcast) to another type if
+ * possible.
+ *
+ * @param other UniquePtr instance to move and convert into this object.
+ */
+ template<typename OtherObjectType>
+ UniquePtr(UniquePtr<OtherObjectType>&& other);
+
+ /**
* Deconstructs the object (if necessary) and releases associated memory.
*/
~UniquePtr();
@@ -105,6 +115,11 @@ class UniquePtr : public NonCopyable {
UniquePtr<ObjectType>& operator=(UniquePtr<ObjectType>&& other);
private:
+ // Befriend this class to itself to allow the templated conversion constructor
+ // permission to access mObject below.
+ template<typename OtherObjectType>
+ friend class UniquePtr;
+
//! A pointer to the underlying storage for this object.
ObjectType *mObject;
};
diff --git a/util/include/chre/util/unique_ptr_impl.h b/util/include/chre/util/unique_ptr_impl.h
index b1cc41a2..69be9d0c 100644
--- a/util/include/chre/util/unique_ptr_impl.h
+++ b/util/include/chre/util/unique_ptr_impl.h
@@ -38,6 +38,13 @@ UniquePtr<ObjectType>::UniquePtr(UniquePtr<ObjectType>&& other) {
}
template<typename ObjectType>
+template<typename OtherObjectType>
+UniquePtr<ObjectType>::UniquePtr(UniquePtr<OtherObjectType>&& other) {
+ mObject = other.mObject;
+ other.mObject = nullptr;
+}
+
+template<typename ObjectType>
UniquePtr<ObjectType>::~UniquePtr() {
if (mObject != nullptr) {
mObject->~ObjectType();