Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / mojo / public / cpp / bindings / struct_ptr.h
blobad77d60b08423ab87b20283a66a7363dbdf7e36d
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_
8 #include <new>
10 #include "mojo/public/cpp/environment/logging.h"
11 #include "mojo/public/cpp/system/macros.h"
13 namespace mojo {
14 namespace internal {
16 template <typename Struct>
17 class StructHelper {
18 public:
19 template <typename Ptr>
20 static void Initialize(Ptr* ptr) { ptr->Initialize(); }
23 } // namespace internal
25 template <typename Struct>
26 class StructPtr {
27 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(StructPtr, RValue);
28 public:
29 typedef typename Struct::Data_ Data_;
31 StructPtr() : ptr_(NULL) {}
32 ~StructPtr() {
33 delete ptr_;
36 StructPtr(RValue other) : ptr_(NULL) { Take(other.object); }
37 StructPtr& operator=(RValue other) {
38 Take(other.object);
39 return *this;
42 template <typename U>
43 U To() const {
44 return TypeConverter<StructPtr, U>::ConvertTo(*this);
47 void reset() {
48 if (ptr_) {
49 delete ptr_;
50 ptr_ = NULL;
54 bool is_null() const { return ptr_ == NULL; }
56 Struct& operator*() const {
57 MOJO_DCHECK(ptr_);
58 return *ptr_;
60 Struct* operator->() const {
61 MOJO_DCHECK(ptr_);
62 return ptr_;
64 Struct* get() const { return ptr_; }
66 void Swap(StructPtr* other) {
67 std::swap(ptr_, other->ptr_);
70 private:
71 typedef Struct* StructPtr::*Testable;
73 public:
74 operator Testable() const { return ptr_ ? &StructPtr::ptr_ : 0; }
76 private:
77 friend class internal::StructHelper<Struct>;
78 void Initialize() {
79 MOJO_DCHECK(!ptr_);
80 ptr_ = new Struct();
83 void Take(StructPtr* other) {
84 reset();
85 Swap(other);
88 Struct* ptr_;
91 // Designed to be used when Struct is small and copyable.
92 template <typename Struct>
93 class InlinedStructPtr {
94 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(InlinedStructPtr, RValue);
95 public:
96 typedef typename Struct::Data_ Data_;
98 InlinedStructPtr() : is_null_(true) {}
99 ~InlinedStructPtr() {}
101 InlinedStructPtr(RValue other) : is_null_(true) { Take(other.object); }
102 InlinedStructPtr& operator=(RValue other) {
103 Take(other.object);
104 return *this;
107 template <typename U>
108 U To() const {
109 return TypeConverter<InlinedStructPtr, U>::ConvertTo(*this);
112 void reset() {
113 is_null_ = true;
114 value_.~Struct();
115 new (&value_) Struct();
118 bool is_null() const { return is_null_; }
120 Struct& operator*() const {
121 MOJO_DCHECK(!is_null_);
122 return value_;
124 Struct* operator->() const {
125 MOJO_DCHECK(!is_null_);
126 return &value_;
128 Struct* get() const { return &value_; }
130 void Swap(InlinedStructPtr* other) {
131 std::swap(value_, other->value_);
132 std::swap(is_null_, other->is_null_);
135 private:
136 typedef Struct InlinedStructPtr::*Testable;
138 public:
139 operator Testable() const { return is_null_ ? 0 : &InlinedStructPtr::value_; }
141 private:
142 friend class internal::StructHelper<Struct>;
143 void Initialize() { is_null_ = false; }
145 void Take(InlinedStructPtr* other) {
146 reset();
147 Swap(other);
150 mutable Struct value_;
151 bool is_null_;
154 } // namespace mojo
156 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_