Index: mojo/public/cpp/bindings/struct_ptr.h |
diff --git a/mojo/public/cpp/bindings/struct_ptr.h b/mojo/public/cpp/bindings/struct_ptr.h |
index dbc3256ea782eed9d1950fee35552c77ceb347d4..a01c42f9d05e689ba3e193242b254cdeebed9fe6 100644 |
--- a/mojo/public/cpp/bindings/struct_ptr.h |
+++ b/mojo/public/cpp/bindings/struct_ptr.h |
@@ -6,10 +6,12 @@ |
#define MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_ |
#include <functional> |
+#include <memory> |
#include <new> |
#include "base/logging.h" |
#include "base/macros.h" |
+#include "base/optional.h" |
#include "mojo/public/cpp/bindings/lib/hash_util.h" |
#include "mojo/public/cpp/bindings/type_converter.h" |
@@ -19,15 +21,6 @@ namespace internal { |
constexpr size_t kHashSeed = 31; |
template <typename Struct> |
-class StructHelper { |
- public: |
- template <typename Ptr> |
- static void Initialize(Ptr* ptr) { |
- ptr->Initialize(); |
- } |
-}; |
- |
-template <typename Struct> |
class StructPtrWTFHelper; |
template <typename Struct> |
@@ -41,35 +34,34 @@ class StructPtr { |
public: |
using Struct = S; |
- StructPtr() : ptr_(nullptr) {} |
- StructPtr(decltype(nullptr)) : ptr_(nullptr) {} |
+ StructPtr() = default; |
+ StructPtr(decltype(nullptr)) {} |
- ~StructPtr() { delete ptr_; } |
+ ~StructPtr() = default; |
StructPtr& operator=(decltype(nullptr)) { |
reset(); |
return *this; |
} |
- StructPtr(StructPtr&& other) : ptr_(nullptr) { Take(&other); } |
+ StructPtr(StructPtr&& other) { Take(&other); } |
StructPtr& operator=(StructPtr&& other) { |
Take(&other); |
return *this; |
} |
+ template <typename... Args> |
+ StructPtr(base::in_place_t, Args&&... args) |
+ : ptr_(new Struct(std::forward<Args>(args)...)) {} |
+ |
template <typename U> |
U To() const { |
return TypeConverter<U, StructPtr>::Convert(*this); |
} |
- void reset() { |
- if (ptr_) { |
- delete ptr_; |
- ptr_ = nullptr; |
- } |
- } |
+ void reset() { ptr_.reset(); } |
- bool is_null() const { return ptr_ == nullptr; } |
+ bool is_null() const { return !ptr_; } |
Struct& operator*() const { |
DCHECK(ptr_); |
@@ -77,9 +69,9 @@ class StructPtr { |
} |
Struct* operator->() const { |
DCHECK(ptr_); |
- return ptr_; |
+ return ptr_.get(); |
} |
- Struct* get() const { return ptr_; } |
+ Struct* get() const { return ptr_.get(); } |
void Swap(StructPtr* other) { std::swap(ptr_, other->ptr_); } |
@@ -106,20 +98,13 @@ class StructPtr { |
explicit operator bool() const { return !is_null(); } |
private: |
- friend class internal::StructHelper<Struct>; |
friend class internal::StructPtrWTFHelper<Struct>; |
- |
- void Initialize() { |
- DCHECK(!ptr_); |
- ptr_ = new Struct(); |
- } |
- |
void Take(StructPtr* other) { |
reset(); |
Swap(other); |
} |
- Struct* ptr_; |
+ std::unique_ptr<Struct> ptr_; |
DISALLOW_COPY_AND_ASSIGN(StructPtr); |
}; |
@@ -155,6 +140,10 @@ class InlinedStructPtr { |
return *this; |
} |
+ template <typename... Args> |
+ InlinedStructPtr(base::in_place_t, Args&&... args) |
+ : value_(std::forward<Args>(args)...), state_(VALID) {} |
+ |
template <typename U> |
U To() const { |
return TypeConverter<U, InlinedStructPtr>::Convert(*this); |
@@ -204,11 +193,7 @@ class InlinedStructPtr { |
explicit operator bool() const { return !is_null(); } |
private: |
- friend class internal::StructHelper<Struct>; |
friend class internal::InlinedStructPtrWTFHelper<Struct>; |
- |
- void Initialize() { state_ = VALID; } |
- |
void Take(InlinedStructPtr* other) { |
reset(); |
Swap(other); |